title: Strange error using JPA on OpenShift
body: <p>I got an error I am worrying about for days already - also because it is hard to test whether it still occurs or not.</p>
  
  <p>From an Java EE application hosted at OpenShift I am connecting to a MySQL cartridge. I am using JPA (Hibernate) to handle my connection and for ORM purposes.</p>
  
  <p>The connection and the application itself is working fine - but when trying to access the database after some hours (I guess after OpenShift "hibernated" the servlet) it doesn't work anymore.</p>
  
  <p>I get the following exception (that is just an excerpt, but it seems to me to be the most important part):</p>
  
  <pre><code>Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
  at com.mysql.jdbc.Util.getInstance(Util.java:360)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
  </code></pre>
  
  <p>Of course I know what the exception is trying to the me - I just do not know why it is thrown...</p>
  
  <p>My database access class looks the following:</p>
  
  <pre><code>public class JPAUtils implements ServletContextListener {
      private static final Logger log = LoggerFactory.getLogger(JPAUtils.class);
      private static EntityManagerFactory emf;
      private static EntityManager em;
  
      public static EntityManagerFactory getEntityManagerFactoryInstance() {
          if (emf == null || !emf.isOpen()) {
              String environment = "test";
  
              if (System.getenv("OPENSHIFT_MYSQL_DB_URL") != null) {
                  environment = "production";
              }
  
              emf = Persistence.createEntityManagerFactory(environment);
          }
  
           return emf;
      }
  
      public static EntityManager getEntityManagerInstance() {
          if (em == null || !em.isOpen()) {
              em = getEntityManagerFactoryInstance().createEntityManager();
          }
  
          return em;
      }
  
      @Override
      public void contextInitialized(ServletContextEvent servletContextEvent) {
          log.debug("Context initialized!");
      }
  
      @Override
      public void contextDestroyed(ServletContextEvent servletContextEvent) {
          try {
              log.debug("Closing JPA connection...");
              em.close();
              emf.close();
              em = null;
              emf = null;
  
              log.debug("Closed JPA connection!");
          }
          catch (Exception e) {
              log.error("Error occurred during closing of JPA connection!", e);
          }
      }
  }
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
               version="1.0"&gt;
      &lt;persistence-unit name="test"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
          &lt;properties&gt;
              &lt;property name="hibernate.archive.autodetection" value="class, hbm" /&gt;
              &lt;property name="hibernate.show_sql" value="false" /&gt;
              &lt;property name="hibernate.format_sql" value="true" /&gt;
              &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
  
              &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost/&lt;db_name&gt;?autoReconnect=true" /&gt;
              &lt;property name="hibernate.connection.username" value="***" /&gt;
              &lt;property name="hibernate.connection.password" value="***" /&gt;
  
  
              &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
  
              &lt;property name="hibernate.c3p0.min_size" value="5" /&gt;
              &lt;property name="hibernate.c3p0.max_size" value="10" /&gt;
              &lt;property name="hibernate.c3p0.timeout" value="300" /&gt;
              &lt;property name="hibernate.c3p0.max_statements" value="50" /&gt;
              &lt;property name="hibernate.c3p0.idle_test_period" value="300" /&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt;
          &lt;/properties&gt;
  
      &lt;/persistence-unit&gt;
      &lt;persistence-unit name="production"&gt;
          &lt;!-- If you are running in a production environment, add a managed
             data source, this example data source is just for development and testing! --&gt;
          &lt;!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
             can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml --&gt;
          &lt;non-jta-data-source&gt;java:comp/env/jdbc/MySQLDS&lt;/non-jta-data-source&gt;
          &lt;properties&gt;
              &lt;!-- Properties for Hibernate --&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
              &lt;property name="hibernate.show_sql" value="false" /&gt;
              &lt;!--&lt;property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" /&gt;--&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I already looked up many examples and tutorials - but I cannot find the problem.</p>
  
  <p>Thanks in advance!</p>