-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to correctly deal with Hibernate session?
PostPosted: Tue Feb 24, 2004 11:32 pm 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
hi experts,

I have two ways to deal with Hibernate session,
1) To close session before ending each request-response cycle, and open new session before starting the same cycle.

2) Just to disconnect() and clear() session before ending each request-response cycle, and reconnect() session before start the same cycle.

Because I still have to reuse the entities and these entities may have lazing loading collections. So I keep them in Visit. Does #2 is more appropriate to match my requirement? Or another way to correctly handle my requirement?

Please advise, thank you all very much.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 3:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
- if you need only one request to do your business --> reconnect a session and "put" it on the threadLocal (many topics deal with it), no need to open/close a session (this is more expensive), a servlet filter is ok to magage it
- if you need more than one request, i advise to use a cache system (jcs is bad, EHcache is ok and pretty simple to use) and the threadlocal strategy.
- if you don't want to use the cache, you may use the HTTPSession to store you hibernateSession, but this strategy is more complexe...

My team use the 3 method and the third was the most complexe to code in our framework

Good luck


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 6:13 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/Documentation/ ... ctionScope

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 7:26 am 
Beginner
Beginner

Joined: Sat Jan 31, 2004 10:09 am
Posts: 26
christian wrote:
http://www.hibernate.org/Documentation/SessionAndTransactionScope


Thank you very much, I ever read this article, and followed the session-per-application-transaction pattern.
But I got into the following Exception:
Code:
Caused by: net.sf.hibernate.HibernateException: [b]You may not dereference a collection with cascade="all-delete-orphan"[/b]
net.sf.hibernate.impl.SessionImpl.updateUnreachableCollection(SessionImpl.java:2885)
net.sf.hibernate.impl.SessionImpl.flushCollections(SessionImpl.java:2754)
net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2225)
net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1769)
net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1536)
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1501)
net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
hibernate.SessionUtil.executeQuery(SessionUtil.java:107)


How this exception occured:
1) The entities relationship is Order(one) -- OrderItems(many)
2) I query out a list of Order(s) in the first request/response cycle.
3) From Order(s) query result, result entities stored in HttpSession, I pick one record and launch secend request/response cycle to its items detail.
4) Then the exception is occured.

Below is my Hibernate session controller source, please note that session is not closed. And it will be much appreciated it if you indicate the problem is:
Code:
public class SessionController implements Filter {

   private static ThreadLocal hSessionHolder = new ThreadLocal();
   private static SessionFactory sessionFactory;
   
   static {
      
      Configuration cfg;
      try {
         cfg = new Configuration().configure();
         sessionFactory = cfg.buildSessionFactory();
      }
      catch (HibernateException e) {
         throw new RuntimeException("Fail to configure hibernate", e);
      }
   }
   
   public static Session getSession() {
      
      Session s = (Session) hSessionHolder.get();
      if (s == null || !s.isOpen()) {
         s = initSession();
         hSessionHolder.set(s);
      }
      return s;
   }
   
   public void init(FilterConfig arg0) throws ServletException {
      
      // empty
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      
     final String HSESSION = "hibernate.session";
    
     HttpSession httpSession = ((HttpServletRequest)request).getSession();
     Session session = (Session) httpSession.getAttribute(HSESSION);
      try {
         try {
           if (session == null) {
             session = initSession();
             httpSession.setAttribute(HSESSION, session);
           }
           else {
             if (!session.isConnected())
               session.reconnect();
           }
          
           hSessionHolder.set(session);

            chain.doFilter(request, response);
         }
         finally {
           session = (Session) hSessionHolder.get();
           hSessionHolder.set(null);
           if (session != null) {
             //session.clear();
               session.disconnect();
           }
         }
      }
      catch (HibernateException e) {
        e.printStackTrace();
         throw new RuntimeException("Fail to close hibernate session", e);
      }
   }

   public void destroy() {
      //empty
   }
   
   private static Session initSession() {
      
      Session session;
      try {
         session = sessionFactory.openSession();
         return session;
      }
      catch (HibernateException e) {
        e.printStackTrace();
         throw new RuntimeException("Fail to open hibernate session.");
      }
      
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 7:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you give us the code of your app?
it will be easier


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 8:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I think you do

Code:
parent = s.load(PArent.class, id);
s.close();
parent.setDeleteOrphanChildren(new HashSet());
openSession();
s.saveOrUpdate(parent);

This is not allowed, Hibernate keep state of orphan to delete in Hibernate Set implementation.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.