Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ECBSHIS.DUAL, DRIVER=4.22.29) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388) at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141) at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:301) at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214) ... 69 moreCaused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ECBSHIS.DUAL, DRIVER=4.22.29 at com.ibm.db2.jcc.am.ld.a(ld.java:810) at com.ibm.db2.jcc.am.ld.a(ld.java:66) at com.ibm.db2.jcc.am.ld.a(ld.java:140) at com.ibm.db2.jcc.am.up.c(up.java:2796) at com.ibm.db2.jcc.am.up.d(up.java:2784) at com.ibm.db2.jcc.am.up.a(up.java:2212) at com.ibm.db2.jcc.t4.bb.i(bb.java:148) at com.ibm.db2.jcc.t4.bb.b(bb.java:41) at com.ibm.db2.jcc.t4.p.a(p.java:32) at com.ibm.db2.jcc.t4.vb.i(vb.java:145) at com.ibm.db2.jcc.am.up.lb(up.java:2181) at com.ibm.db2.jcc.am.up.a(up.java:3300) at com.ibm.db2.jcc.am.up.a(up.java:717) at com.ibm.db2.jcc.am.up.executeQuery(up.java:696) at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208) at org.apache.commons.dbcp.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:658) at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1558) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545) ... 74 more
解决方法:
DBCP针对不同数据库的validationQuery 当你使用DBCP连接池时,你可以通过设置testOnBorrow和testOnReturn属性测试这个连接是否可用。不幸的是你还需要设置validationQuery才能起作用。那么问题来了如何设置validationQuery这个值呢?
什么是validationQuery validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条数据的SELECT语句。每种数据库都有各自的验证语句,下表中收集了几种常见数据库的validationQuery。
-
- DataBase validationQuery
-
- hsqldb select 1 from INFORMATION_SCHEMA.SYSTEM_USERS
-
- Oracle select 1 from dual
-
- DB2 select 1 from sysibm.sysdummy1
-
- MySql select 1
-
- Microsoft SqlServer select1
-
- postgresql select version()
-
- ingres select 1
-
- derby values 1
-
- H2 select 1
根据JDBC驱动获取validationQuery 如果你想支持多种数据库,你可以根据JDBC驱动来获取validationQuery,这里有个简单的类,根据JDBC驱动名称来获取validationQuery