java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed.

Apache's JIRA Issue Tracker | Dave Oxley | 5 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Under high load commons-dbcp (or commons-pool) exhibits thread safety issues and begins throwing various exceptions. I don't yet know the cause of the issue but it looks like a connection maybe handed out to multiple threads concurrently. Here's a few examples of the exceptions we are getting: {noformat} jvm 1 | Caused by: java.sql.SQLException: Attempted to use PooledConnection after closed() was called. jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.assertOpen(PooledConnectionImpl.java:163) jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:174) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:768) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:676) jvm 1 | at uk.co.webessence.kernel.database.DriverAdapterConnectionPool.acquireConnection(DriverAdapterConnectionPool.java:101) jvm 1 | ... 94 more {noformat} {noformat} jvm 1 | Caused by: java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed. jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:179) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:768) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:676) jvm 1 | at uk.co.webessence.kernel.database.DriverAdapterConnectionPool.acquireConnection(DriverAdapterConnectionPool.java:101) jvm 1 | ... 77 more {noformat} {noformat} jvm 1 | Caused by: java.sql.SQLException: Invalid state, the ResultSet object is closed. jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.checkOpen(JtdsResultSet.java:299) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.getColumn(JtdsResultSet.java:273) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.getObject(JtdsResultSet.java:840) jvm 1 | at org.apache.commons.dbcp.DelegatingResultSet.getObject(DelegatingResultSet.java:325) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.getDataArray(Preloader.java:428) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.processSingleRow(Preloader.java:175) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler.processRecordReload(PersistenceHandler.java:471) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler$1.doLoad(PersistenceHandler.java:447) jvm 1 | ... 71 more {noformat} {noformat} Message: TDS Protocol error: Invalid packet type 0x4 jvm 1 | Caused by: java.sql.SQLException: TDS Protocol error: Invalid packet type 0x4 jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2314) jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.getNextRow(TdsCore.java:764) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:593) jvm 1 | at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.loadData(Preloader.java:142) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler$3.doLoad(PersistenceHandler.java:592) jvm 1 | ... 111 more jvm 1 | Caused by: net.sourceforge.jtds.jdbc.ProtocolException: Invalid packet type 0x4 jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2301) jvm 1 | ... 116 more {noformat}

    Apache's JIRA Issue Tracker | 5 years ago | Dave Oxley
    java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed.
  2. 0

    Under high load commons-dbcp (or commons-pool) exhibits thread safety issues and begins throwing various exceptions. I don't yet know the cause of the issue but it looks like a connection maybe handed out to multiple threads concurrently. Here's a few examples of the exceptions we are getting: {noformat} jvm 1 | Caused by: java.sql.SQLException: Attempted to use PooledConnection after closed() was called. jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.assertOpen(PooledConnectionImpl.java:163) jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:174) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:768) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:676) jvm 1 | at uk.co.webessence.kernel.database.DriverAdapterConnectionPool.acquireConnection(DriverAdapterConnectionPool.java:101) jvm 1 | ... 94 more {noformat} {noformat} jvm 1 | Caused by: java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed. jvm 1 | at org.apache.commons.dbcp.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:179) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:768) jvm 1 | at org.apache.commons.dbcp.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:676) jvm 1 | at uk.co.webessence.kernel.database.DriverAdapterConnectionPool.acquireConnection(DriverAdapterConnectionPool.java:101) jvm 1 | ... 77 more {noformat} {noformat} jvm 1 | Caused by: java.sql.SQLException: Invalid state, the ResultSet object is closed. jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.checkOpen(JtdsResultSet.java:299) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.getColumn(JtdsResultSet.java:273) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.getObject(JtdsResultSet.java:840) jvm 1 | at org.apache.commons.dbcp.DelegatingResultSet.getObject(DelegatingResultSet.java:325) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.getDataArray(Preloader.java:428) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.processSingleRow(Preloader.java:175) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler.processRecordReload(PersistenceHandler.java:471) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler$1.doLoad(PersistenceHandler.java:447) jvm 1 | ... 71 more {noformat} {noformat} Message: TDS Protocol error: Invalid packet type 0x4 jvm 1 | Caused by: java.sql.SQLException: TDS Protocol error: Invalid packet type 0x4 jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2314) jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.getNextRow(TdsCore.java:764) jvm 1 | at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:593) jvm 1 | at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207) jvm 1 | at uk.co.webessence.kernel.persistence.Preloader.loadData(Preloader.java:142) jvm 1 | at uk.co.webessence.kernel.persistence.PersistenceHandler$3.doLoad(PersistenceHandler.java:592) jvm 1 | ... 111 more jvm 1 | Caused by: net.sourceforge.jtds.jdbc.ProtocolException: Invalid packet type 0x4 jvm 1 | at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2301) jvm 1 | ... 116 more {noformat}

    Apache's JIRA Issue Tracker | 5 years ago | Dave Oxley
    java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed.
  3. 0

    When testOnBorrow is set on a SharedPoolDataSource with connections on MS SQL Server, the first (and all subsequent) connections retrieved from the pool get the exception in this stack trace as a result of executing Connection.isValid() in the validation on borrow. java -cp ".;$CLASSPATH" Dbcp2TestOnBorrowFailure 'jdbc:sqlserver://myserver.example.com;databasename=mydb' myuser mypassword Iteration: 0 PooledConnection was reused, withoutits previous Connection being closed. java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed. at org.apache.commons.dbcp2.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:183) at org.apache.commons.dbcp2.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:951) at Dbcp2TestOnBorrowFailure.getConnection(Dbcp2TestOnBorrowFailure.java:30) at Dbcp2TestOnBorrowFailure.main(Dbcp2TestOnBorrowFailure.java:42) The code for this example is pasted in here (the preview shows that the code indentation and formatting is lost -- sorry): ---------------------------------------------------------------------- import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS; import org.apache.commons.dbcp2.datasources.SharedPoolDataSource; public final class Dbcp2TestOnBorrowFailure { private static final int MAX_CONNECTIONS = 3; private final SharedPoolDataSource poolDataSource = new SharedPoolDataSource(); public Dbcp2TestOnBorrowFailure(String jdbcUrl) { DriverAdapterCPDS cpds = new DriverAdapterCPDS(); cpds.setUrl(jdbcUrl); poolDataSource.setConnectionPoolDataSource(cpds); poolDataSource.setMaxTotal(MAX_CONNECTIONS); poolDataSource.setMaxConnLifetimeMillis(300000); poolDataSource.setDefaultMaxWaitMillis(1000); poolDataSource.setDefaultAutoCommit(Boolean.TRUE); poolDataSource.setDefaultTestOnBorrow(true); poolDataSource.setValidationQueryTimeout(3000); } public Connection getConnection(String user, String pwd) throws Exception { return poolDataSource.getConnection(user, pwd); } public static void main(String[] args) { if (args.length != 3) { usage(); } Dbcp2TestOnBorrowFailure poolFailure = new Dbcp2TestOnBorrowFailure(args[0]); for (int i = 0; i < 10; ++i) { System.err.println("Iteration: " + i); Connection conn = null; try { conn = poolFailure.getConnection(args[1], args[2]); conn.close(); conn = null; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); System.exit(88); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } } } private static void usage() { System.err.println("Usage: java -cp ... Dbcp2TestOnBorrowFailure jdbcUrl user password"); System.exit(77); } }

    Apache's JIRA Issue Tracker | 2 years ago | Jeff Greif (work)
    java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed.
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    When testOnBorrow is set on a SharedPoolDataSource with connections on MS SQL Server, the first (and all subsequent) connections retrieved from the pool get the exception in this stack trace as a result of executing Connection.isValid() in the validation on borrow. java -cp ".;$CLASSPATH" Dbcp2TestOnBorrowFailure 'jdbc:sqlserver://myserver.example.com;databasename=mydb' myuser mypassword Iteration: 0 PooledConnection was reused, withoutits previous Connection being closed. java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed. at org.apache.commons.dbcp2.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:183) at org.apache.commons.dbcp2.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:951) at Dbcp2TestOnBorrowFailure.getConnection(Dbcp2TestOnBorrowFailure.java:30) at Dbcp2TestOnBorrowFailure.main(Dbcp2TestOnBorrowFailure.java:42) The code for this example is pasted in here (the preview shows that the code indentation and formatting is lost -- sorry): ---------------------------------------------------------------------- import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS; import org.apache.commons.dbcp2.datasources.SharedPoolDataSource; public final class Dbcp2TestOnBorrowFailure { private static final int MAX_CONNECTIONS = 3; private final SharedPoolDataSource poolDataSource = new SharedPoolDataSource(); public Dbcp2TestOnBorrowFailure(String jdbcUrl) { DriverAdapterCPDS cpds = new DriverAdapterCPDS(); cpds.setUrl(jdbcUrl); poolDataSource.setConnectionPoolDataSource(cpds); poolDataSource.setMaxTotal(MAX_CONNECTIONS); poolDataSource.setMaxConnLifetimeMillis(300000); poolDataSource.setDefaultMaxWaitMillis(1000); poolDataSource.setDefaultAutoCommit(Boolean.TRUE); poolDataSource.setDefaultTestOnBorrow(true); poolDataSource.setValidationQueryTimeout(3000); } public Connection getConnection(String user, String pwd) throws Exception { return poolDataSource.getConnection(user, pwd); } public static void main(String[] args) { if (args.length != 3) { usage(); } Dbcp2TestOnBorrowFailure poolFailure = new Dbcp2TestOnBorrowFailure(args[0]); for (int i = 0; i < 10; ++i) { System.err.println("Iteration: " + i); Connection conn = null; try { conn = poolFailure.getConnection(args[1], args[2]); conn.close(); conn = null; } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); System.exit(88); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } } } private static void usage() { System.err.println("Usage: java -cp ... Dbcp2TestOnBorrowFailure jdbcUrl user password"); System.exit(77); } }

    Apache's JIRA Issue Tracker | 2 years ago | Jeff Greif (work)
    java.sql.SQLException: PooledConnection was reused, withoutits previous Connection being closed.

    Root Cause Analysis

    1. java.sql.SQLException

      PooledConnection was reused, withoutits previous Connection being closed.

      at org.apache.commons.dbcp2.cpdsadapter.PooledConnectionImpl.getConnection()
    2. Apache Commons DBCP
      InstanceKeyDataSource.getConnection
      1. org.apache.commons.dbcp2.cpdsadapter.PooledConnectionImpl.getConnection(PooledConnectionImpl.java:184)
      2. org.apache.commons.dbcp2.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:778)
      3. org.apache.commons.dbcp2.datasources.InstanceKeyDataSource.getConnection(InstanceKeyDataSource.java:685)
      3 frames
    3. com.daveoxley.dbcpbug
      App$TestThread.run
      1. com.daveoxley.dbcpbug.App$TestThread.run(App.java:143)
      1 frame
    4. Java RT
      Thread.run
      1. java.lang.Thread.run(Thread.java:662)
      1 frame