Skip to content

Commit 240dfb7

Browse files
lukas-krecan#155 Allow to change coumn names
1 parent abd1d56 commit 240dfb7

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/JdbcTemplateLockProvider.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,29 @@ public static class Configuration {
9090
private final PlatformTransactionManager transactionManager;
9191
private final String tableName;
9292
private final TimeZone timeZone;
93-
94-
Configuration(@NotNull JdbcTemplate jdbcTemplate, @Nullable PlatformTransactionManager transactionManager, @NotNull String tableName, @Nullable TimeZone timeZone) {
93+
private final String nameColumnName;
94+
private final String lockUntilColumnName;
95+
private final String lockedAtColumnName;
96+
private final String lockedByColumnName;
97+
98+
Configuration(
99+
@NotNull JdbcTemplate jdbcTemplate,
100+
@Nullable PlatformTransactionManager transactionManager,
101+
@NotNull String tableName,
102+
@Nullable TimeZone timeZone,
103+
@NotNull String nameColumnName,
104+
@NotNull String lockUntilColumnName,
105+
@NotNull String lockedAtColumnName,
106+
@NotNull String lockedByColumnName
107+
) {
95108
this.jdbcTemplate = requireNonNull(jdbcTemplate, "jdbcTemplate can not be null");
96109
this.transactionManager = transactionManager;
97110
this.tableName = requireNonNull(tableName, "tableName can not be null");
98111
this.timeZone = timeZone;
112+
this.nameColumnName = requireNonNull(nameColumnName, "'name' column name can not be null");
113+
this.lockUntilColumnName = requireNonNull(lockUntilColumnName, "'lockUntil' column name can not be null");
114+
this.lockedAtColumnName = requireNonNull(lockedAtColumnName, "'lockedAt' column name can not be null");
115+
this.lockedByColumnName = requireNonNull(lockedByColumnName, "'lockedBy' column name can not be null");
99116
}
100117

101118
public JdbcTemplate getJdbcTemplate() {
@@ -114,6 +131,22 @@ public TimeZone getTimeZone() {
114131
return timeZone;
115132
}
116133

134+
public String getNameColumnName() {
135+
return nameColumnName;
136+
}
137+
138+
public String getLockUntilColumnName() {
139+
return lockUntilColumnName;
140+
}
141+
142+
public String getLockedAtColumnName() {
143+
return lockedAtColumnName;
144+
}
145+
146+
public String getLockedByColumnName() {
147+
return lockedByColumnName;
148+
}
149+
117150
public static Configuration.Builder builder() {
118151
return new Configuration.Builder();
119152
}
@@ -123,6 +156,10 @@ public static class Builder {
123156
private PlatformTransactionManager transactionManager;
124157
private String tableName = DEFAULT_TABLE_NAME;
125158
private TimeZone timeZone;
159+
private String nameColumnName = "name";
160+
private String lockUntilColumnName = "lock_until";
161+
private String lockedAtColumnName = "locked_at";
162+
private String lockedByColumnName = "locked_by";
126163

127164
public Builder withJdbcTemplate(@NotNull JdbcTemplate jdbcTemplate) {
128165
this.jdbcTemplate = jdbcTemplate;
@@ -144,8 +181,16 @@ public Builder withTimeZone(TimeZone timeZone) {
144181
return this;
145182
}
146183

184+
public Builder withColumnNames(String nameColumnName, String lockUntilColumnName, String lockedAtColumnName, String lockedByColumnName) {
185+
this.nameColumnName = nameColumnName;
186+
this.lockUntilColumnName = lockUntilColumnName;
187+
this.lockedAtColumnName = lockedAtColumnName;
188+
this.lockedByColumnName = lockedByColumnName;
189+
return this;
190+
}
191+
147192
public JdbcTemplateLockProvider.Configuration build() {
148-
return new JdbcTemplateLockProvider.Configuration(jdbcTemplate, transactionManager, tableName, timeZone);
193+
return new JdbcTemplateLockProvider.Configuration(jdbcTemplate, transactionManager, tableName, timeZone, nameColumnName, lockUntilColumnName, lockedAtColumnName, lockedByColumnName);
149194
}
150195
}
151196

providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/JdbcTemplateStorageAccessor.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,24 @@
4242
* Spring JdbcTemplate based implementation usable in JTA environment
4343
*/
4444
class JdbcTemplateStorageAccessor extends AbstractStorageAccessor {
45-
private final String tableName;
4645
private final JdbcTemplate jdbcTemplate;
4746
private final TransactionTemplate transactionTemplate;
48-
private final TimeZone timeZone;
47+
private final Configuration configuration;
4948

5049
JdbcTemplateStorageAccessor(@NotNull Configuration configuration) {
51-
this.jdbcTemplate = requireNonNull(configuration.getJdbcTemplate(), "jdbcTemplate can not be null");
52-
this.tableName = requireNonNull(configuration.getTableName(), "tableName can not be null");
50+
this.configuration = requireNonNull(configuration, "configuration can not be null");
51+
this.jdbcTemplate = configuration.getJdbcTemplate();
5352
PlatformTransactionManager transactionManager = configuration.getTransactionManager() != null ?
5453
configuration.getTransactionManager() :
5554
new DataSourceTransactionManager(jdbcTemplate.getDataSource());
5655

5756
this.transactionTemplate = new TransactionTemplate(transactionManager);
5857
this.transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
59-
this.timeZone = configuration.getTimeZone();
6058
}
6159

6260
@Override
6361
public boolean insertRecord(@NotNull LockConfiguration lockConfiguration) {
64-
String sql = "INSERT INTO " + tableName + "(name, lock_until, locked_at, locked_by) VALUES(?, ?, ?, ?)";
62+
String sql = "INSERT INTO " + tableName() + "(" + name() + ", " + lockUntil() + ", " + lockedAt() + ", " + lockedBy() + ") VALUES(?, ?, ?, ?)";
6563
return transactionTemplate.execute(status -> {
6664
try {
6765
int insertedRows = jdbcTemplate.update(sql, preparedStatement -> {
@@ -82,8 +80,8 @@ public boolean insertRecord(@NotNull LockConfiguration lockConfiguration) {
8280

8381
@Override
8482
public boolean updateRecord(@NotNull LockConfiguration lockConfiguration) {
85-
String sql = "UPDATE " + tableName
86-
+ " SET lock_until = ?, locked_at = ?, locked_by = ? WHERE name = ? AND lock_until <= ?";
83+
String sql = "UPDATE " + tableName()
84+
+ " SET " + lockUntil() + " = ?, " + lockedAt() + " = ?, " + lockedBy() + " = ? WHERE " + name() + " = ? AND " + lockUntil() + " <= ?";
8785
return transactionTemplate.execute(status -> {
8886
int updatedRows = jdbcTemplate.update(sql, statement -> {
8987
Instant now = Instant.now();
@@ -99,8 +97,8 @@ public boolean updateRecord(@NotNull LockConfiguration lockConfiguration) {
9997

10098
@Override
10199
public boolean extend(@NotNull LockConfiguration lockConfiguration) {
102-
String sql = "UPDATE " + tableName
103-
+ " SET lock_until = ? WHERE name = ? AND locked_by = ? AND lock_until > ? ";
100+
String sql = "UPDATE " + tableName()
101+
+ " SET " + lockUntil() + " = ? WHERE " + name() + " = ? AND " + lockedBy() + " = ? AND " + lockUntil() + " > ? ";
104102

105103
logger.debug("Extending lock={} until={}", lockConfiguration.getName(), lockConfiguration.getLockAtMostUntil());
106104
return transactionTemplate.execute(status -> {
@@ -115,6 +113,7 @@ public boolean extend(@NotNull LockConfiguration lockConfiguration) {
115113
}
116114

117115
private void setTimestamp(PreparedStatement preparedStatement, int parameterIndex, Instant time) throws SQLException {
116+
TimeZone timeZone = configuration.getTimeZone();
118117
if (timeZone == null) {
119118
preparedStatement.setTimestamp(parameterIndex, Timestamp.from(time));
120119
} else {
@@ -124,7 +123,7 @@ private void setTimestamp(PreparedStatement preparedStatement, int parameterInde
124123

125124
@Override
126125
public void unlock(@NotNull LockConfiguration lockConfiguration) {
127-
String sql = "UPDATE " + tableName + " SET lock_until = ? WHERE name = ?";
126+
String sql = "UPDATE " + tableName() + " SET " + lockUntil() + " = ? WHERE " + name() + " = ?";
128127
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
129128
@Override
130129
protected void doInTransactionWithoutResult(TransactionStatus status) {
@@ -137,4 +136,24 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
137136
});
138137
}
139138

139+
private String name() {
140+
return configuration.getNameColumnName();
141+
}
142+
143+
private String lockUntil() {
144+
return configuration.getLockUntilColumnName();
145+
}
146+
147+
private String lockedAt() {
148+
return configuration.getLockedAtColumnName();
149+
}
150+
151+
private String lockedBy() {
152+
return configuration.getLockedByColumnName();
153+
}
154+
155+
private String tableName() {
156+
return configuration.getTableName();
157+
}
158+
140159
}

providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlJdbcTemplateLockProviderIntegrationTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,57 @@
1515
*/
1616
package net.javacrumbs.shedlock.provider.jdbctemplate;
1717

18+
import net.javacrumbs.shedlock.core.LockConfiguration;
19+
import net.javacrumbs.shedlock.core.SimpleLock;
1820
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
1921
import net.javacrumbs.shedlock.test.support.jdbc.AbstractHsqlJdbcLockProviderIntegrationTest;
22+
import org.junit.Test;
2023
import org.springframework.jdbc.core.JdbcTemplate;
2124

25+
import java.sql.Connection;
26+
import java.sql.SQLException;
27+
import java.sql.Statement;
28+
import java.time.Instant;
2229
import java.util.Calendar;
30+
import java.util.Optional;
2331
import java.util.TimeZone;
2432

33+
import static net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.Configuration.builder;
34+
2535
public class HsqlJdbcTemplateLockProviderIntegrationTest extends AbstractHsqlJdbcLockProviderIntegrationTest {
2636

2737
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("CEST");
2838

2939
@Override
3040
protected StorageBasedLockProvider getLockProvider() {
31-
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration.builder()
41+
return new JdbcTemplateLockProvider(builder()
3242
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
3343
.withTimeZone(TIME_ZONE)
3444
.build()
3545
);
3646
}
3747

48+
@Test
49+
public void shouldBeAbleToSetCustomColumnNames() throws SQLException {
50+
try (
51+
Connection conn = getDatasource().getConnection();
52+
Statement statement = conn.createStatement()
53+
) {
54+
statement.execute("CREATE TABLE shdlck(n VARCHAR(64), lck_untl TIMESTAMP(3), lckd_at TIMESTAMP(3), lckd_by VARCHAR(255), PRIMARY KEY (n))");
55+
}
56+
57+
JdbcTemplateLockProvider provider = new JdbcTemplateLockProvider(builder()
58+
.withTableName("shdlck")
59+
.withColumnNames("n", "lck_untl", "lckd_at", "lckd_by")
60+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
61+
.build());
62+
63+
Optional<SimpleLock> lock = provider.lock(new LockConfiguration("test", Instant.now().plusSeconds(10)));
64+
lock.get().unlock();
65+
}
66+
3867
@Override
3968
protected Calendar now() {
4069
return Calendar.getInstance(TIME_ZONE);
4170
}
42-
}
71+
}

0 commit comments

Comments
 (0)