Skip to content

Commit cb5d968

Browse files
Support lock renew in jdbc lockregistry
1 parent 3e63fe4 commit cb5d968

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,13 @@
1616

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

19+
import org.springframework.dao.CannotAcquireLockException;
20+
import org.springframework.dao.DataAccessResourceFailureException;
21+
import org.springframework.dao.TransientDataAccessException;
22+
import org.springframework.integration.support.locks.ExpirableLockRegistry;
23+
import org.springframework.integration.util.UUIDConverter;
24+
import org.springframework.util.Assert;
25+
1926
import java.time.Duration;
2027
import java.util.Iterator;
2128
import java.util.Map;
@@ -26,15 +33,7 @@
2633
import java.util.concurrent.locks.Lock;
2734
import java.util.concurrent.locks.ReentrantLock;
2835

29-
import org.springframework.dao.CannotAcquireLockException;
30-
import org.springframework.dao.DataAccessResourceFailureException;
31-
import org.springframework.dao.TransientDataAccessException;
32-
import org.springframework.integration.support.locks.ExpirableLockRegistry;
33-
import org.springframework.integration.util.UUIDConverter;
34-
import org.springframework.util.Assert;
35-
3636
/**
37-
*
3837
* An {@link ExpirableLockRegistry} using a shared database to co-ordinate the locks.
3938
* Provides the same semantics as the
4039
* {@link org.springframework.integration.support.locks.DefaultLockRegistry}, but the
@@ -47,7 +46,6 @@
4746
* @author Kai Zimmermann
4847
* @author Bartosz Rempuszewski
4948
* @author Gary Russell
50-
*
5149
* @since 4.3
5250
*/
5351
public class JdbcLockRegistry implements ExpirableLockRegistry {
@@ -99,6 +97,20 @@ public void expireUnusedOlderThan(long age) {
9997
}
10098
}
10199

100+
public void renewLock(Lock lock) {
101+
Assert.isInstanceOf(JdbcLock.class, lock);
102+
JdbcLock jdbcLock = (JdbcLock) lock;
103+
104+
if (!jdbcLock.isAcquiredInThisProcess()) {
105+
throw new IllegalMonitorStateException("You do not own mutex at " + jdbcLock.path);
106+
}
107+
108+
if (!jdbcLock.mutex.acquire(jdbcLock.path)) {
109+
throw new IllegalStateException("Could not renew mutex at " + jdbcLock.path);
110+
}
111+
112+
}
113+
102114
private static final class JdbcLock implements Lock {
103115

104116
private final LockRepository mutex;

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
import org.junit.Before;
2929
import org.junit.Test;
30+
import org.junit.jupiter.api.Assertions;
3031
import org.junit.runner.RunWith;
3132

3233
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +41,6 @@
4041
/**
4142
* @author Dave Syer
4243
* @author Artem Bilan
43-
*
4444
* @since 4.3
4545
*/
4646
@ContextConfiguration
@@ -266,4 +266,20 @@ public void testTwoThreadsWrongOneUnlocks() throws Exception {
266266
assertThat(((Exception) imse).getMessage()).contains("You do not own");
267267
}
268268

269+
@Test
270+
public void testLockRenew() throws Exception {
271+
final Lock lock = this.registry.obtain("foo");
272+
273+
assertThat(lock.tryLock()).isTrue();
274+
registry.renewLock(lock);
275+
}
276+
277+
@Test
278+
public void testLockRenewLockNotOwned() {
279+
final Lock lock = this.registry.obtain("foo");
280+
281+
Assertions.assertThrows(IllegalMonitorStateException.class, () -> registry.renewLock(lock));
282+
}
283+
284+
269285
}

0 commit comments

Comments
 (0)