aspose file tools*
The moose likes Servlets and the fly likes How to detect unused/Inactive sessions from code and remove it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to detect unused/Inactive sessions from code and remove it" Watch "How to detect unused/Inactive sessions from code and remove it" New topic
Author

How to detect unused/Inactive sessions from code and remove it

jasheer kandath
Greenhorn

Joined: Feb 27, 2010
Posts: 10

How can we detect unused/inactive sessions (probably from a browser crash / Not logging out ) and remove those sessions. ? I am aware of the session time out feature, apart from that , do we have any better options ?


SCJP 1.6
jitu mishra
Greenhorn

Joined: Aug 19, 2011
Posts: 5
Ya you can go to your browser and press CTRL+SHIFT+DELETE


And you are asking about the session timeout ya you can configure through web.xml
jasheer kandath
Greenhorn

Joined: Feb 27, 2010
Posts: 10

Oh.. I think my question is confusing let me rephrase it " How do we detect the inactive sessions inside code and remove it to save memory ? "
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Let me guess... you want to run some code on the server which finds out which sessions are "inactive".

As you already said, the timeout period is the tool for cleaning up unused sessions. There is no way to find out, from the server, what the user of a session is doing now. They might be talking to a co-worker or checking Facebook or they might have turned off their computer, but you can't tell. That's why the timeout is there.
jasheer kandath
Greenhorn

Joined: Feb 27, 2010
Posts: 10

If the timeout is kept at some 5 minutes , and in an application which gets used by millions of users, there could be large of number of inactive/unused sessions (cases where the user might not come back to the session , like he closed the browser and went for a movie ). How can we prevent these kind of sessions getting piled up?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

You can't. How could you tell the difference between someone who's reading information on the screen, from someone who's gone to the bathroom, from someone who's gone to party likes it's 1999?

The session timeout is the right tool for the job. Why are you against using it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Well, as you already know, if you choose a 5-minute timeout period then the session is going to exist in your server until 5 minutes has passed without any request from the session's client. So what exactly is your question?

jasheer kandath
Greenhorn

Joined: Feb 27, 2010
Posts: 10

I Apologize for the repeated questions on the same topic, Actually i got this question on an interview and I replied with the mighty answer of session time out tool, but the "Guy" keep telling me there are other better ways .. His point was that starting from the 1st minute to the end of 4th minute there could be lots of unused sessions which might take lots of memory and slow down the application , so the question was to run something like a session cleanup code after 2.5 minutes and balance the memory usage. Any way Paul , bear thanks for the replies and accepted the session time out solution.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

2.5 minutes is way too short a time to dump a user. I vote that the interviewer was less than savvy.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

Oh, it was an interview question. Not a real-life question at all then.

I suppose if you did have this problem in real life, you could do something like randomly terminating sessions before they actually timed out. The majority of users wouldn't know whether the timeout period was 5 minutes or 4 minutes anyway, and they wouldn't have been keeping track in most cases. And for those who did want to complain, you would provide one of those phone services where they told the user "Your call is important to us" and then put them on hold for 25 minutes.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Maybe a long polling mechanism ( like comet ) can be used? You can check if the client is still there, or if any events in the browser itself are triggered...Of course you will have to use a lot of script, depending how much you want to check...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Polling can't be used as it defeats the entire purpose of this (rather silly) exercise. If the original question is centered around keeping the load on the server to a minimum, a highly expensive technique like Comet will make the situation far far worse.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

Maybe you could store less in the session to save memory? Or save nothing in the session (stateless website.) ?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to detect unused/Inactive sessions from code and remove it