From 45f9b934c31176f11a938e444629977f6cc4fd92 Mon Sep 17 00:00:00 2001 From: Dave Cramer Date: Wed, 8 Jun 2022 12:16:03 -0400 Subject: [PATCH] fix: added GROUP_STARTUP_PARAMETERS boolean property to determine whether or not to group startup parameters in a transaction or not fixes Issue 2423 pgbouncer cannot deal with transactions in statement pooling mode (#2425) * fix: added GROUP_STARTUP_PARAMETERS boolean property to determine whether or not to group startup parameters in a transaction or not fixes Issue #2425 pgbouncer cannot deal with transactions in statement pooling mode * unset environment variables before running tests --- appveyor.yml | 9 +++++++++ .../main/java/org/postgresql/PGProperty.java | 14 +++++++++++++ .../core/v3/ConnectionFactoryImpl.java | 4 ++-- .../postgresql/ds/common/BaseDataSource.java | 20 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 972dfdaaa0..17877474f5 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -76,6 +76,15 @@ build_script: test_script: - echo redirect escape ^> foo.bar - echo privilegedPassword=Password12!>c:\projects\pgjdbc\build.local.properties + - set PGDATABASE= + - set PGHOST= + - set PGPASSFILE= + - set PGPASSWORD= + - set PGPORT= + - set PGSERVICE= + - set PGSERVICEFILE= + - set PGSYSCONFDIR= + - set PGUSER= - gradlew test cache: diff --git a/pgjdbc/src/main/java/org/postgresql/PGProperty.java b/pgjdbc/src/main/java/org/postgresql/PGProperty.java index 797a325039..b683e68c37 100644 --- a/pgjdbc/src/main/java/org/postgresql/PGProperty.java +++ b/pgjdbc/src/main/java/org/postgresql/PGProperty.java @@ -223,6 +223,20 @@ public enum PGProperty { false, new String[] {"select", "callIfNoReturn", "call"}), + /** + * Group startup parameters in a transaction + * This is important in pool-by-transaction scenarios in order to make sure that all the statements + * reaches the same connection that is being initialized. All of the startup parameters will be wrapped + * in a transaction + * Note this is off by default as pgbouncer in statement mode + */ + GROUP_STARTUP_PARAMETERS( + "groupStartupParameters", + "false", + "This is important in pool-by-transaction scenarios in order to make sure that all " + + "the statements reaches the same connection that is being initialized." + ), + GSS_ENC_MODE( "gssEncMode", "allow", diff --git a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java index 6c81e8b9b2..0e10796863 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -863,7 +863,7 @@ private void runInitialQueries(QueryExecutor queryExecutor, Properties info) final int dbVersion = queryExecutor.getServerVersionNum(); - if (dbVersion >= ServerVersion.v9_0.getVersionNum()) { + if (PGProperty.GROUP_STARTUP_PARAMETERS.getBoolean(info) && dbVersion >= ServerVersion.v9_0.getVersionNum()) { SetupQueryRunner.run(queryExecutor, "BEGIN", false); } @@ -880,7 +880,7 @@ private void runInitialQueries(QueryExecutor queryExecutor, Properties info) SetupQueryRunner.run(queryExecutor, sql.toString(), false); } - if (dbVersion >= ServerVersion.v9_0.getVersionNum()) { + if (PGProperty.GROUP_STARTUP_PARAMETERS.getBoolean(info) && dbVersion >= ServerVersion.v9_0.getVersionNum()) { SetupQueryRunner.run(queryExecutor, "COMMIT", false); } } diff --git a/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java b/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java index 384d40fb11..8fc68cd926 100644 --- a/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java +++ b/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java @@ -1015,6 +1015,26 @@ public void setAssumeMinServerVersion(@Nullable String minVersion) { PGProperty.ASSUME_MIN_SERVER_VERSION.set(properties, minVersion); } + /** + * This is important in pool-by-transaction scenarios in order to make sure that all the statements + * reaches the same connection that is being initialized. If set then we will group the startup + * parameters in a transaction + * @return whether to group starup parameters or not + * @see PGProperty#GROUP_STARTUP_PARAMETERS + */ + public boolean getGroupStartupParameters() { + return PGProperty.GROUP_STARTUP_PARAMETERS.getBoolean(properties); + } + + /** + * + * @param groupStartupParameters whether to group startup Parameters in a transaction or not + * @see PGProperty#GROUP_STARTUP_PARAMETERS + */ + public void setGroupStartupParameters(boolean groupStartupParameters) { + PGProperty.GROUP_STARTUP_PARAMETERS.set(properties, groupStartupParameters); + } + /** * @return JAAS application name * @see PGProperty#JAAS_APPLICATION_NAME