java connection pool and autocommit status

1.4k views Asked by At

Quick question that I can't find an answer to. If you have a connection pool where some connections may have been set to autocommit false. If those are returned and retrieved from the pool. Is autocommit set back to true (the default)? Or is it that I could have some connections in one mode or the other?

1

There are 1 answers

1
Mark Rotteveel On BEST ANSWER

A properly behaving connection pool should always return connections in the same clean state. Which, assuming defaults specified in the JDBC specification, would be that a connection has no outstanding transaction and is in auto-commit mode.

However, historically this has been a bit of a mess, and some connection pools will not rollback outstanding transactions when a connection is returned to the pool, nor reset the current auto-commit mode, or they need to be explicitly configured to do so. Some do this for performance reasons, but from a pure JDBC standpoint, such behaviour is incorrect.

It is always advisable to check the documentation and verify (test) the behaviour of a data source, and not blindly rely on assumptions.

DBCP will by default call setAutoCommit(true) when a connection is returned (setting enableAutoCommitOnReturn) and rollback outstanding transactions (setting rollbackOnReturn). See also BasicDataSource Configuration Parameters.