Commons Dbcp
  1. Commons Dbcp
  2. DBCP-412

dbcp2.PoolableConnection.close raises NullPointerException

    Details

      Description

      I found a critical error while closing a PoolableConnection.
      Here's the code to reproduce the bug (largely copied from the example shown in the Apache DBCP site):

      PoolingDataSourceExample2.java
      public static void main(String[] args) throws Throwable {
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          DataSource dataSource = setupDataSource(jdbcUrl);
      
          Connection connection = null;
          PreparedStatement statement = null;
          ResultSet result = null;
      
          try {
              connection = dataSource.getConnection();
              statement = connection.prepareStatement("SELECT 1");
              result = statement.executeQuery();
          } finally {
              result.close();
              statement.close();
              connection.close();
          }
      }
      
      public static DataSource setupDataSource(String connectURI) {
          ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);
          PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
          ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
          PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
      
          return dataSource;
      }
      

      When the code tries to close the connection (in the final block), an exception is raised:

      PoolingDataSourceExample2.java
      Exception in thread "main" java.lang.NullPointerException
      	at org.apache.commons.dbcp2.PoolableConnection.close(PoolableConnection.java:151)
      	at org.apache.commons.dbcp2.DelegatingConnection.closeInternal(DelegatingConnection.java:235)
      	at org.apache.commons.dbcp2.DelegatingConnection.close(DelegatingConnection.java:218)
      	at org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:212)
      	at dbcp.PoolingDataSourceExample2.closeQuietly(PoolingDataSourceExample2.java:64)
      	at dbcp.PoolingDataSourceExample2.main(PoolingDataSourceExample2.java:43)

      As I can see, the problem is the "_pool" variable inside PoolableConnection but I could not find any solution.

      1. DBCP-412.patch
        3 kB
        Phil Steitz

        Activity

          People

          • Assignee:
            Unassigned
            Reporter:
            Davide Caroselli
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development