Skip to content

Commit 4181266

Browse files
committed
fix(bigquery-jdbc): add connection state and session reset hook for pooling
- Add package-private BigQueryConnection.resetForReuse() to close open statements, reset autoCommit to true, roll back active transactions without starting a follow-up transaction, abort driver-created sessions while preserving user-configured session_id properties, clear clientInfo and SQLWarnings, and restore holdability. - Collapse rollbackImpl into rollbackImpl(boolean startNextTransaction) and reuse closeOpenStatements() and accumulateException() in closeImpl(). - Add unit tests in BigQueryConnectionTest covering transaction rollback, driver-created vs. caller-configured session reset, property/statement cleanup, and exception accumulation.
1 parent 44dbda5 commit 4181266

2 files changed

Lines changed: 292 additions & 35 deletions

File tree

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java‎

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
255255
private boolean isReadOnlyTokenUsed = false;
256256
private final ExecutorService metadataExecutor;
257257
private final ExecutorService queryExecutor;
258+
private final int initialHoldability;
259+
private final SessionState initialSessionState;
258260

259261
/**
260262
* All session-scoped state, published as a single immutable snapshot.
@@ -383,11 +385,13 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
383385
this.unsupportedHTAPIFallback = ds.getUnsupportedHTAPIFallback();
384386
this.maxResults = ds.getMaxResults();
385387
Map<String, String> queryPropertiesMap = ds.getQueryProperties();
386-
this.sessionState.set(
388+
this.initialHoldability = this.holdability;
389+
this.initialSessionState =
387390
new SessionState(
388391
getSessionPropertyFromQueryProperties(queryPropertiesMap),
389392
convertMapToConnectionPropertiesList(queryPropertiesMap),
390-
false));
393+
false);
394+
this.sessionState.set(this.initialSessionState);
391395
this.enableWriteAPI = ds.getEnableWriteAPI();
392396
this.writeAPIActivationRowCount = ds.getSwaActivationRowCount();
393397
this.writeAPIAppendRowCount = ds.getSwaAppendRowCount();
@@ -1009,10 +1013,10 @@ public void rollback() throws SQLException {
10091013
"Cannot rollback without an active transaction. Please set setAutoCommit to false to"
10101014
+ " start a transaction.");
10111015
}
1012-
rollbackImpl();
1016+
rollbackImpl(!getAutoCommit());
10131017
}
10141018

1015-
private void rollbackImpl() throws SQLException {
1019+
private void rollbackImpl(boolean startNextTransaction) throws SQLException {
10161020
try {
10171021
QueryJobConfiguration transactionRollbackJobConfig =
10181022
QueryJobConfiguration.newBuilder("ROLLBACK TRANSACTION;")
@@ -1022,7 +1026,7 @@ private void rollbackImpl() throws SQLException {
10221026
Job rollbackJob = this.bigQuery.create(JobInfo.of(transactionRollbackJobConfig));
10231027
rollbackJob.waitFor();
10241028
this.transactionStarted = false;
1025-
if (!getAutoCommit()) {
1029+
if (startNextTransaction) {
10261030
beginTransaction();
10271031
}
10281032
} catch (BigQueryException ex) {
@@ -1033,6 +1037,77 @@ private void rollbackImpl() throws SQLException {
10331037
}
10341038
}
10351039

1040+
/**
1041+
* Resets mutable JDBC connection state, closes remaining open statements, rolls back any active
1042+
* transaction without starting a follow-up transaction, and aborts any driver-created BigQuery
1043+
* session before returning this physical connection for pooled reuse.
1044+
*/
1045+
void resetForReuse() throws SQLException {
1046+
try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(this.connectionId)) {
1047+
LOG.finer("++enter++");
1048+
try {
1049+
checkClosed();
1050+
SQLException exceptionToThrow = closeOpenStatements();
1051+
1052+
this.autoCommit = true;
1053+
if (this.transactionStarted) {
1054+
try {
1055+
rollbackImpl(false);
1056+
} catch (SQLException e) {
1057+
exceptionToThrow = accumulateException(exceptionToThrow, e);
1058+
} finally {
1059+
this.transactionStarted = false;
1060+
}
1061+
}
1062+
1063+
SessionState snapshot = this.sessionState.get();
1064+
if (snapshot.sessionInfo != null
1065+
&& snapshot.createdByDriver
1066+
&& !Thread.currentThread().isInterrupted()) {
1067+
try {
1068+
abortSession(snapshot);
1069+
} catch (SQLException e) {
1070+
exceptionToThrow = accumulateException(exceptionToThrow, e);
1071+
}
1072+
}
1073+
this.sessionState.set(this.initialSessionState);
1074+
1075+
this.clientInfo.clear();
1076+
this.sqlWarnings.clear();
1077+
this.holdability = this.initialHoldability;
1078+
1079+
if (exceptionToThrow != null) {
1080+
throw exceptionToThrow;
1081+
}
1082+
} finally {
1083+
LOG.finer("++exit++");
1084+
}
1085+
}
1086+
}
1087+
1088+
private SQLException closeOpenStatements() {
1089+
SQLException exceptionToThrow = null;
1090+
for (Statement statement : new ArrayList<>(this.openStatements)) {
1091+
try {
1092+
statement.close();
1093+
} catch (SQLException e) {
1094+
exceptionToThrow = accumulateException(exceptionToThrow, e);
1095+
}
1096+
}
1097+
this.openStatements.clear();
1098+
return exceptionToThrow;
1099+
}
1100+
1101+
private static SQLException accumulateException(SQLException current, SQLException next) {
1102+
if (current == null) {
1103+
return next;
1104+
}
1105+
if (current != next) {
1106+
current.addSuppressed(next);
1107+
}
1108+
return current;
1109+
}
1110+
10361111
@Override
10371112
public DatabaseMetaData getMetaData() throws SQLException {
10381113
if (databaseMetaData == null) {
@@ -1105,31 +1180,16 @@ private void closeImpl() throws SQLException {
11051180
this.queryExecutor.shutdown();
11061181
}
11071182

1108-
for (Statement statement : this.openStatements) {
1109-
try {
1110-
statement.close();
1111-
} catch (SQLException e) {
1112-
if (exceptionToThrow == null) {
1113-
exceptionToThrow = e;
1114-
} else {
1115-
exceptionToThrow.addSuppressed(e);
1116-
}
1117-
}
1118-
}
1119-
this.openStatements.clear();
1183+
exceptionToThrow = closeOpenStatements();
11201184

11211185
if (isTransactionStarted()) {
11221186
try {
11231187
// It looks like there's no need to start a new transaction after a rollback,
11241188
// but the commit behavior is preserved since close() may still fail before isClosed is
11251189
// updated.
1126-
rollbackImpl();
1190+
rollbackImpl(!getAutoCommit());
11271191
} catch (SQLException e) {
1128-
if (exceptionToThrow == null) {
1129-
exceptionToThrow = e;
1130-
} else {
1131-
exceptionToThrow.addSuppressed(e);
1132-
}
1192+
exceptionToThrow = accumulateException(exceptionToThrow, e);
11331193
}
11341194
}
11351195

@@ -1138,11 +1198,7 @@ private void closeImpl() throws SQLException {
11381198
try {
11391199
abortSession(snapshot);
11401200
} catch (SQLException e) {
1141-
if (exceptionToThrow == null) {
1142-
exceptionToThrow = e;
1143-
} else {
1144-
exceptionToThrow.addSuppressed(e);
1145-
}
1201+
exceptionToThrow = accumulateException(exceptionToThrow, e);
11461202
}
11471203
}
11481204

@@ -1205,13 +1261,11 @@ private void closeImpl() throws SQLException {
12051261
Thread.currentThread().interrupt();
12061262
}
12071263
if (terminationInterruptedException != null) {
1208-
BigQueryJdbcException interruptedException =
1209-
new BigQueryJdbcException("Interrupted during close", terminationInterruptedException);
1210-
if (exceptionToThrow == null) {
1211-
exceptionToThrow = interruptedException;
1212-
} else {
1213-
exceptionToThrow.addSuppressed(interruptedException);
1214-
}
1264+
exceptionToThrow =
1265+
accumulateException(
1266+
exceptionToThrow,
1267+
new BigQueryJdbcException(
1268+
"Interrupted during close", terminationInterruptedException));
12151269
}
12161270
} catch (ConcurrentModificationException ex) {
12171271
throw new BigQueryJdbcException("Concurrent modification during close", ex);

0 commit comments

Comments
 (0)