Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,6 @@ private CommunityLockEntry getLockEntry(Connection conn, String key) throws SQLE


private void upsertLockEntry(Connection conn, String key, String owner, LocalDateTime expiresAt) throws SQLException {
dialectHelper.upsertLockEntry(conn, lockRepositoryName, key, owner, expiresAt);
dialectHelper.upsertLockEntry(conn, lockRepositoryName, key, owner, LockStatus.LOCK_HELD.name(), expiresAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void storeLock(LockKey key, LockAcquisition acquisition) {
delete.setString(1, keyStr);
delete.executeUpdate();
}
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), expiresAt);
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), LockStatus.LOCK_HELD.name(), expiresAt);
connection.commit();
return;
} else {
Expand All @@ -119,7 +119,7 @@ public void storeLock(LockKey key, LockAcquisition acquisition) {
if (existing == null ||
owner.toString().equals(existing.getOwner()) ||
LocalDateTime.now().isAfter(existing.getExpiresAt())) {
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), expiresAt);
dialectHelper.upsertLockEntry(connection, lockTableName, keyStr, owner.toString(), LockStatus.LOCK_HELD.name(), expiresAt);
// Commit for all dialects except Informix (which uses auto-commit above)
if (dialectHelper.getSqlDialect() != SqlDialect.INFORMIX) {
connection.commit();
Expand Down
1 change: 0 additions & 1 deletion utils/sql-util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ java {
}

dependencies {
implementation(project(":core:flamingock-core"))
implementation("com.zaxxer:HikariCP:3.4.5")
implementation("org.testcontainers:testcontainers-junit-jupiter:2.0.2")
// SQL Testcontainers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.flamingock.internal.common.sql.SqlDialectFactory;
import io.flamingock.internal.common.sql.SqlDialect;
import io.flamingock.internal.core.external.store.lock.LockStatus;

import java.sql.*;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -254,14 +253,14 @@ public String getDeleteLockSqlString(String tableName) {
}
}

public void upsertLockEntry(Connection conn, String tableName, String key, String owner, LocalDateTime expiresAt) throws SQLException {
public void upsertLockEntry(Connection conn, String tableName, String key, String owner, String lockStatus, LocalDateTime expiresAt) throws SQLException {
String sql = getInsertOrUpdateLockSqlString(tableName);

if (sqlDialect == SqlDialect.DB2) {
// UPDATE first
try (PreparedStatement update = conn.prepareStatement(
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
update.setString(1, LockStatus.LOCK_HELD.name());
update.setString(1, lockStatus);
update.setString(2, owner);
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
update.setString(4, key);
Expand All @@ -275,7 +274,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
try (PreparedStatement insert = conn.prepareStatement(
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
insert.setString(1, key);
insert.setString(2, LockStatus.LOCK_HELD.name());
insert.setString(2, lockStatus);
insert.setString(3, owner);
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
insert.executeUpdate();
Expand All @@ -287,7 +286,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
// Try UPDATE first
try (PreparedStatement update = conn.prepareStatement(
"UPDATE " + tableName + " SET status = ?, owner = ?, expires_at = ? WHERE lock_key = ?")) {
update.setString(1, LockStatus.LOCK_HELD.name());
update.setString(1, lockStatus);
update.setString(2, owner);
update.setTimestamp(3, Timestamp.valueOf(expiresAt));
update.setString(4, key);
Expand All @@ -301,7 +300,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
try (PreparedStatement insert = conn.prepareStatement(
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
insert.setString(1, key);
insert.setString(2, LockStatus.LOCK_HELD.name());
insert.setString(2, lockStatus);
insert.setString(3, owner);
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
insert.executeUpdate();
Expand All @@ -313,12 +312,12 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
// For SQL Server/Sybase, use Statement and format SQL
try (Statement stmt = conn.createStatement()) {
String formattedSql = sql
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
.replaceFirst("\\?", "'" + lockStatus + "'")
.replaceFirst("\\?", "'" + owner + "'")
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'")
.replaceFirst("\\?", "'" + key + "'")
.replaceFirst("\\?", "'" + key + "'")
.replaceFirst("\\?", "'" + LockStatus.LOCK_HELD.name() + "'")
.replaceFirst("\\?", "'" + lockStatus + "'")
.replaceFirst("\\?", "'" + owner + "'")
.replaceFirst("\\?", "'" + Timestamp.valueOf(expiresAt) + "'");
stmt.execute(formattedSql);
Expand All @@ -328,7 +327,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin

if (sqlDialect == SqlDialect.FIREBIRD) {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, LockStatus.LOCK_HELD.name());
ps.setString(1, lockStatus);
ps.setString(2, owner);
ps.setTimestamp(3, Timestamp.valueOf(expiresAt));
ps.setString(4, key);
Expand All @@ -337,7 +336,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
String insertSql = "INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)";
try (PreparedStatement ins = conn.prepareStatement(insertSql)) {
ins.setString(1, key);
ins.setString(2, LockStatus.LOCK_HELD.name());
ins.setString(2, lockStatus);
ins.setString(3, owner);
ins.setTimestamp(4, Timestamp.valueOf(expiresAt));
ins.executeUpdate();
Expand All @@ -352,7 +351,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
try (PreparedStatement insert = conn.prepareStatement(
"INSERT INTO " + tableName + " (lock_key, status, owner, expires_at) VALUES (?, ?, ?, ?)")) {
insert.setString(1, key);
insert.setString(2, LockStatus.LOCK_HELD.name());
insert.setString(2, lockStatus);
insert.setString(3, owner);
insert.setTimestamp(4, Timestamp.valueOf(expiresAt));
insert.executeUpdate();
Expand All @@ -364,7 +363,7 @@ public void upsertLockEntry(Connection conn, String tableName, String key, Strin
// Default case for other dialects
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, key);
ps.setString(2, LockStatus.LOCK_HELD.name());
ps.setString(2, lockStatus);
ps.setString(3, owner);
ps.setTimestamp(4, Timestamp.valueOf(expiresAt));
ps.executeUpdate();
Expand Down
Loading