tomcat dbcp 基于jndi配置时出现java.sql.SQLException: Already closed


最近观察生产环境发现一个现象,一段时间不操作,再重新操作时,数据库连接第一次会出现:java.sql.SQLException: Already closed.,如下:




数据源是用tomcat dbcp 基于jndi配置管理的,查了下,如下:


原因:

You‘re probably running into the fact that MYSQL closes connections  which have been open "too long". 
Probably if you make the request to the server again, you‘ll find that  it works because the connection is reopened. 


解决方法:

One way to deal with this is to add the following to your context.xml  Resource: 

validationQuery="select 1"  (基于mysql)

validationQuery="select 1 from dual"  (基于oracle)


代价:

This causes a very cheap test query to always be run first; if the  connection has been closed, this gets the failure, and 
then a new connection is opened.


细想下原来用weblogic jdbc jndi的配置有一个专门的配置项来配置这个test query,配置tomcat时粗心大意了 :(  


tomcat dbcp 基于jndi配置时出现java.sql.SQLException: Already closed,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。