Java 运行时JDBC程序中的MySQLNonTransientConnectionException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2297356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQLNonTransientConnectionException in JDBC program at run time
提问by Sunil Kumar Sahoo
I have a JDBC MySQL connection in Java. My program works fine for simple execution of query.
我在 Java 中有一个 JDBC MySQL 连接。我的程序可以很好地执行简单的查询。
If I run the same program for more than 10 hours and execute a query then I receive the following MySQL exception:
如果我运行同一个程序超过 10 个小时并执行查询,那么我会收到以下 MySQL 异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Connection.close() has already been called. Invalid operation in
this state.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(
Native Method)
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(
Native Method)
I have not used close()
method anywhere. I created database connection and opened it forever and always executed query. There is no place where I explicitly mentioned timeout for connection. I am unable to identify the problem.
我没有close()
在任何地方使用过方法。我创建了数据库连接并永远打开它并始终执行查询。我没有明确提到连接超时的地方。我无法确定问题。
Here is the code I use for the database connection:
这是我用于数据库连接的代码:
String driver = PropertyReader.getDriver();
String url = dbURLPath;
Class.forName(driver);
connectToServerDB = DriverManager.getConnection(url);
connectToServerDB.setAutoCommit(false);
What causes that exception?
是什么导致了这个异常?
采纳答案by Sunil Kumar Sahoo
You have to make a change in the configuration file or increase the timeout period of your database. If database remains idle for more than 8 hours it is closed by default.
您必须更改配置文件或增加数据库的超时时间。如果数据库空闲时间超过 8 小时,则默认关闭。
Thanks
谢谢
回答by Slava Imeshev
MySQL terminates a connection after 8 hour timeout. You can modify the timeout by setting wait_timeoutvariable in MySQL.
MySQL 在 8 小时超时后终止连接。您可以通过在 MySQL 中设置wait_timeout变量来修改超时。
Yet, generally it is not such a good idea for an application to hold a connection for such a long time. A better approach is to set up a connection pool using a pooling API such as Apache DBCPand retrieve connections from the pool rather than directly though a driver. A connection pool will also take care about re-establishing a pooled connection if it dies for some reason, including timeouts.
然而,通常对于应用程序来说,保持连接这么长时间并不是一个好主意。更好的方法是使用池 API(例如Apache DBCP)设置连接池并从池中检索连接,而不是直接通过驱动程序。如果连接池由于某种原因(包括超时)死亡,连接池还将负责重新建立池连接。
回答by Vivek
I faced the same problem too. It's because of connection timeout by mysql and generally its not a good practice to extent the timeout in mysql as it serves for many other applications. It's good to reconnect the database on timeouts (i.e when this exception occurs) or to use some open source libraries for connection pooling like Apache DBCPas @slava suggested. ORM Frameworks takes care of this by default. Cheers!!
我也面临同样的问题。这是因为 mysql 的连接超时,并且通常它不是一个好的做法来扩展 mysql 中的超时,因为它服务于许多其他应用程序。最好在超时(即发生此异常时)重新连接数据库或使用一些开源库进行连接池,如@slava 建议的Apache DBCP。默认情况下,ORM 框架会处理这个问题。干杯!!