Hello,
Does calling commit() or rollback() automatically close the session that you are using? I've checked the doco and it does not mention it, but after issuing a commit (or rollback) I get the following exception:
Code:
org.hibernate.SessionException: Session was already closed
org.hibernate.impl.SessionImpl.close(SessionImpl.java:275)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
$Proxy2.close(Unknown Source)
com.thinkdesigns.sett.ChargePeriodRegistryHibernate.createChargePeriod(ChargePeriodRegistryHibernate.java:28)
Below is the createChargePeriod function from the ChargePeriodRegistryHibernate class:
Code:
public long createChargePeriod( ChargePeriodBean chg ) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Long id = (Long) session.save(chg);
session.getTransaction().commit();
session.close();
return id;
}
The HibernateUtil class is as follows:
Code:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Many Thanks for your help
Andy