This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Closing/Clearing objects within a Session when browser closes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Closing/Clearing objects within a Session when browser closes" Watch "Closing/Clearing objects within a Session when browser closes" New topic
Author

Closing/Clearing objects within a Session when browser closes

Arpit Purohit
Greenhorn

Joined: Jan 09, 2007
Posts: 21
Hi,

I have Object added to a Session which makes connection to another server(like Database-Connection or Socket).
I use to close it explicitly when user logs out. But How to do this when user close the browser?

Do the Session Invalidating due to session timeout in turn destroys all the objects it holds?
And if the answer is Yes then what about the connection established by object with another server?
How that connection can to be closed explicitly in such condition?

Regards
Arpit Purohit
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Arpit Purohit wrote:
I have Object added to a Session which makes connection to another server(like Database-Connection or Socket).

Ouch, that's really a bad practice. Never do that. You should acquire AND close expensive JDBC resources as soon as possible and in the shortest possible scope. To improve performance you can make use of connection pooling, but still then, you should acquire and close them in the shortest possible scope.


I use to close it explicitly when user logs out. But How to do this when user close the browser?

Do the Session Invalidating due to session timeout in turn destroys all the objects it holds?
And if the answer is Yes then what about the connection established by object with another server?
How that connection can to be closed explicitly in such condition?

Regards
Arpit Purohit

Although the answer is indeed Yes, when the session expires, it will trash all bound objects. But in this case it is irrelevant, you shouldn't even think about storing expensive resources in a session. It makes your application unreliable as it may crash at unpredicted moments due to a connection timeout.
Arpit Purohit
Greenhorn

Joined: Jan 09, 2007
Posts: 21
JDBC Connection is Just an example. It can be any kind of Connection.

Lets talk about my specific problem:

I am trying to build a web based Telnet client. I create a TelnetCient (org.apache.commons.net.telnet.TelnetClient) object
to establish connection to remote server.
Further, I store this object in Session so that it can be used for next command user wants to execute (without logging in again).
In this scenario, for every user there will be a TelnetCient object which connects to remote server with user specific credentials.

I will disconnect this TelneClient explicitly when user logs out. But what to do if user close the browser without logging out.

Regards
Arpit Purohit
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The session will timeout sooner or later. By default after 30 mins of inactivity. This is configureable in web.xml. When the sesson times out, it will be invalidated and destroyed. All of its attributes will be unbound. If not referenced by other classes, they will just be GC'ed.

Still then, you shouldn't keep expensive resources open that long. That's simply receipt for trouble. Mark my words.
 
jQuery in Action, 2nd edition
 
subject: Closing/Clearing objects within a Session when browser closes