Skip to content

Commit 7286d8f

Browse files
committed
Fix such that there is a rollback() before LogicalLock unlock()
When there is an error during migration and using logical lock then ensure there is a rollback before the unlock() on the logical lock.
1 parent 2a1e5eb commit 7286d8f

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationEngine.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.ebean.migration.MigrationException;
66
import io.ebean.migration.MigrationResource;
77

8-
import java.io.IOException;
98
import java.sql.Connection;
109
import java.sql.SQLException;
1110
import java.util.Collections;
@@ -44,7 +43,6 @@ public List<MigrationResource> run(Connection connection) {
4443
}
4544

4645
long startMs = System.currentTimeMillis();
47-
connection.setAutoCommit(false);
4846
MigrationTable table = initialiseMigrationTable(connection);
4947
try {
5048
List<MigrationResource> result = runMigrations(resources.versions(), table, checkStateOnly);
@@ -58,30 +56,31 @@ public List<MigrationResource> run(Connection connection) {
5856
}
5957
}
6058
return result;
59+
} catch (Exception e) {
60+
log.log(ERROR, "Perform rollback due to DB migration error", e);
61+
rollback(connection);
62+
throw new MigrationException("Error running DB migrations", e);
6163
} finally {
6264
table.unlockMigrationTable();
6365
}
64-
65-
} catch (MigrationException e) {
66-
rollback(connection);
67-
throw e;
68-
69-
} catch (Exception e) {
70-
rollback(connection);
71-
throw new MigrationException("Error running DB migrations", e);
72-
7366
} finally {
7467
close(connection);
7568
}
7669
}
7770

78-
private MigrationTable initialiseMigrationTable(Connection connection) throws SQLException, IOException {
79-
MigrationPlatform platform = derivePlatformName(migrationConfig, connection);
80-
new MigrationSchema(migrationConfig, connection).createAndSetIfNeeded();
71+
private MigrationTable initialiseMigrationTable(Connection connection) {
72+
try {
73+
connection.setAutoCommit(false);
74+
MigrationPlatform platform = derivePlatformName(migrationConfig, connection);
75+
new MigrationSchema(migrationConfig, connection).createAndSetIfNeeded();
8176

82-
final MigrationTable table = new MigrationTable(migrationConfig, connection, checkStateOnly, platform);
83-
table.createIfNeededAndLock();
84-
return table;
77+
final MigrationTable table = new MigrationTable(migrationConfig, connection, checkStateOnly, platform);
78+
table.createIfNeededAndLock();
79+
return table;
80+
} catch (Exception e) {
81+
rollback(connection);
82+
throw new MigrationException("Error initialising db migrations table", e);
83+
}
8584
}
8685

8786
/**

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationPlatform.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ebean.migration.runner;
22

33
import io.ebean.ddlrunner.DdlDetect;
4+
import io.ebean.migration.MigrationException;
45

56
import java.sql.*;
67
import java.util.ArrayList;
@@ -30,7 +31,7 @@ DdlDetect ddlDetect() {
3031
return DdlDetect.NONE;
3132
}
3233

33-
void unlockMigrationTable(String sqlTable, Connection connection) throws SQLException {
34+
void unlockMigrationTable(String sqlTable, Connection connection) {
3435
// do nothing by default for select for update case
3536
}
3637

@@ -118,9 +119,14 @@ void lockMigrationTable(String sqlTable, Connection connection) throws SQLExcept
118119
}
119120

120121
@Override
121-
void unlockMigrationTable(String sqlTable, Connection connection) throws SQLException {
122-
releaseLogicalLock(sqlTable, connection);
123-
connection.commit();
122+
void unlockMigrationTable(String sqlTable, Connection connection) {
123+
try {
124+
releaseLogicalLock(sqlTable, connection);
125+
connection.commit();
126+
} catch (SQLException e) {
127+
rollback(connection);
128+
throw new MigrationException("Error releasing logical lock for ebean migrations");
129+
}
124130
}
125131

126132
private boolean obtainLogicalLock(String sqlTable, Connection connection) throws SQLException {
@@ -190,11 +196,16 @@ private boolean obtainNamedLock(Connection connection) throws SQLException {
190196
return false;
191197
}
192198

199+
@SuppressWarnings("all")
193200
@Override
194-
void unlockMigrationTable(String sqlTable, Connection connection) throws SQLException {
195-
String hash = Integer.toHexString(connection.getMetaData().getURL().hashCode());
196-
try (Statement query = connection.createStatement()) {
197-
query.execute("select release_lock('ebean_migration-" + hash + "')");
201+
void unlockMigrationTable(String sqlTable, Connection connection) {
202+
try {
203+
String hash = Integer.toHexString(connection.getMetaData().getURL().hashCode());
204+
try (Statement query = connection.createStatement()) {
205+
query.execute("select release_lock('ebean_migration-" + hash + "')");
206+
}
207+
} catch (SQLException e) {
208+
throw new MigrationException("Error releasing lock for ebean_migration", e);
198209
}
199210
}
200211
}

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void obtainLockWithWait() throws SQLException {
176176
/**
177177
* Release a lock on the migration table (MySql, MariaDB only).
178178
*/
179-
void unlockMigrationTable() throws SQLException {
179+
void unlockMigrationTable() {
180180
platform.unlockMigrationTable(sqlTable, connection);
181181
}
182182

0 commit comments

Comments
 (0)