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!