Receiving org.hibernate.ResourceClosedException "This
TransactionCoordinator has been closed"
I have a servlet, running on Tomcat 6, that uses Hibernate 4.1.6 and c3p0
0.9.1.2, and I am getting too much org.hibernate.ResourceClosedException,
with the message "This TransactionCoordinator has been closed".
I've already checked that beginTransaction() and rollback() or commit()
are done before the thread is reused by another call.
At hibernate.cfg.xml, I have
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property
name="hibernate.connection.url">jdbc:mysql://host/database</property>
<property name="hibernate.connection.username">username</property>
<property
name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- mappings start here -->
...
</session-factory>
</hibernate-configuration>
and I am using a HibernateUtil.java to create a static sessionFactory for
my application and I use it to control my transactions:
public class HibernateUtil {
private static final SessionFactory sessionFactory =
buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new
ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory =
configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}
public static void beginTransaction() {
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
}
public static void commit() {
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
}
public static void rollback() {
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
}
}
When I start using the database in a servlet, my code is basically this:
try {
HibernateUtil.beginTransaction();
// do stuffs
HibernateUtil.commit();
} catch (RuntimeException e) {
HibernateUtil.rollback();
throw e;
} catch (Exception e) {
HibernateUtil.rollback();
throw e;
}
Where am I doing wrong?
No comments:
Post a Comment