-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: HibernateException: Session is closed
PostPosted: Tue Mar 16, 2004 12:47 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
Hi,

I have 2 methods:
Code:
public OrderHeader getOrder(long orderNumber) throws DatabaseException {
   try {
      Session s = sessionFactory.openSession();
      Transaction t = s.beginTransaction();
      try{
         OrderHeader order = (OrderHeader)s.load(OrderHeader.class, new Long(orderNumber));
         s.flush();
         t.commit();
         s.close();
         return order;
      }catch(HibernateException he) {
         s.close();
         t.rollback();
         throw new DatabaseException("Trouble during order retrieve! - [" + orderNumber + "]!", he);
      }
   } catch(HibernateException he) {
      throw new DatabaseException("Unable to retrieve order [" + orderNumber + "]", he);
   }      
}

public void saveOrder(OrderHeader order) throws DatabaseException {
      try {
         Session s = sessionFactory.openSession();
         Transaction t = s.beginTransaction();
         try{
            s.save(order);
            s.flush();
            t.commit();
            s.close();
         }catch(HibernateException he) {
            s.close();
            t.rollback();
            throw new DatabaseException("Trouble during saving order! - [" + order + "]!", he);
         }
      } catch(HibernateException he) {
         throw new DatabaseException("Unable to save order [" + order + "]", he);
      }      
}


sessionFactory is defined as follow:
Code:
protected static final SessionFactory sessionFactory;


I first call saveOrder(order) and then getOrder(long) but then i get the exception.

What am i doing wrong?

Code:
net.sf.hibernate.HibernateException: Session is closed
   at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3250)
   at net.sf.hibernate.transaction.JDBCTransaction.toggleAutoCommit(JDBCTransaction.java:104)
   at net.sf.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:95)
   at com.bertrams.berteShop.dao.BookingDAO.getOrder(BookingDAO.java:46)
   at com.bertrams.berteShop.dao.TestBookingDAO.testOrderHeader(TestBookingDAO.java:55)
   at java.lang.reflect.Method.invoke(Native Method)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 12:54 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
}catch(HibernateException he) {
s.close();
t.rollback();
throw ........

You close session before rollback.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Session s = null;
s = sessionFactory.openSession(); // debug, look attributes of session
what architecture are you using?

i think you don't have to flush before commit since commit execute flush...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 12:58 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
looking at stacktrace the problem is on getOrder() method no?

but baliukas is right (of course :))


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:13 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
BTW data access methods must look like this, without code dublication:

Code:
public OrderHeader getOrder(long orderNumber) throws Exception {
        return (OrderHeader)currentSession().
            load(OrderHeader.class, new Long(orderNumber));
       
}


try / cach, close, commit , ... outside method in single place.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:22 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
looking at stacktrace the problem is on getOrder() method no?

but baliukas is right (of course :))


Yes I know, but why?

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:23 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
Session s = null;
s = sessionFactory.openSession(); // debug, look attributes of session
what architecture are you using?

i think you don't have to flush before commit since commit execute flush...


What do you mean by saying what architecture?

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:28 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
Session s = null;
s = sessionFactory.openSession(); // debug, look attributes of session
what architecture are you using?

i think you don't have to flush before commit since commit execute flush...


The class OrderHeader have orderItems:
Code:
<map name="orderItems">
        <key>
           <column name="ODORNO"/>
        </key>
        <index column="ODISBN" type="com.bertrams.berteShop.dao.hibernate.TrimmedString"/>
        <one-to-many class="OrderItem"/>
</map>


and i think the problem is to do with that. If i remove this from the mapping it is working ok.

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:47 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
try

<map lazy="false" name="orderItems">
<key>
<column name="ODORNO"/>
</key>
<index column="ODISBN" type="com.bertrams.berteShop.dao.hibernate.TrimmedString"/>
<one-to-many class="OrderItem"/>
</map>


the pb that can occur is that the collection is hit after the session is closed...

I was talking about architecture because there are better many ways to manage hibernate session (see open session in view for example)
but it depends your architecture (controller, servlet...)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:00 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
try

<map lazy="false" name="orderItems">
<key>
<column name="ODORNO"/>
</key>
<index column="ODISBN" type="com.bertrams.berteShop.dao.hibernate.TrimmedString"/>
<one-to-many class="OrderItem"/>
</map>


the pb that can occur is that the collection is hit after the session is closed...

I was talking about architecture because there are better many ways to manage hibernate session (see open session in view for example)
but it depends your architecture (controller, servlet...)


We uses struts to call hibernate code. But as you said i am not quite sure how to work properly with session.

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:03 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ok if you use struts, just take a look at open session in view (search in the site)
it's just a servlet filter and with it you're sure your session is created/close on each httpRequest

what about setting lazy at false?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:13 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
ok if you use struts, just take a look at open session in view (search in the site)
it's just a servlet filter and with it you're sure your session is created/close on each httpRequest

what about setting lazy at false?


Sorry about it but what is servletFilter?

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:14 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
don't be sorry
just follow this:
http://www.hibernate.org/43.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:15 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:38 am
Posts: 26
delpouve wrote:
ok if you use struts, just take a look at open session in view (search in the site)
it's just a servlet filter and with it you're sure your session is created/close on each httpRequest

what about setting lazy at false?


I set it to false but it did not help.

At the moment i am testing the code with JUnit(TestCase).

_________________
Oren Berenson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 2:31 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Remove noise from code and you will find the cause :

}catch(HibernateException he) {
s.close();
t.rollback(); //this throws exception

use this way:

public OrderHeader getOrder(Session session, long orderNumber) throws Exception {
return (OrderHeader)session.
load(OrderHeader.class, new Long(orderNumber));

}

or

public OrderHeader getOrder( long orderNumber) throws Exception {
return (OrderHeader)((Session)ThreadLocal.get()).
load(OrderHeader.class, new Long(orderNumber));

}

You will have more problems, if you will dublicated code.

you can manage session and demarcate transactions in Servlet.service method :

void service(.....
try{

super.service( ...

COMMIT

}catch( ....

ABORT

}finally{

CLOSE
.........


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.