In my application I have created some locking / unlocking functionality based on the current page that the user is viewing. A user can view a page and will acquire a lock which is stored in my database, when they navigate away the lock is released and the database is updated.
I am trying to handle the situation when the user closes the browser window without logging out. They will still be holding the lock and I need that to be released and a database call needs to occur.
Is there a way to handle this in a JSF application using Spring or something else?
I know the javascript function onbeforeunload but I need to call a java method that will enable me to update the database.
The
unloadandbeforeunloadevents aren’t going to help you here since it’s not by the specification guaranteed that any ajax request which is fired within those events is ever going to successfully hit the server. More than often, this won’t.The most reliable way is to keep the server side session lifetime very short, e.g. 1 minute and introduce an ajax poller on the page which polls within the same session to the server every minute (minus a few seconds) until the user is been inactive for more than the actual maximum session timeout. The user activity can be tested by listening on JavaScript keyboard (
keyup) and mouse (click) events.