views:

505

answers:

2

Hi, 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.

A: 

Please do as

  1. Use your connection and other database classes to remove the DB resources.
  2. Clear you session using session object.
  3. Check for the cookie you have created in application and remove required one or all.
Umesh Aawte
I am closing DB resources individually in all the EJBs and DAOs. I wish to know if there is some mechanism to close all open DB resources at one place for a particular user session.
Ravi Gupta
Then you have to study the DB connection pool in Java. I am not aware about EJB.
Umesh Aawte
+1  A: 

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.

BalusC
I do close the DB resources in the same block where I am opening and I am using Weblogic connection pooling. My real problem is that some queries take long time to execute and once the user logs out its a possibility that those queries are still running and consuming system resources. I am looking for a way to terminate those connections after user logs out.
Ravi Gupta
Store a handle in the session scope and use `HttpSessionListener#sessionDestroyed()` to get them back and stop them all.
BalusC