-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Guarantee SessionFactory doesn't re-use Sessions
PostPosted: Sat Apr 08, 2006 1:37 am 
Newbie

Joined: Fri Feb 24, 2006 12:50 pm
Posts: 3
Location: Helena, MT
Hibernate version: 3


Name and version of the database you are using: Oracle 10g-RAC


I'm using one-user, one-connection to the Oracle DB, and went down the path of starting a single (Long) Hibernate Session (and DB connection) at login and retain it throughout the user's use of the application. (Struts/JSP web-app)

Works fine with a single user, but during system testing we found problems with more than one concurrent user.

The SessionFactory is re-assigning existing HibernateSessions, which results in numerous problems like duplicate-collections, flushed/evicted collections, etc. when two users share a Hibernate Session and perform similar (or incompatible) tasks.

If everyone has their own Hibernate Session, all is well. It seems if someone is actively using their session (performing a task), the Factory assigns any new users a different Session.

Allow your Session to sit idle for a minute, and it might get "borrowed" by the next person to log in.

At this point, I think I need to dig up the source code for HibernateSessionFactory and modify it to never re-use a Session.

Or is there a simpler way to guarantee the SessionFactory returns a given Session once, and only once?

I'm already managing that the user gets and keeps his/her HibSession throughout their use of the application - I just didn't expect two users to be given the same Session object.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 5:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
huh ? SessionFactory *never* returns the same session twice.

(currentSession can do that though, but that is the whole point of that method)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 1:22 pm 
Newbie

Joined: Fri Feb 24, 2006 12:50 pm
Posts: 3
Location: Helena, MT
OK, the DAOs, etc. all call my HibernateSessionFactory.currentSession()

currentSession() looks like this:

Code:
public static Session currentSession() throws HibernateException {
           Session session = (Session) threadLocal.get();
           if (session == null || session.isOpen() == false) {
               if (sessionFactory == null) {
                   try {
                       cfg.configure(CONFIG_FILE_LOCATION);
                       sessionFactory = cfg.buildSessionFactory();
                   }
                   catch (Exception e) {
                       System.err.println("%%%% Error Creating SessionFactory %%%%");
                       e.printStackTrace();
                   }
               }
               session = sessionFactory.openSession();
               threadLocal.set(session);
           }
           return session;
       }


Before any DAOs make an attempt to call "currentSession", I pull the Hibernate Session from the HTTP Session and force the threadLocal referenced above to be the "existing HibSession fetched from the HttpSession"

Thus, the currentSession() reads: "if the threadlocal wasn't already set, retrieve a new hibSession object"

Typically, that only happens at login, when the user doesn't have a Hibernate Session yet.

My problem is that it frequently winds up assigning the same Hibernate Session to TWO (or a dozen) User/HttpSessions.

Then I wind up with "two collections with same identifier already in Session" and "object was already evicted" or "already flushed" problems from two people playing in the same sandbox.

I'll double check and put the session.hashCode out to the log file anytime "openSession" is called and see if it IS returning a different one each time and somewhere I'm fouling it up.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:29 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Just do not store hibernate session in http session, it can not work on heavy load.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 4:35 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
BTW it looks like you do not clear thread local and it is reused for next user.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.