@@ -22,48 +22,43 @@ class PostgresConnectionPool {
2222 private static final String VALIDATION_QUERY = "SELECT 1" ;
2323 private static final Duration VALIDATION_QUERY_TIMEOUT = Duration .ofSeconds (5 );
2424
25- // data source for pools with auto-commits enabled
26- private final PoolingDataSource <PoolableConnection > regularDataSource ;
27- // data source for pools with auto-commits disabled. This is used for transactional operations
28- // that manage their own commit logic
29- private final PoolingDataSource <PoolableConnection > transactionalDataSource ;
25+ // Single pool with autoCommit=true by default. Transactional connections flip autoCommit
26+ // to false on borrow; DBCP2 resets it back when the connection is returned to the pool.
27+ private final PoolingDataSource <PoolableConnection > dataSource ;
3028
3129 PostgresConnectionPool (final PostgresConnectionConfig config ) {
32- this .regularDataSource = createPooledDataSource (config , true );
33- this .transactionalDataSource = createPooledDataSource (config , false );
30+ this .dataSource = createPooledDataSource (config );
3431 }
3532
3633 /**
37- * Get a connection from the regular pool with autoCommit=true. Use for read-only queries that
38- * don't need manual transaction management.
34+ * Get a connection with autoCommit=true (pool default) . Use for simple queries that don't need
35+ * manual transaction management.
3936 */
4037 public Connection getConnection () throws SQLException {
41- return regularDataSource .getConnection ();
38+ return dataSource .getConnection ();
4239 }
4340
4441 /**
45- * Get a connection from the transactional pool with autoCommit=false. Use for operations that
46- * require manual transaction management (commit/rollback).
42+ * Get a connection with autoCommit=false for manual transaction management (commit/rollback).
43+ * When the connection is closed/returned to the pool, DBCP2 automatically resets autoCommit back
44+ * to the pool default (true).
4745 */
4846 public Connection getTransactionalConnection () throws SQLException {
49- return transactionalDataSource .getConnection ();
47+ Connection conn = dataSource .getConnection ();
48+ conn .setAutoCommit (false );
49+ return conn ;
5050 }
5151
5252 public void close () {
5353 try {
54- regularDataSource .close ();
54+ dataSource .close ();
5555 } catch (final SQLException e ) {
56- log .warn ("Unable to close regular Postgres connection pool" , e );
57- }
58- try {
59- transactionalDataSource .close ();
60- } catch (final SQLException e ) {
61- log .warn ("Unable to close transactional Postgres connection pool" , e );
56+ log .warn ("Unable to close Postgres connection pool" , e );
6257 }
6358 }
6459
6560 private PoolingDataSource <PoolableConnection > createPooledDataSource (
66- final PostgresConnectionConfig config , final boolean autoCommit ) {
61+ final PostgresConnectionConfig config ) {
6762 final ConnectionFactory connectionFactory =
6863 new DriverManagerConnectionFactory (config .toConnectionString (), config .buildProperties ());
6964 final PoolableConnectionFactory poolableConnectionFactory =
@@ -73,7 +68,7 @@ private PoolingDataSource<PoolableConnection> createPooledDataSource(
7368
7469 final ConnectionPoolConfig poolConfig = config .connectionPoolConfig ();
7570 setPoolProperties (connectionPool , poolConfig );
76- setFactoryProperties (poolableConnectionFactory , connectionPool , autoCommit );
71+ setFactoryProperties (poolableConnectionFactory , connectionPool );
7772
7873 return new PoolingDataSource <>(connectionPool );
7974 }
@@ -100,13 +95,12 @@ private void setPoolProperties(
10095
10196 private void setFactoryProperties (
10297 PoolableConnectionFactory poolableConnectionFactory ,
103- GenericObjectPool <PoolableConnection > connectionPool ,
104- boolean autoCommit ) {
98+ GenericObjectPool <PoolableConnection > connectionPool ) {
10599 poolableConnectionFactory .setPool (connectionPool );
106100 poolableConnectionFactory .setValidationQuery (VALIDATION_QUERY );
107101 poolableConnectionFactory .setValidationQueryTimeout ((int ) VALIDATION_QUERY_TIMEOUT .toSeconds ());
108102 poolableConnectionFactory .setDefaultReadOnly (false );
109- poolableConnectionFactory .setDefaultAutoCommit (autoCommit );
103+ poolableConnectionFactory .setDefaultAutoCommit (true );
110104 poolableConnectionFactory .setDefaultTransactionIsolation (TRANSACTION_READ_COMMITTED );
111105 poolableConnectionFactory .setPoolStatements (false );
112106 }
0 commit comments