现象:
1
2
3
|
Caused by: java.sql.SQLException: Already closed. at org.apache.tomcat.dbcp.dbcp.PoolableConnection.close(PoolableConnecti on.java:114) |
原因:
在长时间没有访问tomcat后,数据库连接已经失效,连接池因为没有检测,所以认为连接有效,导致连接出错。避免这种错误的方法是设置连接有效查询sql语句。设置后,连接池会使用该sql语句测试连接是否有效,如果无效,会重新建立连接。
解决方案:
设置方法为:在%JUSTEP_HOME%\apache-tomcat\conf\context.xml中的节点中增加属性validationQuery
mysql数据库:
1
2
3
4
5
|
< Resource name = "jdbc/cloud" type = "javax.sql.DataSource" maxActive = "100" maxIdle = "30" username = "root" password = "x5" validationQuery = "select 1" /> |
SQLServer数据库:
在server.xml中的节点中增加属性validationQuery=”select sysdate from dual;”。例如:
1
2
3
|
< Resource name = "system" auth = "Container" type = "javax.sql.DataSource" driverClassName = "net.sourceforge.jtds.jdbc.Driver" url = "jdbc:jtds:sqlserver://127.0.0.1:1433/x5_1900" username = "sa" password = "sa" maxActive = "10" maxIdle = "5" validationQuery = "select sysdate from dual;" /> |
oralce示例
1
2
3
|
< Resource name = "reg" auth = "Container" type = "javax.sql.DataSource" driverClassName = "oracle.jdbc.driver.OracleDriver" url = "xxxx" username = "xx" password = "xx" maxActive = "10" maxIdle = "5" validationQuery = "select 1 from dual" /></ pre > < pre > |