簡體   English   中英

在OpenShift上使用JPA產生奇怪的錯誤

[英]Strange error using JPA on OpenShift

我已經擔心了幾天的錯誤-也是因為很難測試它是否仍然發生。

從OpenShift托管的Java EE應用程序中,我正在連接到MySQL盒式磁帶。 我正在使用JPA(休眠)來處理我的連接並用於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>

我已經查看了許多示例和教程-但是我找不到問題。

提前致謝!

始終不要使用對EntityManager實例的引用,在您的情況下,靜態變量em在關閉連接后將保持停頓對象。

有關將EntityManager范圍限制到當前事務的最佳實踐建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM