首页 文章

JDBC MySQL - 连接已关闭

提问于
浏览
0

我有一个Web应用程序 . 我使用JPA(Hibernate作为供应商),应用程序在glassfish服务器上运行 . 几分钟后使用应用程序(包括检索并将实体保存到数据库的操作)我收到此错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
root cause

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
root cause

org.hibernate.exception.GenericJDBCException: Could not open connection
root cause

java.sql.SQLException: Error in allocating a connection. Cause: java.lang.RuntimeException: Got exception during XAResource.start:
root cause

javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: java.lang.RuntimeException: Got exception during XAResource.start:
root cause

com.sun.appserv.connectors.internal.api.PoolingException: java.lang.RuntimeException: Got exception during XAResource.start:
root cause

com.sun.appserv.connectors.internal.api.PoolingException: java.lang.RuntimeException: Got exception during XAResource.start:
root cause

java.lang.RuntimeException: Got exception during XAResource.start:
root cause

javax.transaction.xa.XAException: com.sun.appserv.connectors.internal.api.PoolingException: javax.resource.spi.LocalTransactionException: No operations allowed after connection closed.

我正在使用JTA事务管理器 . 我的服务层中的每个函数都有 @Transactional 注释 . 这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
    version="2.0">
    <persistence-unit name="myPU" transaction-type="JTA">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/myDataSource</jta-data-source>

        <class>org.company.entities.User</class>
            <!-- OTHER CLASSES -->

        <exclude-unlisted-classes />

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.transaction.flush_before_completion" value="true" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.connection.release_mode" value="auto"/>
            <property name="hibernate.current_session_context_class" value="jta"/>
            <property name="hibernate.transaction.auto_close_session" value="true"/>
<!--             <property name="hibernate.transaction.flush_before_completion" value="true"/> -->
<!--             <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/> -->
        </properties>

    </persistence-unit>
</persistence>

我正在使用这个mysql连接器: mysql-connector-java-5.1.22-bin.jar 为什么我收到此错误?我或JPA在哪里关闭连接?

EDIT: 删除了类"BaseEntityDAO"的详细信息 - 不相关 . 我的数据源是通过glassfish的管理控制台配置的:在JDBC连接池中:

  • 资源类型:javax.sql.DataSource

  • DataSource类名:com.mysql.jdbc.jdbc2.optional.MysqlDataSource

  • 我正在使用glassfish的默认池设置 - 不知道它是否重要,但其中一个默认设置是"Idle Timeout",其默认值为300秒 .

  • 在附加属性中,我有以下属性:portNumber,databaseName,serverName,user,password

在JDBC资源中:

  • JNDI名称:jdbc / myDataSource

  • 池名称:上述JDBC连接池的池名称

这是我的 spring 配置:

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPersistenceUnit" />

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
        <map>
            <entry key="myPersistenceUnit" value="persistence/myPersistenceUnit" />
        </map>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDataSource" />

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource" />
</bean>

1 回答

  • 0

    您可以使用此配置尝试使用C3P0:

    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="c3p0.testConnectionOnCheckout">false</property>
    <property name="c3p0.min_size">2</property>
    <property name="c3p0.max_size">10</property>
    <property name="c3p0.timeout">300</property>
    <property name="c3p0.max_statements">50</property>
    <property name="c3p0.idleTestPeriod">300</property>
    

    我多年来一直在很多项目中使用这种配置 .

相关问题