-->
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.  [ 2 posts ] 
Author Message
 Post subject: [org.hibernate.SessionException: Session is closed!]
PostPosted: Mon Jan 14, 2008 7:14 am 
Newbie

Joined: Thu Sep 06, 2007 9:48 pm
Posts: 2
Hibernate version:
3.2.5ga

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.passpod.server.model">
<class name="PasspodUser" table="user">
<id name="id" type="long" column="ID">
<generator class="native" />
</id>
<property name="username" column="USERNAME" type="string" not-null="true" unique="true"/>
<property name="created" column="CREATED" type="timestamp" not-null="true" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:503)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.saveOrUpdate(Unknown Source)
at com.passpod.server.dao.hibernate.GenericHibernateDAO.makePersistent(GenericHibernateDAO.java:84)
at com.passpod.server.util.TestUtil.main(TestUtil.java:40)


Name and version of the database you are using:
INFO: RDBMS: MySQL, version: 5.0.22-community-nt

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Hi,

I am trying to implement a very simple dao layer. I follow the style of CaveatEmptor example. I am sure I have read the above article thoroughly, but I still got this exception again and again.
Exception in thread "main" org.hibernate.SessionException: Session is closed!

Please see my code:
Code:
DAOFactory daoFactory = DAOFactory.instance(DAOFactory.HIBERNATE);
      PasspodUserDAO udao = daoFactory.getPasspodUserDAO();
      PasspodUser usr = null;
      
      // =====Transaction1 Start=====
      HibernateUtil.getSessionFactory().getCurrentSession()
            .beginTransaction();
      usr = udao.getUserByName("test");
      HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
            .commit();
      // =====Transaction1 End=====
      
      if (usr == null) {
         System.out.println("user not found! now creating...");
         // =====Transaction2 Start=====
         Session s1 = HibernateUtil.getSessionFactory().getCurrentSession();
         System.out.println(s1.isOpen());
         Transaction tx1 = s1.beginTransaction();
         System.out.println(s1.isOpen());
         usr = new PasspodUser();
         usr.setUsername("test");
         //s1.saveOrUpdate(usr);
         udao.makePersistent(usr);
         tx1.commit();
         // =====Transaction2 End=====
      } else {
         System.out.println("user found! created at: " + usr.getCreated());
      }


The strange thing is that the first transaction works fine, but second one does NOT work at all. The problem comes from here:
Code:
udao.makePersistent(usr);


If I comment this line out and another line, i.e.
Code:
s1.saveOrUpdate(usr);

Then it works!

The two lines
Code:
System.out.println(s1.isOpen());

print out "true".

Very strange! The session is open, but this line throws an exception saying that "Session is closed!"
Code:
udao.makePersistent(usr);


Please give help! This problem make me crazy. :( Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 14, 2008 5:16 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
I would suggest to retry your code example not creating so many sessions. Evidently, whatever session "udao.makePersistent()" is using is not the same as those sessions you are creating for loading and transactions.

_________________
Gonzalo Díaz


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