The following code can reproduce the issue
https://github.com/cuipengfei/Spikes/blob/master/jpa/spring-jdbc-distributed-lock-issue/
run the test cases in the above code to reproduce
before running the test case, start pg db in docker:
docker run -e POSTGRES_USER=localtest -e POSTGRES_PASSWORD=localtest -e POSTGRES_DB=orders -p 5432:5432 -d postgres:9.6.12
when running 2 workers in 2 separate java processes, TTL works as expected ↑
when running both workers in one java process, TTL has no effect ↑
Aside from the above code, the real issue I was facing is like this:
request 1 hits server 1, while handling the request, the thread sometimes hangs(due to a weird issue of a 3rd party jar which we can not replace) when that hanging happens, our code won't have a chance to release the lock
then request 2 comes, if it hits other servers, then it's fine. but if it hits server 1, then request 2 won't be able to get the lock since the hanging thread never released the lock and TTL does not help in this case.
In summary: In one java process, a thread gets the lock but somehow due to bugs or whatever reason it did not get a chance to release the lock. Then subsequent threads in this java process won't be able to obtain the same lock and can not proceed with unfinished job. In this case, are there any recommended ways to allow the subsequent threads to be able to get the lock?