在JAVA中JDK版本与JDBC版本的一致性十分重要,开发都们常常会忽略了这一点导致很多不必要的错误。昨天给客户排查了一个关于EDB在JBoss中使用时关于这方面的问题,希望给大家一点启发。
系统环境:
Red Hat Enterpirse Linux 6
JBoss Enterprise Application Server 6
EnterpriseDB Postgres Plus Advanced Server 9
Oracle JDK 1.6
问题症状:
在JBoss日志中间歇性地出现以下错误
java.sql.SQLException: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/jdbc/DSZY101----Caused by: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/jdbc/DSZY101-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:390)-- at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)-- at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)-- at jp.co.toyota.perz060.servlet.CCERZ060.getConnection(CCERZ060.java:489)-- at jp.co.toyota.perz060.servlet.CCERZ060.doPost(CCERZ060.java:118)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1352.util.CIZY1392.doFilter(CIZY1392.java:362)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1900.util.CIZY1947.doFilter(CIZY1947.java:99)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)-- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)-- at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)-- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)-- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)-- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)-- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)-- at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505)-- at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:452)-- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931)-- at java.lang.Thread.run(Thread.java:662)----Caused by: javax.resource.ResourceException: IJ000658: Unexpected throwable while trying to create a connection: null-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:371)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)-- at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)-- at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)-- at jp.co.toyota.perz060.servlet.CCERZ060.getConnection(CCERZ060.java:489)-- at jp.co.toyota.perz060.servlet.CCERZ060.doPost(CCERZ060.java:118)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1352.util.CIZY1392.doFilter(CIZY1392.java:362)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1900.util.CIZY1947.doFilter(CIZY1947.java:99)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)-- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)-- at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)-- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)-- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)-- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)-- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)-- at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505)-- at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:452)-- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931)-- at java.lang.Thread.run(Thread.java:662)----Caused by: javax.resource.ResourceException: Could not create connection-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:277)-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:235)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:761)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:343)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)-- at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)-- at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)-- at jp.co.toyota.perz060.servlet.CCERZ060.getConnection(CCERZ060.java:489)-- at jp.co.toyota.perz060.servlet.CCERZ060.doPost(CCERZ060.java:118)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1352.util.CIZY1392.doFilter(CIZY1392.java:362)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1900.util.CIZY1947.doFilter(CIZY1947.java:99)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)-- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)-- at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)-- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)-- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)-- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)-- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)-- at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505)-- at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:452)-- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931)-- at java.lang.Thread.run(Thread.java:662)----Caused by: com.edb.util.PSQLException: The connection attempt failed.-- at com.edb.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:189)-- at com.edb.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)-- at com.edb.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:161)-- at com.edb.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)-- at com.edb.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:23)-- at com.edb.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)-- at com.edb.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:25)-- at com.edb.Driver.makeConnection(Driver.java:391)-- at com.edb.Driver.connect(Driver.java:266)-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:249)-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:235)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:761)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:343)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)-- at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)-- at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)-- at jp.co.toyota.perz060.servlet.CCERZ060.getConnection(CCERZ060.java:489)-- at jp.co.toyota.perz060.servlet.CCERZ060.doPost(CCERZ060.java:118)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1352.util.CIZY1392.doFilter(CIZY1392.java:362)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1900.util.CIZY1947.doFilter(CIZY1947.java:99)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)-- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)-- at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)-- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)-- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)-- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)-- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)-- at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505)-- at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:452)-- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931)-- at java.lang.Thread.run(Thread.java:662)----Caused by: java.net.SocketException: Too many open files-- at java.net.Socket.createImpl(Socket.java:397)-- at java.net.Socket.<init>(Socket.java:371)-- at java.net.Socket.<init>(Socket.java:189)-- at com.edb.core.PGStream.<init>(PGStream.java:70)-- at com.edb.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:115)-- at com.edb.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)-- at com.edb.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:161)-- at com.edb.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)-- at com.edb.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:23)-- at com.edb.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)-- at com.edb.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:25)-- at com.edb.Driver.makeConnection(Driver.java:391)-- at com.edb.Driver.connect(Driver.java:266)-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:249)-- at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:235)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:761)-- at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:343)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)-- at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)-- at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)-- at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)-- at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)-- at jp.co.toyota.perz060.servlet.CCERZ060.getConnection(CCERZ060.java:489)-- at jp.co.toyota.perz060.servlet.CCERZ060.doPost(CCERZ060.java:118)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)-- at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1352.util.CIZY1392.doFilter(CIZY1392.java:362)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at jp.co.toyota.pzy1900.util.CIZY1947.doFilter(CIZY1947.java:99)-- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)-- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)-- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)-- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)-- at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)-- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)-- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)-- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)-- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)-- at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505)-- at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:452)-- at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931)-- at java.lang.Thread.run(Thread.java:662)--
E 2014-06-15 20:27:44.272 XML - 0 - -
问题分析:
- 从上面的错误代码中可见“javax.resource.ResourceException: IJ000658: Unexpected throwable while trying to create a connection: null”,从经验上判断这可能是由于JDK版本与JDBC不匹配所造成的。当前各数据库都有类似的操作方式,通常针对JDK 1.6以下版本,及1.6以上版本会发布不同的JDBC驱动包,以EDB为例会有:
edb-jdbc14.jar 对应 JDK1.6以下版本;
edb-jdbc16.jar 对应 JDK1.6及以上版本。
- 由以上怀疑点要求客户调出系统中存放JDBC驱动的目录,发现在此目录下同时存在以上两个版本的jar文件,同时此目录中有一个module.xml文件,当中内存如下:
<
module
xmlns
=
"urn:jboss:module:1.0"
name
=
"com.edb"
>
<
resources
>
<
resource-root
path
=
"edb-jdbc16.jar"
/>
</
resources
>
<
dependencies
><
module
name
=
"javax.api"
/></
dependencies
>
</
module
>
- 进一步分析,客户告知在他们的代码中有可能出现以下操作:
Class.forName("com.edb.Driver");
String url = "jdbc:edb://xxx.xxx.xxx.xxx:5444/edb";
String user = "enterprisedb";
String password = "xxxxxxx";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
......
由此推断如果客户代码中存在以上操作,在运行此操作时将不授JBoss中的配置影响而直接通过JDK调用可以找到的JDBC。由于目录中存在edb-jdbc14.jar,而从目录的搜索角度14排在16之前,因此程序在执行以上操作时很可能会使用了edb-jdbc14.jar。在JAVA进行数据序列化操作时,因版本不对应就有可能产生以上问题。这就解析了为何只有少量操作报连接无法找到而不是全部数据库连接均报错。
- 进一步检查在另一套客户的测试环境中没有出现以上错误,而此测试环境下只有edb-jdbc16.jar驱动
- 从EDB数据库方面分析,在数据库日志中并没有在此上述时段收到连接,也没有相应错误。判断:JAVA端在获得连接Socket端口后由于版本不对应,无法序列化,因此可能根本上就没有发送用户名、密码等登陆信息,因此数据库没有任何日志。
- 终上所述基本定位问题所在
分析图解:
解决方案:
- 申请停机时间,进行完整测试及修复;
- 建议将目录中的edb-jdbc14.jar删除;
- 严格保证日后所有项目中对应一个数据库只有一个JDBC;
- 在JBoss框架下严格使用JNDI或其他公共方式进行连接,避免在JAVA程序中通过直接调用getConnection方式进行数据库连接;
解决结果【未进行】:
当前以上所暴露的问题只是一个推断,由于客户当前是一个生产系统,无法现场进行测试及修复。【未完待续】