Skip to content

Implement AutoCloseable in JDBC connection classes - #338

Open
rgala wants to merge 1 commit into
IBM:mainfrom
rgala:jdbc_closeable
Open

Implement AutoCloseable in JDBC connection classes#338
rgala wants to merge 1 commit into
IBM:mainfrom
rgala:jdbc_closeable

Conversation

@rgala

@rgala rgala commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Added AutoCloseable implementation to JDBC connection classes so they can be used in try-with-resources blocks.

Signed-off-by: Rafał Gała <rafal.gala@hotmail.com>
@NicolaIsotta

Copy link
Copy Markdown
Contributor

Are you sure this actually needed? java.sql.Connection already implements AutoCloseable, while PooledConnection doesn't implement AutoCloseable by design (closing is handled by the pool manager)

@rgala

rgala commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

PooledCnnection class supports close() method which does not really close the connection but returns it to the pool. With AutoCloseable support it would be possible to obtain pooled connections in try-with-resources blocks and return them to the pool automatically without a need of calling close() method explicitly.

@NicolaIsotta

Copy link
Copy Markdown
Contributor

Sorry but I don't agree - even the PooledConnection's javadoc state that the close method shouldn't be called by the application.
image

@rgala

rgala commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Then why was this method implemented in AS400JDBCPooledConnection class?

Here is a sample code from IBM directly and as you can see, close method is called to return connection to the pool and AFAIK this is the only way to do it - https://www.ibm.com/docs/en/i/7.6.0?topic=classes-as400jdbcconnectionpool

I am confused with Oracle's documentation, it says that applications never call close method directly, but then:

When an application closes a connection, it calls the Connection method close. When connection pooling is being done, the connection pool manager is notified because it has registered itself as a ConnectionEventListener object using the ConnectionPool method addConnectionEventListener. The connection pool manager deactivates the handle to the PooledConnection object and returns the PooledConnection object to the pool of connections so that it can be used again. Thus, when an application closes its connection, the underlying physical connection is recycled rather than being closed.

@NicolaIsotta

Copy link
Copy Markdown
Contributor

The IBM example uses Connection, not PooledConnection.

PooledConnection are not meant to be used by final applications, that's the key point and the reason the class doesn't implement AutoCloseable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants