From 3618965491f47ac889f9d3e9cb71f7da957e58cc Mon Sep 17 00:00:00 2001 From: abilan Date: Tue, 16 May 2023 11:00:14 -0400 Subject: [PATCH] GH-8623: DefLockRepository: back to LocalDateTime Fixes https://github.com/spring-projects/spring-integration/issues/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 --- .../jdbc/lock/DefaultLockRepository.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java index 1b7e9c5d06..7d317bbb85 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java @@ -17,7 +17,8 @@ package org.springframework.integration.jdbc.lock; import java.time.Duration; -import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.UUID; import javax.sql.DataSource; @@ -341,13 +342,13 @@ public class DefaultLockRepository Boolean result = this.serializableTransactionTemplate.execute( transactionStatus -> { - if (this.template.update(this.updateQuery, this.id, Instant.now(), - this.region, lock, this.id, Instant.now().minus(this.ttl)) > 0) { + if (this.template.update(this.updateQuery, this.id, epochMillis(), + this.region, lock, this.id, ttlEpochMillis()) > 0) { return true; } try { return this.template.update(this.insertQuery, this.region, lock, this.id, - Instant.now()) > 0; + epochMillis()) > 0; } catch (DataIntegrityViolationException ex) { return false; @@ -362,8 +363,7 @@ public class DefaultLockRepository transactionStatus -> Integer.valueOf(1).equals( this.template.queryForObject(this.countQuery, - Integer.class, this.region, lock, this.id, - Instant.now().minus(this.ttl)))); + Integer.class, this.region, lock, this.id, ttlEpochMillis()))); return Boolean.TRUE.equals(result); } @@ -371,15 +371,23 @@ public class DefaultLockRepository public void deleteExpired() { this.defaultTransactionTemplate.executeWithoutResult( transactionStatus -> - this.template.update(this.deleteExpiredQuery, this.region, Instant.now().minus(this.ttl))); + this.template.update(this.deleteExpiredQuery, this.region, ttlEpochMillis())); } @Override public boolean renew(String lock) { final Boolean result = this.defaultTransactionTemplate.execute( transactionStatus -> - this.template.update(this.renewQuery, Instant.now(), this.region, lock, this.id) > 0); + this.template.update(this.renewQuery, epochMillis(), this.region, lock, this.id) > 0); return Boolean.TRUE.equals(result); } + private LocalDateTime ttlEpochMillis() { + return epochMillis().minus(this.ttl); + } + + private static LocalDateTime epochMillis() { + return LocalDateTime.now(ZoneOffset.UTC); + } + }