jakarta-eelogout

How to close all DB resources once the user logs out


I have J2EE app on which the logout is not implemented properly, my intention is to close all DB resources once the user logs out. Not sure if session.invalidate(); will really help, I will try it today. Please advice what would be the best to way to implement logout so that all valuable resources are released once user logs out.


Solution

  • my intention is to close all DB resources once the user logs out

    This is already a sign of a bad practice. The DB resources ought to be acquired and closed in the shortest possible scope, i.e. inside the very same method block. You may never get hold of a DB resource (Connection, Statement and/or ResultSet) as a static or instance variable. It's receipt to resource leaks and more serious trouble.

    If the original intent is to improve connecting performance, you need to introduce a connection pool. Keep in mind that this isn't an excuse to keep the connections open yourself. Just acquire and close them as soon as possible the same way, the connection pool implementation itself will worry itself about the actually closing the connection or releasing it back to the pool. It's all fully transparent. Decent application servers ships with connection pooling capabilities in flavor of JNDI datasources. Make use of it. Once done that, you don't need to worry about any open DB resources nor performance at all.