Using JdbcLockRegistry from different threads to lock and unlock based on lockKey

24 views Asked by At

We are using the Temporal framework to run several workflows. Due to the design of the application I cannot return Lock instance to the main method which starts all the workflows and hence I am relying on JdbcLockRegistry and same lock_keys.

Now this is how my DatabaseLockManager.java looks like,

public class DatabaseLockManager {

private final JdbcLockRegistry lockRegistry;

@Autowired
public DatabaseLockManager(JdbcLockRegistry lockRegistry){
  this.lockRegistry = lockRegistry;
}

public void acquire(String lockKey){
  jdbcLockRegistry.obtain(lockKey).lock();
}

public void release(String lockKey){
  jdbcLockRegistry.obtain(lockKey).unlock();
}

My Parent workflow calls the acquire method and release methods independently. I can run it successfully locally but when running over GCP, during the release method call, I am running into the following exception

"Mutex is not held by the current Thread"

Fix1 Now I tried using ReneableLocks but the output remains the same. I can see the entry against the lock_key in the database during the acquire method call and after a while, it will disappear as it is more like a lease but it is not due to my release method call.

I cannot use the traditional locking/unlocking here, because the release method has to be called after all the workflows are done which is managed by Temporal. Also, my microservice which is responsible for acquiring the releasing locks is different from the one that initiates workflows. Since Locks are not serializable I cannot even pass them from the first microservice to another.

Fix2 Also the TTL for lockRegistry is set to 30 minutes and my operation finishes in 5 mins.

Fix3 I cannot use Semaphores because there is another application that will use the same lockKey to fetch the locks on the same entity objects. My microservice is contending with this other application.

0

There are 0 answers