我有一个持久单元,在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>
tome.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获得时,证明数据源还可以:
@Resource(name=xxxDB)
DataSource dataSource;
....
try (Connection connection = dataSource.getConnection()) {
// do a query to prove connection OK
}
我有每小时会得到这个servlet的cron工作。连接即使在JPA抛出com.mysql.jdbc.exceptions.jdbc4.mysqlnontransientConnectionException
之后,连接也可以。我的环境Java 8,Tomee 7.1.0,Eclipselink 2.7.3
问题是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,但羽流使用eclipselink。