Skip to content

Commit df1b8b4

Browse files
artembilangaryrussell
authored andcommitted
GH-3885: Change LockRepository to LocalDateTime
Fixed #3885 Turns out the `Date` is stored into DB without time zone, so instances in different zones may not react properly for updates * Change the logic in the `DefaultLockRepository` to store time via `LocalDateTime.now(ZoneOffset.UTC)` to ensure that all instance deals with the same time representation without time zone offsets
1 parent e2a5b7d commit df1b8b4

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.integration.jdbc.lock;
1818

19-
import java.util.Date;
19+
import java.time.LocalDateTime;
20+
import java.time.ZoneOffset;
21+
import java.time.temporal.ChronoUnit;
2022
import java.util.UUID;
2123

2224
import javax.sql.DataSource;
@@ -223,40 +225,46 @@ public void delete(String lock) {
223225

224226
@Override
225227
public boolean acquire(String lock) {
226-
return this.serializableTransactionTemplate.execute(transactionStatus -> {
227-
if (this.template.update(this.updateQuery, this.id, new Date(), this.region, lock, this.id,
228-
new Date(System.currentTimeMillis() - this.ttl)) > 0) {
229-
return true;
230-
}
231-
try {
232-
return this.template.update(this.insertQuery, this.region, lock, this.id, new Date()) > 0;
233-
}
234-
catch (DataIntegrityViolationException ex) {
235-
return false;
236-
}
237-
});
228+
return this.serializableTransactionTemplate.execute(
229+
transactionStatus -> {
230+
if (this.template.update(this.updateQuery, this.id, LocalDateTime.now(ZoneOffset.UTC),
231+
this.region, lock, this.id,
232+
LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)) > 0) {
233+
return true;
234+
}
235+
try {
236+
return this.template.update(this.insertQuery, this.region, lock, this.id,
237+
LocalDateTime.now(ZoneOffset.UTC)) > 0;
238+
}
239+
catch (DataIntegrityViolationException ex) {
240+
return false;
241+
}
242+
});
238243
}
239244

240245
@Override
241246
public boolean isAcquired(String lock) {
242-
return this.readOnlyTransactionTemplate.execute(transactionStatus ->
243-
this.template.queryForObject(this.countQuery, // NOSONAR query never returns null
244-
Integer.class, this.region, lock, this.id, new Date(System.currentTimeMillis() - this.ttl))
245-
== 1);
247+
return this.readOnlyTransactionTemplate.execute(
248+
transactionStatus ->
249+
this.template.queryForObject(this.countQuery, // NOSONAR query never returns null
250+
Integer.class, this.region, lock, this.id,
251+
LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)) == 1);
246252
}
247253

248254
@Override
249255
public void deleteExpired() {
250256
this.defaultTransactionTemplate.executeWithoutResult(
251257
transactionStatus ->
252258
this.template.update(this.deleteExpiredQuery, this.region,
253-
new Date(System.currentTimeMillis() - this.ttl)));
259+
LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)));
254260
}
255261

256262
@Override
257263
public boolean renew(String lock) {
258264
return this.defaultTransactionTemplate.execute(
259-
transactionStatus -> this.template.update(this.renewQuery, new Date(), this.region, lock, this.id) > 0);
265+
transactionStatus ->
266+
this.template.update(this.renewQuery, LocalDateTime.now(ZoneOffset.UTC),
267+
this.region, lock, this.id) > 0);
260268
}
261269

262270
}

0 commit comments

Comments
 (0)