The moose likes Servlets and the fly likes Insert session data into a db at end of session Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Insert session data into a db at end of session" Watch "Insert session data into a db at end of session" New topic
Author

Insert session data into a db at end of session

Brian E
Greenhorn

Joined: Sep 07, 2001
Posts: 12
I'm storing session data in my application. I need to know how I can insert that data into a db at the end of a session -- both by explicitly killing the session (ie -- logout) or by session timeout.
I am running Tomcat 3.2.2 on Apache w/ Oracle 8.1.7
Thanks for your help.
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
implement the interface javax.servlet.http.HttpSessionBindingListener to bind all ur values to session. When the sesison times out or when it is invalidated (on clicking a 'logout' button, invalidate the session), it invokes sessionUnbound() on all the listeners. U can put in code for insertion into database in sessionUnbound() method then.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It might make more sense to bind a single listener and have it be responsible for persisting all the session data (as opposed to having each data object be responsible for persisting itself).
Brian E
Greenhorn

Joined: Sep 07, 2001
Posts: 12
Thanks for the replies.
I'm still having a bit of a time trying to figure this out. Could someone reply with some sample code of how I might do this?
Thanks.
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
If the object that u store in session is SessionBoundObject, when this obj is added to session by using :
session.putValue("name", obj);
it executes the valueBound() method.
And when session is destroyed, the valueUnbound method is called.
So whatever u need to do before destroying this object (like insert into database) should be done in the valueUnbound() method.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Insert session data into a db at end of session
 
Similar Threads
Regarding JSP-EJB Communication
What's the best way to connect to DB
Beginner trying to understand stale data
Exception in Junit
Regarding JSP-EJB Communication