Skip to content

Commit

Permalink
select first to not increment primary key foreign key constraint viol…
Browse files Browse the repository at this point in the history
…ation with h2
  • Loading branch information
frsyuki committed Sep 23, 2016
1 parent ea2c87a commit fb7f32d
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -965,33 +965,35 @@ public <T> T putAndLockSession(Session session, SessionLockAction<T> func)
return DatabaseSessionStoreManager.this.<T, ResourceConflictException, ResourceNotFoundException>transaction((handle, dao) -> {
StoredSession storedSession;

if (dao instanceof H2Dao) {
catchForeignKeyNotFound(
() -> {
((H2Dao) dao).upsertAndLockSession(
// select first so that conflicting insert (postgresql) or foreign key constraint violation (h2)
// doesn't increment sequence of primary key unnecessarily
storedSession = dao.getSessionByConflictedNamesInternal(
session.getProjectId(),
session.getWorkflowName(),
session.getSessionTime().getEpochSecond());

if (storedSession == null) {
if (dao instanceof H2Dao) {
catchForeignKeyNotFound(
() -> {
((H2Dao) dao).upsertAndLockSession(
session.getProjectId(),
session.getWorkflowName(),
session.getSessionTime().getEpochSecond());
return 0;
},
"project id=%d", session.getProjectId());
storedSession = dao.getSessionByConflictedNamesInternal(
session.getProjectId(),
session.getWorkflowName(),
session.getSessionTime().getEpochSecond());
return 0;
},
"project id=%d", session.getProjectId());
storedSession = dao.getSessionByConflictedNamesInternal(
session.getProjectId(),
session.getWorkflowName(),
session.getSessionTime().getEpochSecond());
if (storedSession == null) {
throw new IllegalStateException(String.format(ENGLISH,
"Database state error: locked session is null: project_id=%d, workflow_name=%s, session_time=%d",
session.getProjectId(), session.getWorkflowName(), session.getSessionTime().getEpochSecond()));
if (storedSession == null) {
throw new IllegalStateException(String.format(ENGLISH,
"Database state error: locked session is null: project_id=%d, workflow_name=%s, session_time=%d",
session.getProjectId(), session.getWorkflowName(), session.getSessionTime().getEpochSecond()));
}
}
}
else {
// select first so that conflicting insert doesn't increment sequence of primary key unnecessarily
storedSession = dao.getSessionByConflictedNamesInternal(
session.getProjectId(),
session.getWorkflowName(),
session.getSessionTime().getEpochSecond());
if (storedSession == null) {
else {
storedSession = catchForeignKeyNotFound(
() -> ((PgDao) dao).upsertAndLockSession(
session.getProjectId(),
Expand Down

0 comments on commit fb7f32d

Please sign in to comment.