I am implementing a locking service in a distributed system using Mysql GET_LOCK. Upon calling my getLock() method, if a lock is obtained by a client, I make an entry to DB and delete the entry when lock is released.
Assumption is calling client would release the lock once its purpose is served. However, I would like to ensure that lock gets released in case client does not release it or doesn't do proper cleanup.
One way would be to use finalize method on my lock object to release it when finalize is called. However it's not ideal, adds complications and is deprecated in Java 9. I read about Phantom references which are better than finalizers, but its complexity level is also high. I am keeping it as my last resort.
Is there any simpler, less JVM dependent way to handle this usecase?