This week's book giveaways are in the Refactoring and Agile forums. We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line! See this thread and this one for details.
But I think what you really need is a session listener, which listens for the timeout to occur. When timeout occurs and session is going to be destroyed then this listener will be notified and you can update the database using it.