Forums Register Login

Closing/Clearing objects within a Session when browser closes

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1258 times.
Similar Threads
Session Management--Telling objects to go away!
Browser close session destroy
session object
How to Close the Response Object at ServerSide?
Do stuffs when browser closes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:16:30.