Skip to content

Commit 3618965

Browse files
committed
GH-8623: DefLockRepository: back to LocalDateTime
Fixes #8623 Turns out not all JDBC drivers (or RDBMS vendors) support `java.time.Instant` mapping to their `TIMESTAMP` type. For example the PostgreSQL fails like: ``` org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.Instant. ``` * Use `LocalDateTime.now(ZoneOffset.UTC)` instead `Instant.now()`. Essentially bringing back the behavior from the previous version
1 parent 191f693 commit 3618965

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.integration.jdbc.lock;
1818

1919
import java.time.Duration;
20-
import java.time.Instant;
20+
import java.time.LocalDateTime;
21+
import java.time.ZoneOffset;
2122
import java.util.UUID;
2223

2324
import javax.sql.DataSource;
@@ -341,13 +342,13 @@ public boolean acquire(String lock) {
341342
Boolean result =
342343
this.serializableTransactionTemplate.execute(
343344
transactionStatus -> {
344-
if (this.template.update(this.updateQuery, this.id, Instant.now(),
345-
this.region, lock, this.id, Instant.now().minus(this.ttl)) > 0) {
345+
if (this.template.update(this.updateQuery, this.id, epochMillis(),
346+
this.region, lock, this.id, ttlEpochMillis()) > 0) {
346347
return true;
347348
}
348349
try {
349350
return this.template.update(this.insertQuery, this.region, lock, this.id,
350-
Instant.now()) > 0;
351+
epochMillis()) > 0;
351352
}
352353
catch (DataIntegrityViolationException ex) {
353354
return false;
@@ -362,24 +363,31 @@ public boolean isAcquired(String lock) {
362363
transactionStatus ->
363364
Integer.valueOf(1).equals(
364365
this.template.queryForObject(this.countQuery,
365-
Integer.class, this.region, lock, this.id,
366-
Instant.now().minus(this.ttl))));
366+
Integer.class, this.region, lock, this.id, ttlEpochMillis())));
367367
return Boolean.TRUE.equals(result);
368368
}
369369

370370
@Override
371371
public void deleteExpired() {
372372
this.defaultTransactionTemplate.executeWithoutResult(
373373
transactionStatus ->
374-
this.template.update(this.deleteExpiredQuery, this.region, Instant.now().minus(this.ttl)));
374+
this.template.update(this.deleteExpiredQuery, this.region, ttlEpochMillis()));
375375
}
376376

377377
@Override
378378
public boolean renew(String lock) {
379379
final Boolean result = this.defaultTransactionTemplate.execute(
380380
transactionStatus ->
381-
this.template.update(this.renewQuery, Instant.now(), this.region, lock, this.id) > 0);
381+
this.template.update(this.renewQuery, epochMillis(), this.region, lock, this.id) > 0);
382382
return Boolean.TRUE.equals(result);
383383
}
384384

385+
private LocalDateTime ttlEpochMillis() {
386+
return epochMillis().minus(this.ttl);
387+
}
388+
389+
private static LocalDateTime epochMillis() {
390+
return LocalDateTime.now(ZoneOffset.UTC);
391+
}
392+
385393
}

0 commit comments

Comments
 (0)