Skip to content

Commit 675edbb

Browse files
authored
Merge pull request lukas-krecan#237 from lukas-krecan/server-time
Honor useServerTime flag
2 parents 6f08186 + e051add commit 675edbb

12 files changed

+307
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.javacrumbs.shedlock.provider.jdbctemplate;
2+
3+
class PostgresSqlStatementsSource extends SqlStatementsSource {
4+
PostgresSqlStatementsSource(JdbcTemplateLockProvider.Configuration configuration) {
5+
super(configuration);
6+
}
7+
8+
@Override
9+
String getInsertStatement() {
10+
return super.getInsertStatement() + " ON CONFLICT (" + name() + ") DO UPDATE " +
11+
"SET " + lockUntil() + " = :lockUntil, " + lockedAt() + " = :now, " + lockedBy() + " = :lockedBy " +
12+
"WHERE " + tableName() + "." + lockUntil() + " <= :now";
13+
}
14+
15+
}

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,35 @@ class SqlStatementsSource {
2828
static SqlStatementsSource create(Configuration configuration) {
2929
String databaseProductName = getDatabaseProductName(configuration);
3030

31-
switch (databaseProductName) {
32-
case "PostgreSQL":
31+
if (configuration.getUseServerTime()) {
32+
switch (databaseProductName) {
33+
case "PostgreSQL":
34+
logger.debug("Using PostgresSqlServerTimeStatementsSource");
35+
return new PostgresSqlServerTimeStatementsSource(configuration);
36+
case "Microsoft SQL Server":
37+
logger.debug("Using MsSqlServerTimeStatementsSource");
38+
return new MsSqlServerTimeStatementsSource(configuration);
39+
case "Oracle":
40+
logger.debug("Using OracleServerTimeStatementsSource");
41+
return new OracleServerTimeStatementsSource(configuration);
42+
case "MySQL":
43+
logger.debug("Using MySqlServerTimeStatementsSource");
44+
return new MySqlServerTimeStatementsSource(configuration);
45+
case "MariaDB":
46+
logger.debug("Using MySqlServerTimeStatementsSource (for MariaDB)");
47+
return new MySqlServerTimeStatementsSource(configuration);
48+
default:
49+
logger.debug("Using SqlStatementsSource");
50+
return new SqlStatementsSource(configuration);
51+
}
52+
} else {
53+
if ("PostgreSQL".equals(databaseProductName)) {
3354
logger.debug("Using PostgresSqlServerTimeStatementsSource");
34-
return new PostgresSqlServerTimeStatementsSource(configuration);
35-
case "Microsoft SQL Server":
36-
logger.debug("Using MsSqlServerTimeStatementsSource");
37-
return new MsSqlServerTimeStatementsSource(configuration);
38-
case "Oracle":
39-
logger.debug("Using OracleServerTimeStatementsSource");
40-
return new OracleServerTimeStatementsSource(configuration);
41-
case "MySQL":
42-
logger.debug("Using MySqlServerTimeStatementsSource");
43-
return new MySqlServerTimeStatementsSource(configuration);
44-
case "MariaDB":
45-
logger.debug("Using MySqlServerTimeStatementsSource (for MariaDB)");
46-
return new MySqlServerTimeStatementsSource(configuration);
47-
default:
55+
return new PostgresSqlStatementsSource(configuration);
56+
} else {
4857
logger.debug("Using SqlStatementsSource");
4958
return new SqlStatementsSource(configuration);
59+
}
5060
}
5161
}
5262

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,10 @@
1717

1818
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
1919
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMariaDbJdbcLockProviderIntegrationTest;
20-
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMySqlJdbcLockProviderIntegrationTest;
21-
22-
import static org.assertj.core.api.Assertions.assertThat;
2320

2421
public class MariaDbJdbcTemplateLockProviderIntegrationTest extends AbstractMariaDbJdbcLockProviderIntegrationTest {
2522
@Override
2623
protected StorageBasedLockProvider getLockProvider() {
2724
return new JdbcTemplateLockProvider(getDatasource());
2825
}
29-
30-
@Override
31-
protected void assertUnlocked(String lockName) {
32-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
33-
}
34-
35-
@Override
36-
protected void assertLocked(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-
}
3926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.AbstractMariaDbJdbcLockProviderIntegrationTest;
20+
import org.springframework.jdbc.core.JdbcTemplate;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class MariaDbServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractMariaDbJdbcLockProviderIntegrationTest {
25+
@Override
26+
protected StorageBasedLockProvider getLockProvider() {
27+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
28+
.builder()
29+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
30+
.usingServerTime()
31+
.build()
32+
);
33+
}
34+
@Override
35+
protected void assertUnlocked(String lockName) {
36+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
37+
}
38+
39+
@Override
40+
protected void assertLocked(String lockName) {
41+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
42+
}
43+
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,10 @@
1717

1818
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
1919
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMsSqlServerJdbcLockProviderIntegrationTest;
20-
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMySqlJdbcLockProviderIntegrationTest;
21-
22-
import static org.assertj.core.api.Assertions.assertThat;
2320

2421
public class MsSqlServerJdbcTemplateLockProviderIntegrationTest extends AbstractMsSqlServerJdbcLockProviderIntegrationTest {
2522
@Override
2623
protected StorageBasedLockProvider getLockProvider() {
2724
return new JdbcTemplateLockProvider(getDatasource());
2825
}
29-
30-
@Override
31-
protected void assertUnlocked(String lockName) {
32-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= current_timestamp", new Object[]{lockName}, Integer.class)).isEqualTo(1);
33-
}
34-
35-
@Override
36-
protected void assertLocked(String lockName) {
37-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > current_timestamp", new Object[]{lockName}, Integer.class)).isEqualTo(1);
38-
}
3926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.AbstractMsSqlServerJdbcLockProviderIntegrationTest;
20+
import org.springframework.jdbc.core.JdbcTemplate;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class MsSqlServerServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractMsSqlServerJdbcLockProviderIntegrationTest {
25+
@Override
26+
protected StorageBasedLockProvider getLockProvider() {
27+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
28+
.builder()
29+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
30+
.usingServerTime()
31+
.build()
32+
);
33+
}
34+
@Override
35+
protected void assertUnlocked(String lockName) {
36+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= current_timestamp", new Object[]{lockName}, Integer.class)).isEqualTo(1);
37+
}
38+
39+
@Override
40+
protected void assertLocked(String lockName) {
41+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > current_timestamp", new Object[]{lockName}, Integer.class)).isEqualTo(1);
42+
}
43+
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,9 @@
1818
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
1919
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMySqlJdbcLockProviderIntegrationTest;
2020

21-
import static org.assertj.core.api.Assertions.assertThat;
22-
2321
public class MySqlJdbcTemplateLockProviderIntegrationTest extends AbstractMySqlJdbcLockProviderIntegrationTest {
2422
@Override
2523
protected StorageBasedLockProvider getLockProvider() {
2624
return new JdbcTemplateLockProvider(getDatasource());
2725
}
28-
29-
@Override
30-
protected void assertUnlocked(String lockName) {
31-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
32-
}
33-
34-
@Override
35-
protected void assertLocked(String lockName) {
36-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > now()", new Object[]{lockName}, Integer.class)).isEqualTo(1);
37-
}
3826
}
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.AbstractMySqlJdbcLockProviderIntegrationTest;
20+
import org.springframework.jdbc.core.JdbcTemplate;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class MySqlServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractMySqlJdbcLockProviderIntegrationTest {
25+
@Override
26+
protected StorageBasedLockProvider getLockProvider() {
27+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
28+
.builder()
29+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
30+
.usingServerTime()
31+
.build()
32+
);
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+
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,11 @@
1616
package net.javacrumbs.shedlock.provider.jdbctemplate;
1717

1818
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
19-
import net.javacrumbs.shedlock.test.support.jdbc.AbstractMsSqlServerJdbcLockProviderIntegrationTest;
2019
import net.javacrumbs.shedlock.test.support.jdbc.AbstractOracleJdbcLockProviderIntegrationTest;
2120

22-
import static org.assertj.core.api.Assertions.assertThat;
23-
2421
public class OracleJdbcTemplateLockProviderIntegrationTest extends AbstractOracleJdbcLockProviderIntegrationTest {
2522
@Override
2623
protected StorageBasedLockProvider getLockProvider() {
2724
return new JdbcTemplateLockProvider(getDatasource());
2825
}
29-
30-
@Override
31-
protected void assertUnlocked(String lockName) {
32-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= current_timestamp(3)", new Object[]{lockName}, Integer.class)).isEqualTo(1);
33-
}
34-
35-
@Override
36-
protected void assertLocked(String lockName) {
37-
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > current_timestamp(3)", new Object[]{lockName}, Integer.class)).isEqualTo(1);
38-
}
3926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.AbstractOracleJdbcLockProviderIntegrationTest;
20+
import org.springframework.jdbc.core.JdbcTemplate;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
public class OracleServerTimeJdbcTemplateLockProviderIntegrationTest extends AbstractOracleJdbcLockProviderIntegrationTest {
25+
@Override
26+
protected StorageBasedLockProvider getLockProvider() {
27+
return new JdbcTemplateLockProvider(JdbcTemplateLockProvider.Configuration
28+
.builder()
29+
.withJdbcTemplate(new JdbcTemplate(getDatasource()))
30+
.usingServerTime()
31+
.build()
32+
);
33+
}
34+
@Override
35+
protected void assertUnlocked(String lockName) {
36+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until <= current_timestamp(3)", new Object[]{lockName}, Integer.class)).isEqualTo(1);
37+
}
38+
39+
@Override
40+
protected void assertLocked(String lockName) {
41+
assertThat(testUtils.getJdbcTemplate().queryForObject("SELECT count(*) FROM shedlock WHERE name = ? and lock_until > current_timestamp(3)", new Object[]{lockName}, Integer.class)).isEqualTo(1);
42+
}
43+
}

0 commit comments

Comments
 (0)