Let's see if I can explain this effectively... (and I hope I am posting in the right forum)
Problem I'm trying to solve: When a user logs into a J2EE based application, a record is inserted into a user session table in the database. The 'logout' column at this point is of course null. When the user logs out or when their session times out, code is in place to insert the proper 'logout' value into the 'logout' column. But when the server shuts down, any user that is currently logged in and has not logged out or timed out yet, does not have their respective 'logout' column populated.
Attempt one: I created an object which implemented ServletContextListener. In the contextInitialized method, I created a synchronized Map. (which will hold references to authenticated and active session objects keyed by a hacked session id - use of the map allows a few other requirements to be solved so ignore that for now) When a user logs in and is authenticated, their session is added to the map. When a user logs out, their session is removed from the map. When their session times out, their session is removed from the map. So I then tried to add code to the contextDestroyed method which would get the IDs for the remaining active user sessions and update their respective records in the database and ultimately populate the 'logout' column for each one.
Sounded like it would work!?!?
So I start up the application, log a couple users in and then 'shutdown' the server. (in this case, JBoss) The result is that I get an exception "javax.naming.NameNotFoundException: null not bound" when trying to get a database connection! (dang it) So it looks like I cannot connect to the database at this point. (JNDI bindings have already gone bye bye?)
Is there a listener method that is higher up in the shutdown sequence that I can override prior to anything 'going bye bye' on me behind the scenes?
I�m no jboss expert, but I know that containers like weblogic have two ways to shutdown a server instance: graceful shutdown or forced shutdown. The difference between the two is that a forced shutdown is immediate. In my opinion your design has very slim chances to succeed if the server initiates a forced shutdown sequence, because it doesn�t wait for any client initiated work to complete. The graceful one in turn will allow to certain application to complete their processing work. It will allow for example to all web session to either complete or expire and won�t accept any new session creation requests. However the rule that governs the way the sessions are terminated varies upon the type of session persistence: in memory replication, database persistence, etc. Bottom line is that following this line you might not end up designing the most flexible or simplest solution, but you might have it working. In my opinion the simplest way to do this is to use declarative security and let the server handle the session information. You can also customize the way log messages are written and you should be able to redirect them to a database if you whish. Of course this might be more than a headache if you already use custom solutions for handling the security. Besides that, you should post your question to the jboss forum and probably some jboss experts will offer you a better strategy. Regards.