我有一个用于Apache TomEE 7.0.4(eclipselink 2.6.4)的持久性单元,但是在升级到TomEE 7.1.0(eclipselink 2.7.4)之后,JPA调用在一段时间后失败了。 persistence.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xxxPU" transaction-type="JTA">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<jta-data-source>xxxDB</jta-data-source>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="mysql"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
tomee.xml中定义的数据源是
<Resource id="xxxDB" type="javax.sql.DataSource">
UserName = xxxx
Password = yyyy
JdbcDriver = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost/dbname
JtaManaged = true
factory = org.apache.tomcat.jdbc.pool.DataSourceFactory
ConnectionProperties = autoReconnect=true;autoReconnectForPools=true;zeroDateTimeBehavior=convertToNull;useUnicode=yes;characterEncoding=UTF-8;useSSL=false
defaultAutoCommit = false
testOnBorrow = true
validationQuery = SELECT 1
validationInterval = 30000
</Resource>
一段时间后出现故障,日志在这里:
15-May-2019 19:12:49.236 SEVERE [ajp-nio-8009-exec-10] org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException EjbTransactionUtil.handleSystemException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Error Code: 0
显然mysql连接已关闭,但实际上并未关闭。我有一个servlet,当进行HTTP GET时,证明数据源是可以的:
@Resource(name=xxxDB)
DataSource dataSource;
....
try (Connection connection = dataSource.getConnection()) {
// do a query to prove connection OK
}
我有cron作业,每小时获取此servlet。即使在JPA抛出com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException
之后,连接仍然可以我的环境Java 8,TomEE 7.1.0,Eclipselink 2.7.3
答案 0 :(得分:0)
问题出在Eclipselink 2.7。我将以下参数添加到了数据源中:
testOnBorrow = true
testWhileIdle = true
timeBetweenEvictionRuns = 60000 millisecond
testOnReturn = true
validationQuery = SELECT 1
validationInterval = 30000
此操作因与apache-tomee-plume的关闭连接而失败,但对于apache-tomee-plus则可以。两者都是ver 7.1.0,只有“ plus”使用openjpa,而plume使用eclipselink。