• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpSession Object Persist Across Restart

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesnt that call for a ServletContextAttributeListener or ServletContextListener?
Bill
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic