File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes HttpSession Object Persist Across Restart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "HttpSession Object Persist Across Restart" Watch "HttpSession Object Persist Across Restart" New topic
Author

HttpSession Object Persist Across Restart

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
In Tomcat 5, if I stop and restart an application using the manager appl, the HttpSession objects specific to that application is still there and can still be retrieved by request.getSession(). Is there any way to disable this feature, i.e. forcing all session objects be removed without restarting tomcat?

Also, if objects can persist across restart, is there anyway to persist our own arbitrary objects across restart, too?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Arbitrary objects that are Serializable will be persisted as part of the session. Watch out for storing objects with complex links or your serialized sessions will get huge.

Bill


Java Resources at www.wbrogden.com
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
My objects aren't serializable. It is a context attribute referencing the HttpSession objects. Since tomcat is keeping HttpSession objects, would there be a way to keep other objects referencing the HttpSession objects?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

Tomcat persists the data referred to by the session object. It can't do fancy garbage-collection things like finding out what other objects refer to that data, no Java code can do that. If you want other data persisted then you have a couple of choices:

1. Put it into the session and let Tomcat persist it for you.

2. Design and implement your own persistence mechanism.
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
My problem is that I maintain a Collection of all HttpSession objects created so far and I want that Collection persisted.

I have a HttpSessionListener that add any session object created to a Collection stored in ServletContext (and remove from it when the session is destroyed). In this way, I can have an admin servlet that can access all the current active sessions. But, when I restart tomcat, my Collection in the ServletContext is lost while the session objects are still there. That cause and inconsistent between my Collection and the actual existing sessions. The HttpSessionListener doesnt seem executed when the web appli restart.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Doesnt that call for a ServletContextAttributeListener or ServletContextListener?
Bill
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Still, the ServletContextAttributeListener or ServletContextListener need a
place to store the Collection. But the point is my Collection is maintaining references to the session objects, and so it is not serializable! It is just removed together with the servlet context.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
I think you need to step back from the problem a bit.

Here is what I do with user data that represents the current state of taking a mock exam.
All data needed to recreate the user's state is stored in a serialized object that is persisted to disk with a unique name. That name is stored in the user's session while taking the test and is also displayed to the user so they can copy it. The object is serialized to disk with every significant change and there is also a in memory collection that holds it.
During an active session, the in-memory collection provides the current state, grabbing the unique name from the session or from a hidden form variable.
When a user resumes a test, he enters the unique name at the start and the state is restored. Therefore I don't care what the server does with a session.

Bill
Eric B.
Greenhorn

Joined: May 31, 2006
Posts: 2
I'm encountering the same issue Alec is encountering. Alec - did you manage to come up with a solution that worked for you?

Thanks,

Eric
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Eric,

If this is what you want to do -

Originally posted by Alec Lee:
Is there any way to disable this feature, i.e. forcing all session objects be removed without restarting tomcat?


Have a look at how to configure the context to do this.
Quoted from http://tomcat.apache.org/tomcat-5.0-doc/config/manager.html


pathname -

Absolute or relative (to the work directory for this Context) pathname of the file in which session state will be preserved across application restarts, if possible. The default is "SESSIONS.ser". See Restart Persistence for more information. Restart persistence may be disabled by setting this attribute to an empty string.


HTH.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

"Eric B.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSession Object Persist Across Restart
 
Similar Threads
Tomcat.5.0.28 - how to disable session persistence
When Classes are unloaded from Tomcat
Storing Javascript object and reloading it
Need to store non serialized object in session as attributes
HttpSession lifetime and storage