commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bernd Eckenfels (JIRA)" <>
Subject [jira] [Commented] (DBCP-413) Closing a Connection does not close Statement objects created on that connection, by way of the close() method.
Date Thu, 10 Apr 2014 02:13:17 GMT


Bernd Eckenfels commented on DBCP-413:

I have used the follwing test code:

package test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;

public class DBCPTest

    public static void main(String[] args) throws SQLException
        DataSource ds = setupDataSource("jdbc:h2:target/testdb");
        Connection c = ds.getConnection();
        PreparedStatement ps = c.prepareStatement("SELECT 1 FROM DUAL");
        System.out.println("impls: connection=" + c.getClass() + " ps=" + ps.getClass());
        System.out.println("ps is closed? " + ps.isClosed());

    public static DataSource setupDataSource(String connectURI)
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
        ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
        PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
        return dataSource;

And with H2 database it returns true for isClosed() and it silently skips the close on the

impls: connection=class org.apache.commons.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
ps=class org.apache.commons.dbcp2.DelegatingPreparedStatement
ps is closed? true

I suggest the OP tries it with MSSQL and jTDS drivers.

> Closing a Connection does not close Statement objects created on that connection, by
way of the close() method.
> ---------------------------------------------------------------------------------------------------------------
>                 Key: DBCP-413
>                 URL:
>             Project: Commons Dbcp
>          Issue Type: Bug
>    Affects Versions: 1.4
>            Reporter: Mark W
>            Priority: Minor
> While using the MS SQL 2012 jdbc drivers with dbcp, I noticed that if you create a connection,
then create a preparedStatement from that connection, and close the connection object, PreparedStatement.isClosed()
will return false, but you will get a SQLException stating the statement has been closed if
you attempt to call any of the set or execute methods.
> From the JDBC spec:
> Connection.close
> An application calls the method Connection.close() to indicate that it has finished using
a connection. All Statement objects created from a given Connection object will be closed
when the close method for the Connection object is called.

This message was sent by Atlassian JIRA

View raw message