I have an web-based application that allows the user exclusive access to a resource on a java server using Spring MVC framework. When the user begins using a resource, the client side web page will call a URL that instructs the server to lock the resource. Likewise, when the user stops using the resource, the client side web page will call another URL to release the lock on the resource. The server application will also unlock the resource when the user session expires (after 2 hours).
However, if the browser is closed, or terminated unexpectly, the client application will not have a chance to unlock the resource. To solve this problem, I implemented a javascript timer that periodically sends a keepalive keep the resource locked. However, sometimes the keepalive did not fire as expected, and the resource would be unlocked unexpectedly.
Is there a better way to implement this keepalive so that the resource will be unlocked automatically when the client closes the browser, or navigate to another page without using timers?