2015-04-02 95 views
0

我得到了一个错误,我很担心已经有好几天了 - 还因为很难测试它是否仍然存在。OpenShift上使用JPA的奇怪错误

从OpenShift托管的Java EE应用程序中,我连接到MySQL盒式磁带。我正在使用JPA(Hibernate)来处理我的连接和ORM的目的。

连接和本身工作正常的应用程序 - 但试图几个小时后才能访问数据库的时候(我猜OpenShift后“休眠”该servlet),它不工作了。

我得到下面的异常(这仅仅是摘录,但在我看来是最重要的部分):

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) 

我当然知道什么异常试图将我 - 我只是不知道为什么它被抛出...

我的数据库访问类看起来如下:

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); 
     } 
    } 
} 

的persistence.xml:

<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"> 
    <persistence-unit name="test"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 

     <properties> 
      <property name="hibernate.archive.autodetection" value="class, hbm" /> 
      <property name="hibernate.show_sql" value="false" /> 
      <property name="hibernate.format_sql" value="true" /> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> 

      <property name="hibernate.connection.url" value="jdbc:mysql://localhost/<db_name>?autoReconnect=true" /> 
      <property name="hibernate.connection.username" value="***" /> 
      <property name="hibernate.connection.password" value="***" /> 


      <property name="hibernate.hbm2ddl.auto" value="update" /> 

      <property name="hibernate.c3p0.min_size" value="5" /> 
      <property name="hibernate.c3p0.max_size" value="10" /> 
      <property name="hibernate.c3p0.timeout" value="300" /> 
      <property name="hibernate.c3p0.max_statements" value="50" /> 
      <property name="hibernate.c3p0.idle_test_period" value="300" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
     </properties> 

    </persistence-unit> 
    <persistence-unit name="production"> 
     <!-- If you are running in a production environment, add a managed 
      data source, this example data source is just for development and testing! --> 
     <!-- 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 --> 
     <non-jta-data-source>java:comp/env/jdbc/MySQLDS</non-jta-data-source> 
     <properties> 
      <!-- Properties for Hibernate --> 
      <property name="hibernate.hbm2ddl.auto" value="update" /> 
      <property name="hibernate.show_sql" value="false" /> 
      <!--<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />--> 
     </properties> 
    </persistence-unit> 
</persistence> 

我已经看过了许多例子和教程 - 但我不能发现问题。

提前致谢!

回答

1

保持对EntityManager实例的引用永远不会是一个好习惯,您的情况下的静态变量em在连接关闭后保留一个停止对象。

最佳实践建议范围EntityManager s到当前事务。

+0

好的,好点。 但是,这可能不会解决它,至少如果我得到您的建议正确的...... – 2015-04-02 11:37:28

+1

是的,我认为它应该,如果连接因任何原因(钝化等)关闭,您保留在EntityManager静态变量继续使用它,并关闭。这是我认为的错误的起源。 – EmirCalabuch 2015-04-02 16:12:37

+0

好的,在耗尽测试后,它似乎以这种方式工作 - 谢谢! 所以它看起来(这也是什么错误消息说)该entityManager已关闭 - 我只是希望em.isOpen()返回false以防连接关闭...好吧,这可能是因为设置em = null,并且对于活动的entityManger实例可能有限制.... – 2015-04-05 08:25:34