我有一个网络应用程序。我使用 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
为什么会出现这个错误?i 或 JPA 在哪里关闭连接?
编辑:删除了“BaseEntityDAO”类的细节——不相关。我的数据源是通过 glassfish 的管理控制台配置的: 在 JDBC 连接池中:
- 资源类型:javax.sql.DataSource
- 数据源类名:com.mysql.jdbc.jdbc2.optional.MysqlDataSource
- 我使用 glassfish 的默认池设置 - 不知道它是否重要,但默认设置之一是“空闲超时”,其默认值为 300 秒。
- 在 Additional Properties 中,我有以下属性:portNumber、databaseName、serverName、user、password
在 JDBC 资源中:
- JNDI 名称:jdbc/myDataSource
- Pool Name:上述JDBC连接池的池名
这是我的弹簧配置:
<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>