关于java.sql.SQLRecoverableException: Closed Connection异常的解决方案(转)

 

转自:https://www.cnblogs.com/softidea/p/4004076.html

 

在项目中碰到了一个应用异常,从表象来看应用僵死。查看Weblogic状态为Running,内存无溢出,但是出现多次线程堵塞。查看Weblogic日志,发现程序出现多次Time Out。

我们知道,Weblogic会自动检测线程运行超时,当超过特点时间(默认600S),即认为此线程为堵塞线程。在日志中发现多次堵塞线程,通过查找资料,发现Weblogic在发生多次线程堵塞后,会自动把应用挂起。默认次数为15次。

 

是什么造成了线程堵塞呢?通过进一步分析日志,我们发现在线程堵塞之前,发生了多次java.sql.SQLRecoverableException: Closed Connection异常。异常情况:

 

从表现来看是数据库连接出了异常。我们对数据库和网络进行了分析,确定数据库和网络都无异常。我们的另外一个应用在Weblogic运行没有类似问题。

 

最后在Oracle的论坛上找到了问题的根结,由于我们的应用是自己开发的数据库连接池,应用和数据库之间有一层防火墙。防火墙策略是对于1800s未使用的Socket连接将自动关闭。Oracle的日志中也发现Socket异常关闭的异常。我们对应用进行了调整,当连接池中的连接15分钟不用时,自动回收,问题解决。

 

http://blog.csdn.net/gavinloo/article/details/12206763

 

java.sql.SQLException: Io 异常: Connection reset

当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,但客户端并不知道,在进行数据库操作时仍然使用的是无效的数据库连接,这样,就导致客户端程序报“ java.sql.SQLException: Io 异常: Connection reset” 或“java.sql.SQLException 关闭的连接”异常。

在配置数据源后面加上

<property name="validationQuery" value="select * from dual"/>


配置后,客户端在使用一个无效的连接时会先对该连接进行测试,如果发现该连接已经无效,则重新从连接池获取有效数据库连接来使用。

http://blog.csdn.net/majian_1987/article/details/18598589

JDK1.6:
java.sql
Interface Statement:
setQueryTimeout

void setQueryTimeout(int seconds)
                     throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown. A JDBC driver must apply this limit to the execute, executeQuery and executeUpdate methods. JDBC driver implementations may also apply this limit to ResultSet methods (consult your driver vendor documentation for details).

 

Parameters:
seconds - the new query timeout limit in seconds; zero means there is no limit
Throws:
SQLException - if a database access error occurs, this method is called on a closed Statement or the condition seconds >= 0 is not satisfied
See Also:
getQueryTimeout()
 
posted on 2019-05-10 19:56  腾逸  阅读(2206)  评论(0编辑  收藏  举报