Skip to content

Commit

Permalink
fixup! [liquibase#23] Ensure exclusive connection
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Jan 5, 2016
1 parent 8f243c4 commit 0649078
Showing 1 changed file with 15 additions and 11 deletions.
Expand Up @@ -64,7 +64,7 @@ private LockableConnection(Connection delegate) {
public static LockableConnection acquire(Connection delegate) {
LockableConnection connection = new LockableConnection(delegate);
try {
connection.initiateLocking();
connection.acquireLock();
return connection;
} catch (RuntimeException e) {
try {
Expand All @@ -85,7 +85,7 @@ public static LockableConnection acquire(Connection delegate) {
*/
@Override
public void close() throws SQLException {
Runtime.getRuntime().removeShutdownHook(task);
removeShutdownHook();
releaseLock();
delegate.close();
}
Expand Down Expand Up @@ -302,12 +302,6 @@ public void setSchema(String schema) throws SQLException {
delegate.setSchema(schema);
}

private final void initiateLocking() {
registerLockRemovalHook();
ensureLockUnicity();
tryWriteLock();
}

final void releaseLock() {
try (PreparedStatement statement = prepareStatement(
"MATCH (lock:__LiquigraphLock {uuid:{1}}) DELETE lock")) {
Expand All @@ -323,14 +317,24 @@ final void releaseLock() {
}
}

private final void registerLockRemovalHook() {
private final void acquireLock() {
addShutdownHook();
ensureLockUnicity();
tryWriteLock();
}

private final void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(task);
}

private final void removeShutdownHook() {
Runtime.getRuntime().removeShutdownHook(task);
}

private final void ensureLockUnicity() {
try (Statement statement = delegate.createStatement()) {
statement.execute("CREATE CONSTRAINT ON (lock:__LiquigraphLock) ASSERT lock.name IS UNIQUE");
delegate.commit();
commit();
}
catch (SQLException e) {
throw new LiquigraphLockException(
Expand All @@ -348,7 +352,7 @@ private final void tryWriteLock() {

statement.setString(1, uuid.toString());
statement.execute();
delegate.commit();
commit();
}
catch (SQLException e) {
throw new LiquigraphLockException(
Expand Down

0 comments on commit 0649078

Please sign in to comment.