62
62
* @author Gary Russell
63
63
* @author Alexandre Strubel
64
64
* @author Ruslan Stelmachenko
65
+ * @author Eddie Cho
65
66
*
66
67
* @since 4.3
67
68
*/
@@ -76,19 +77,12 @@ public class DefaultLockRepository
76
77
*/
77
78
public static final String DEFAULT_TABLE_PREFIX = "INT_" ;
78
79
79
- /**
80
- * Default value for the time-to-live property.
81
- */
82
- public static final Duration DEFAULT_TTL = Duration .ofSeconds (10 );
83
-
84
80
private final String id ;
85
81
86
82
private final JdbcTemplate template ;
87
83
88
84
private final AtomicBoolean started = new AtomicBoolean ();
89
85
90
- private Duration ttl = DEFAULT_TTL ;
91
-
92
86
private String prefix = DEFAULT_TABLE_PREFIX ;
93
87
94
88
private String region = "DEFAULT" ;
@@ -100,7 +94,7 @@ public class DefaultLockRepository
100
94
101
95
private String deleteExpiredQuery = """
102
96
DELETE FROM %sLOCK
103
- WHERE REGION=? AND CREATED_DATE <?
97
+ WHERE REGION=? AND EXPIRED_AFTER <?
104
98
""" ;
105
99
106
100
private String deleteAllQuery = """
@@ -110,24 +104,24 @@ public class DefaultLockRepository
110
104
111
105
private String updateQuery = """
112
106
UPDATE %sLOCK
113
- SET CLIENT_ID=?, CREATED_DATE =?
114
- WHERE REGION=? AND LOCK_KEY=? AND (CLIENT_ID=? OR CREATED_DATE <?)
107
+ SET CLIENT_ID=?, EXPIRED_AFTER =?
108
+ WHERE REGION=? AND LOCK_KEY=? AND (CLIENT_ID=? OR EXPIRED_AFTER <?)
115
109
""" ;
116
110
117
111
private String insertQuery = """
118
- INSERT INTO %sLOCK (REGION, LOCK_KEY, CLIENT_ID, CREATED_DATE )
112
+ INSERT INTO %sLOCK (REGION, LOCK_KEY, CLIENT_ID, EXPIRED_AFTER )
119
113
VALUES (?, ?, ?, ?)
120
114
""" ;
121
115
122
116
private String countQuery = """
123
117
SELECT COUNT(REGION)
124
118
FROM %sLOCK
125
- WHERE REGION=? AND LOCK_KEY=? AND CLIENT_ID=? AND CREATED_DATE >=?
119
+ WHERE REGION=? AND LOCK_KEY=? AND CLIENT_ID=? AND EXPIRED_AFTER >=?
126
120
""" ;
127
121
128
122
private String renewQuery = """
129
123
UPDATE %sLOCK
130
- SET CREATED_DATE =?
124
+ SET EXPIRED_AFTER =?
131
125
WHERE REGION=? AND LOCK_KEY=? AND CLIENT_ID=?
132
126
""" ;
133
127
@@ -188,14 +182,6 @@ public void setPrefix(String prefix) {
188
182
this .prefix = prefix ;
189
183
}
190
184
191
- /**
192
- * Specify the time (in milliseconds) to expire deadlocks.
193
- * @param timeToLive the time to expire deadlocks.
194
- */
195
- public void setTimeToLive (int timeToLive ) {
196
- this .ttl = Duration .ofMillis (timeToLive );
197
- }
198
-
199
185
/**
200
186
* Set a {@link PlatformTransactionManager} for operations.
201
187
* Otherwise, a primary {@link PlatformTransactionManager} bean is obtained
@@ -219,8 +205,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
219
205
* <pre class="code">
220
206
* {@code
221
207
* UPDATE %sLOCK
222
- * SET CLIENT_ID=?, CREATED_DATE =?
223
- * WHERE REGION=? AND LOCK_KEY=? AND (CLIENT_ID=? OR CREATED_DATE <?)
208
+ * SET CLIENT_ID=?, EXPIRED_AFTER =?
209
+ * WHERE REGION=? AND LOCK_KEY=? AND (CLIENT_ID=? OR EXPIRED_AFTER <?)
224
210
* }
225
211
* </pre>
226
212
* @param updateQuery the query to update a lock record.
@@ -247,7 +233,7 @@ public String getUpdateQuery() {
247
233
* Set a custom {@code INSERT} query for a lock record.
248
234
* The {@link #getInsertQuery()} can be used as a template for customization.
249
235
* The default query is
250
- * {@code INSERT INTO %sLOCK (REGION, LOCK_KEY, CLIENT_ID, CREATED_DATE ) VALUES (?, ?, ?, ?)}.
236
+ * {@code INSERT INTO %sLOCK (REGION, LOCK_KEY, CLIENT_ID, EXPIRED_AFTER ) VALUES (?, ?, ?, ?)}.
251
237
* For example a PostgreSQL {@code ON CONFLICT DO NOTHING} hint can be provided like this:
252
238
* <pre class="code">
253
239
* {@code
@@ -281,7 +267,7 @@ public String getInsertQuery() {
281
267
* <pre class="code">
282
268
* {@code
283
269
* UPDATE %sLOCK
284
- * SET CREATED_DATE =?
270
+ * SET EXPIRED_AFTER =?
285
271
* WHERE REGION=? AND LOCK_KEY=? AND CLIENT_ID=?
286
272
* }
287
273
* </pre>
@@ -389,23 +375,23 @@ public void close() {
389
375
}
390
376
391
377
@ Override
392
- public void delete (String lock ) {
393
- this .defaultTransactionTemplate .executeWithoutResult (
394
- transactionStatus -> this .template .update (this .deleteQuery , this .region , lock , this .id ));
378
+ public boolean delete (String lock ) {
379
+ return this .defaultTransactionTemplate .execute (
380
+ transactionStatus -> this .template .update (this .deleteQuery , this .region , lock , this .id )) > 0 ;
395
381
}
396
382
397
383
@ Override
398
- public boolean acquire (String lock ) {
384
+ public boolean acquire (String lock , Duration ttlDuration ) {
399
385
Boolean result =
400
386
this .readCommittedTransactionTemplate .execute (
401
387
transactionStatus -> {
402
- if (this .template .update (this .updateQuery , this .id , epochMillis ( ),
403
- this .region , lock , this .id , ttlEpochMillis ()) > 0 ) {
388
+ if (this .template .update (this .updateQuery , this .id , ttlEpochMillis ( ttlDuration ),
389
+ this .region , lock , this .id , epochMillis ()) > 0 ) {
404
390
return true ;
405
391
}
406
392
try {
407
393
return this .template .update (this .insertQuery , this .region , lock , this .id ,
408
- epochMillis ( )) > 0 ;
394
+ ttlEpochMillis ( ttlDuration )) > 0 ;
409
395
}
410
396
catch (DataIntegrityViolationException ex ) {
411
397
return false ;
@@ -420,27 +406,27 @@ public boolean isAcquired(String lock) {
420
406
transactionStatus ->
421
407
Integer .valueOf (1 ).equals (
422
408
this .template .queryForObject (this .countQuery ,
423
- Integer .class , this .region , lock , this .id , ttlEpochMillis ())));
409
+ Integer .class , this .region , lock , this .id , epochMillis ())));
424
410
return Boolean .TRUE .equals (result );
425
411
}
426
412
427
413
@ Override
428
414
public void deleteExpired () {
429
415
this .defaultTransactionTemplate .executeWithoutResult (
430
416
transactionStatus ->
431
- this .template .update (this .deleteExpiredQuery , this .region , ttlEpochMillis ()));
417
+ this .template .update (this .deleteExpiredQuery , this .region , epochMillis ()));
432
418
}
433
419
434
420
@ Override
435
- public boolean renew (String lock ) {
421
+ public boolean renew (String lock , Duration ttlDuration ) {
436
422
final Boolean result = this .defaultTransactionTemplate .execute (
437
423
transactionStatus ->
438
- this .template .update (this .renewQuery , epochMillis ( ), this .region , lock , this .id ) > 0 );
424
+ this .template .update (this .renewQuery , ttlEpochMillis ( ttlDuration ), this .region , lock , this .id ) > 0 );
439
425
return Boolean .TRUE .equals (result );
440
426
}
441
427
442
- private Timestamp ttlEpochMillis () {
443
- return Timestamp .valueOf (currentTime ().minus ( this . ttl ));
428
+ private Timestamp ttlEpochMillis (Duration ttl ) {
429
+ return Timestamp .valueOf (currentTime ().plus ( ttl ));
444
430
}
445
431
446
432
private static Timestamp epochMillis () {
0 commit comments