title: Receiving org.hibernate.ResourceClosedException "This TransactionCoordinator has been closed"
body: <p>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".</p>
  
  <p>I've already checked that <code>beginTransaction()</code> and <code>rollback()</code> or <code>commit()</code> are done before the thread is reused by another call.</p>
  
  <p>At hibernate.cfg.xml, I have</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
   &lt;session-factory&gt;
    &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
    &lt;property name="hibernate.connection.password"&gt;password&lt;/property&gt;
    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://host/database&lt;/property&gt;
    &lt;property name="hibernate.connection.username"&gt;username&lt;/property&gt;
    &lt;property name="hibernate.connection.zeroDateTimeBehavior"&gt;convertToNull&lt;/property&gt;
    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/property&gt;
    &lt;property name="show_sql"&gt;true&lt;/property&gt;
    &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
    &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
    &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
    &lt;property name="hibernate.c3p0.timeout"&gt;1800&lt;/property&gt;
    &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
  
    &lt;!-- mappings start here --&gt;
    ...
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>and I am using a HibernateUtil.java to create a static sessionFactory for my application and I use it to control my transactions:</p>
  
  <pre><code>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();
      }
  
  }
  </code></pre>
  
  <p>When I start using the database in a servlet, my code is basically this:</p>
  
  <pre><code>    try {
          HibernateUtil.beginTransaction();
  
          // do stuffs
  
          HibernateUtil.commit();
      } catch (RuntimeException e) {
          HibernateUtil.rollback();
          throw e;
      } catch (Exception e) {
          HibernateUtil.rollback();
          throw e;
      }
  </code></pre>
  
  <p>Where am I doing wrong?</p>