Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用EclipseLink避免MySQL连接超时错误?_Java_Mysql_Jpa_Eclipselink_Connection Timeout - Fatal编程技术网

Java 如何使用EclipseLink避免MySQL连接超时错误?

Java 如何使用EclipseLink避免MySQL连接超时错误?,java,mysql,jpa,eclipselink,connection-timeout,Java,Mysql,Jpa,Eclipselink,Connection Timeout,如果什么也没有发生,MySQL会在一段时间后关闭连接()。时间可能会受到配置中wait_timeout变量的影响 我有一个Eclipse RCP应用程序,其中我使用EclipseLink作为持久性框架,当客户端超过超时时,我会收到一个错误: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connecti

如果什么也没有发生,MySQL会在一段时间后关闭连接()。时间可能会受到配置中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/autoeconnectforpools设置为true,但这没有帮助

谢谢

编辑

在my 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);
Map=。。。
put(PersistenceUnitProperties.JDBC_URL,…);
put(PersistenceUnitProperties.JDBC_USER,…);
put(PersistenceUnitProperties.JDBC_密码,…);
put(PersistenceUnitProperties.JDBC_驱动程序,…);
put(PersistenceUnitProperties.CLASSLOADER,this.getClass())
.getClassLoader());
put(PersistenceUnitProperties.TARGET_数据库,“MySQL”);
entityManagerFactory=新的PersistenceProvider()
.createEntityManagerFactory(“…”,地图);

最简单的方法是生成一个线程,每小时左右发送一次keepalive或简单查询。在这里,我们将留下一个标志,以便线程可以在程序退出、db更改等时关闭。如果它需要更快地响应这种类型的关闭,您可以更改for循环中的计数器和睡眠时间

boolean parentKilledMe = false;
while (!parentKilledMe){
    //put query here
    for (int x = 0; x < 360 && !parentKilledMe;x++){
        try{
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            //your error handling here
        }
    }
}
布尔parentKilledMe=false;
而(!parentKilledMe){
//把查询放在这里
对于(int x=0;x<360&!parentKilledMe;x++){
试一试{
睡眠(6000);
}捕捉(中断异常e){
//您的错误处理在这里
}
}
}

EclipseLink应自动重新连接失效连接。EclipseLink将捕获错误,测试连接,如果连接失效,则重新连接,并可能重试查询(如果在事务之外)


但这取决于您使用的是什么连接池,什么是persistence.xml。

非常感谢您的回答。我将我的配置添加到问题中。我在persistence.xml中找不到如何配置这个的解决方案。您使用的是什么版本的EclipseLink?您是否可以包含完整的异常堆栈,并在获得时进行解释。如果处理错误并重试,是否再次出现错误?同时删除“eclipselink.jdbc.read connections.max”和“eclipselink.jdbc.read connections.shared”设置,不建议使用这些设置。使用“eclipselink.connection pool.default.max”设置最大值(默认值为32),除非您使用的是旧版本。我使用的是eclipselink 2.2.0。现在升级到2.3.1。正如你所说,我还更改了参数设置。在这之后,我再也不能重现错误了。非常感谢詹姆斯的帮助。