-->
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: commit closes my session
PostPosted: Mon Jul 11, 2011 8:26 am 
Newbie

Joined: Mon Jul 11, 2011 8:14 am
Posts: 3
Hello everyone.

I have some java code and the commit closes automaticly the session.
do I do some thing wrong
i always give error => connection already closed
or how can i make the code better ( just learning java)
Thx for helping me
Bert


Code:
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = null;
       try {
           tx = session.beginTransaction();
           Bestellingt tmpa = new Bestellingt();
           tmpa.setBesteldate(new Date());
           Producten tmpProd = new ProductHelper().getProdById(10);
           session.saveOrUpdate(tmpa);
              for (int i = 0; i < 5; i++) {
                 Bestellijnt bestellijntsT = new Bestellijnt(tmpa, tmpProd, i, tmpProd.getProdprijs(), new Date());
                 session.saveOrUpdate(bestellijntsT);
              }
              System.out.println("voor commit:" + session.isConnected());=> here is the session open
              tx.commit();
              System.out.println("voor commit:" + session.isConnected());=> here is the session closed
      } catch (Exception e) {
         System.out.println("1 try:" +e.getStackTrace());
      }
      finally{
         try {
            session.close();
         } catch (Exception e) {
            // TODO: handle exception
            System.out.println("2 try:" + e.getStackTrace());
         }
         
         
      }


Top
 Profile  
 
 Post subject: Re: commit closes my session
PostPosted: Tue Jul 12, 2011 4:24 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
That is a configuration setting. By default, Hibernate closes a session on commit.

This is actually best practice. The Hibernate books write about long-running sessions with multiple commits, even sessions that live through the entire lifetime of an application, but I found that this doesn't work in practice: the first exception thrown by the database layer invalidates the Session object, so you need to be able to restart a session anyway. It's far easier to design the application around short-lived transactions=sessions than to implement a Session recovery mechanism.


Top
 Profile  
 
 Post subject: Re: commit closes my session
PostPosted: Tue Jul 12, 2011 4:43 am 
Newbie

Joined: Mon Jul 11, 2011 8:14 am
Posts: 3
thanks, for the clear and fast explanation.
one last question though, why do you see with all the examples session close behind the transaction commit.
If it is best practice, the commit closes the session, and you don't have to do it manually( it even throws an exception).
Greetings Bert


Top
 Profile  
 
 Post subject: Re: commit closes my session
PostPosted: Tue Jul 12, 2011 4:51 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
No idea why examples are that way.
Maybe people usually start out implementing long-running sessions, then find it doesn't work as soon as real-world databases start to throw exceptions, then don't go back and set autoclose back on.
Maybe different session factories behave differently. I'm using JDBCTransactionFactory, which is a bit unusual since it's for standalone J2SE, and maybe it preconfigures sessions differently than what most people get inside a J2EE container. My experience with Hibernate factory configuration variants found in the wild is limited.


Top
 Profile  
 
 Post subject: Re: commit closes my session
PostPosted: Tue Jul 12, 2011 5:21 am 
Newbie

Joined: Mon Jul 11, 2011 8:14 am
Posts: 3
Ok thx
Bert


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.