如果没有任何反应,MySQL 会在一段时间后关闭连接(默认为 8 小时)。时间会受到配置中的 wait_timeout 变量的影响。
我有一个 Eclipse RCP 应用程序,我在其中使用 EclipseLink 作为持久性框架,当客户端超过超时时出现错误:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
...
com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException
...
org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor...
我试图设置 autoReconnect/autoReconnectForPools=true 但这没有帮助。
谢谢
编辑
在我的 persistence.xml 中,我设置了以下属性:
<property
name="eclipselink.jdbc.read-connections.max"
value="10" />
<property
name="eclipselink.jdbc.cache-statements"
value="true" />
<property
name="eclipselink.jdbc.read-connections.shared"
value="true" />
其余的配置在代码中完成:
Map<Object, Object> map = ...
map.put(PersistenceUnitProperties.JDBC_URL,...);
map.put(PersistenceUnitProperties.JDBC_USER,...);
map.put(PersistenceUnitProperties.JDBC_PASSWORD, ...);
map.put(PersistenceUnitProperties.JDBC_DRIVER, ...);
map.put(PersistenceUnitProperties.CLASSLOADER, this.getClass()
.getClassLoader());
map.put(PersistenceUnitProperties.TARGET_DATABASE, "MySQL");
entityManagerFactory = new PersistenceProvider()
.createEntityManagerFactory("...", map);