Skip to content

Commit 5907b1d

Browse files
committed
H2 and HSQL support server time
1 parent 977deb3 commit 5907b1d

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.javacrumbs.shedlock.provider.jdbctemplate;
2+
3+
import net.javacrumbs.shedlock.core.LockConfiguration;
4+
import net.javacrumbs.shedlock.support.annotation.NonNull;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
class H2ServerTimeStatementsSource extends SqlStatementsSource {
10+
private final String now = "CURRENT_TIMESTAMP(3)";
11+
private final String lockAtMostFor = "TIMESTAMPADD(MICROSECOND, :lockAtMostForMicros, " + now + ")";
12+
13+
H2ServerTimeStatementsSource(JdbcTemplateLockProvider.Configuration configuration) {
14+
super(configuration);
15+
}
16+
17+
@Override
18+
String getInsertStatement() {
19+
return "INSERT INTO " + tableName() + "(" + name() + ", " + lockUntil() + ", " + lockedAt() + ", " + lockedBy() + ") VALUES(:name, " + lockAtMostFor + ", " + now + ", :lockedBy)";
20+
}
21+
22+
@Override
23+
public String getUpdateStatement() {
24+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now;
25+
}
26+
27+
@Override
28+
public String getUnlockStatement() {
29+
String lockAtLeastFor = "TIMESTAMPADD(MICROSECOND, :lockAtLeastForMicros, " + lockedAt() + ")";
30+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = CASE WHEN " + lockAtLeastFor + " > " + now + " THEN " + lockAtLeastFor + " ELSE " + now + " END WHERE " + name() + " = :name AND " + lockedBy() + " = :lockedBy";
31+
}
32+
33+
@Override
34+
public String getExtendStatement() {
35+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + " WHERE " + name() + " = :name AND " + lockedBy() + " = :lockedBy AND " + lockUntil() + " > " + now;
36+
}
37+
38+
@Override
39+
@NonNull
40+
Map<String, Object> params(@NonNull LockConfiguration lockConfiguration) {
41+
Map<String, Object> params = new HashMap<>();
42+
params.put("name", lockConfiguration.getName());
43+
params.put("lockedBy", configuration.getLockedByValue());
44+
params.put("lockAtMostForMicros", lockConfiguration.getLockAtMostFor().toNanos() / 1_000);
45+
params.put("lockAtLeastForMicros", lockConfiguration.getLockAtLeastFor().toNanos() / 1_000);
46+
return params;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.javacrumbs.shedlock.provider.jdbctemplate;
2+
3+
import net.javacrumbs.shedlock.core.LockConfiguration;
4+
import net.javacrumbs.shedlock.support.annotation.NonNull;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
class HsqlServerTimeStatementsSource extends SqlStatementsSource {
10+
private final String now = "CURRENT_TIMESTAMP(3)";
11+
private final String lockAtMostFor = "TIMESTAMPADD(MICROSECOND, :lockAtMostForMicros, " + now + ")";
12+
13+
HsqlServerTimeStatementsSource(JdbcTemplateLockProvider.Configuration configuration) {
14+
super(configuration);
15+
}
16+
17+
@Override
18+
String getInsertStatement() {
19+
return "INSERT INTO " + tableName() + "(" + name() + ", " + lockUntil() + ", " + lockedAt() + ", " + lockedBy() + ") VALUES(:name, " + lockAtMostFor + ", " + now + ", :lockedBy)";
20+
}
21+
22+
@Override
23+
public String getUpdateStatement() {
24+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now;
25+
}
26+
27+
@Override
28+
public String getUnlockStatement() {
29+
String lockAtLeastFor = "TIMESTAMPADD(MICROSECOND, :lockAtLeastForMicros, " + lockedAt() + ")";
30+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = CASE WHEN " + lockAtLeastFor + " > " + now + " THEN " + lockAtLeastFor + " ELSE " + now + " END WHERE " + name() + " = :name AND " + lockedBy() + " = :lockedBy";
31+
}
32+
33+
@Override
34+
public String getExtendStatement() {
35+
return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + " WHERE " + name() + " = :name AND " + lockedBy() + " = :lockedBy AND " + lockUntil() + " > " + now;
36+
}
37+
38+
@Override
39+
@NonNull
40+
Map<String, Object> params(@NonNull LockConfiguration lockConfiguration) {
41+
Map<String, Object> params = new HashMap<>();
42+
params.put("name", lockConfiguration.getName());
43+
params.put("lockedBy", configuration.getLockedByValue());
44+
params.put("lockAtMostForMicros", lockConfiguration.getLockAtMostFor().toNanos() / 1_000);
45+
params.put("lockAtLeastForMicros", lockConfiguration.getLockAtLeastFor().toNanos() / 1_000);
46+
return params;
47+
}
48+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ static SqlStatementsSource create(Configuration configuration) {
4545
case "MariaDB":
4646
logger.debug("Using MySqlServerTimeStatementsSource (for MariaDB)");
4747
return new MySqlServerTimeStatementsSource(configuration);
48+
case "HSQL Database Engine":
49+
logger.debug("Using HsqlServerTimeStatementsSource");
50+
return new HsqlServerTimeStatementsSource(configuration);
51+
case "H2":
52+
logger.debug("Using H2ServerTimeStatementsSource");
53+
return new H2ServerTimeStatementsSource(configuration);
4854
default:
49-
throw new UnsupportedOperationException("DB time is not supported for " + databaseProductName);
55+
throw new UnsupportedOperationException("DB time is not supported for '" + databaseProductName + "'");
5056
}
5157
} else {
5258
if ("PostgreSQL".equals(databaseProductName)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2009-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.javacrumbs.shedlock.provider.jdbctemplate;
17+
18+
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
19+
import net.javacrumbs.shedlock.test.support.jdbc.AbstractH2JdbcLockProviderIntegrationTest;
20+
import net.javacrumbs.shedlock.test.support.jdbc.AbstractHsqlJdbcLockProviderIntegrationTest;
21+
import org.springframework.jdbc.core.JdbcTemplate;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class H2ServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractH2JdbcLockProviderIntegrationTest {
26+
@Override
27+
public StorageBasedLockProvider getLockProvider() {
28+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
29+
.builder()
30+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
31+
.usingDbTime()
32+
.build()
33+
);
34+
}
35+
@Override
36+
protected void assertUnlocked(String lockName) {
37+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
38+
}
39+
40+
@Override
41+
protected void assertLocked(String lockName) {
42+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2009-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.javacrumbs.shedlock.provider.jdbctemplate;
17+
18+
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
19+
import net.javacrumbs.shedlock.test.support.jdbc.AbstractHsqlJdbcLockProviderIntegrationTest;
20+
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMariaDbJdbcLockProviderIntegrationTest;
21+
import org.springframework.jdbc.core.JdbcTemplate;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class HsqlServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractHsqlJdbcLockProviderIntegrationTest {
26+
@Override
27+
public StorageBasedLockProvider getLockProvider() {
28+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
29+
.builder()
30+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
31+
.usingDbTime()
32+
.build()
33+
);
34+
}
35+
@Override
36+
protected void assertUnlocked(String lockName) {
37+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
38+
}
39+
40+
@Override
41+
protected void assertLocked(String lockName) {
42+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
43+
}
44+
}

0 commit comments

Comments
 (0)