From 5e83258d7074030343c9811a9be6af746cb0bb87 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Wed, 28 Aug 2024 10:48:52 +0200 Subject: [PATCH] Make sure that we always get the latest change log version id Even though we are ordering by DATEEXECUTED, and the last ID should be the last executed one. It is still possible that there are multiple entries with the same DATEEXECUTED value and the order might not be correct. e.g. MySQL 8.0 sometimes does not return the correct order. --- .../db/EngineSqlScriptBasedDbSchemaManager.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/db/EngineSqlScriptBasedDbSchemaManager.java b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/db/EngineSqlScriptBasedDbSchemaManager.java index 945e6c53344..b60d7e93491 100644 --- a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/db/EngineSqlScriptBasedDbSchemaManager.java +++ b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/db/EngineSqlScriptBasedDbSchemaManager.java @@ -199,14 +199,22 @@ protected ChangeLogVersion getChangeLogVersion() { } try (PreparedStatement statement = databaseConfiguration.getConnection() .prepareStatement("select ID from " + changeLogTableName + " order by DATEEXECUTED")) { - String changeLogVersion = null; + int changeLogVersion = 0; try (ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { - changeLogVersion = resultSet.getString(1); + String changeLogVersionId = resultSet.getString(1); + int parsedChangeLogVersion = Integer.parseInt(changeLogVersionId); + if (parsedChangeLogVersion > changeLogVersion) { + // Even though we are ordering by DATEEXECUTED, and the last ID should be the last executed one. + // It is still possible that there are multiple entries with the same DATEEXECUTED value and the order might not be correct. + // e.g. MySQL 8.0 sometimes does not return the correct order. + changeLogVersion = parsedChangeLogVersion; + } } } - if (changeLogVersion != null) { - return new ChangeLogVersion(changeLogVersion, getDbVersionForChangelogVersion(changeLogVersion)); + if (changeLogVersion > 0) { + String changeLogVersionString = String.valueOf(changeLogVersion); + return new ChangeLogVersion(changeLogVersionString, getDbVersionForChangelogVersion(changeLogVersionString)); } } catch (SQLException e) { throw new RuntimeException("Failed to get change log version from " + changeLogTableName, e);