From fb7f32d4a6824a53a203e0df0b383a6b8f146c20 Mon Sep 17 00:00:00 2001 From: Sadayuki Furuhashi Date: Fri, 23 Sep 2016 10:30:03 -0700 Subject: [PATCH] select first to not increment primary key foreign key constraint violation with h2 --- .../database/DatabaseSessionStoreManager.java | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/digdag-core/src/main/java/io/digdag/core/database/DatabaseSessionStoreManager.java b/digdag-core/src/main/java/io/digdag/core/database/DatabaseSessionStoreManager.java index 088c558553..86be68ee2d 100644 --- a/digdag-core/src/main/java/io/digdag/core/database/DatabaseSessionStoreManager.java +++ b/digdag-core/src/main/java/io/digdag/core/database/DatabaseSessionStoreManager.java @@ -965,33 +965,35 @@ public T putAndLockSession(Session session, SessionLockAction func) return DatabaseSessionStoreManager.this.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(),