in src/main/java/com/google/cloud/spanner/pgadapter/statements/BackendConnection.java [1140:1219]
private void flush(boolean isSync) {
int index = 0;
try {
while (index < bufferedStatements.size()) {
BufferedStatement<?> bufferedStatement = bufferedStatements.get(index);
maybeBeginImplicitTransaction(index, isSync);
// Prepare the connection for executing a DDL statement. This could include committing the
// current transaction, depending on the settings for execute DDL in transactions.
if (bufferedStatement.parsedStatement.isDdl()) {
prepareExecuteDdl(bufferedStatement);
} else if (transactionMode == TransactionMode.DDL_BATCH && !isTransactionStatement(index)) {
// End the automatically created DDL batch and revert to an explicit transaction.
try {
spannerConnection.runBatch();
} catch (Exception exception) {
// Register the exception on the current statement, even though it was caused by a
// previous one.
bufferedStatements.get(index).result.setException(exception);
throw exception;
} finally {
transactionMode = TransactionMode.EXPLICIT;
}
spannerConnection.beginTransaction();
}
boolean canUseBatch = false;
if (!spannerConnection.isDdlBatchActive()
&& !spannerConnection.isDmlBatchActive()
&& bufferedStatement.isBatchingPossible()
&& index < (getStatementCount() - 1)) {
StatementType statementType = getStatementType(index);
// Do not start an explicit batch if the connection is already in auto-DML-batch mode.
if (!(statementType == StatementType.UPDATE && spannerConnection.isAutoBatchDml())) {
StatementType nextStatementType = getStatementType(index + 1);
canUseBatch = canBeBatchedTogether(statementType, nextStatementType);
}
}
if (canUseBatch) {
index += executeStatementsInBatch(index);
} else {
bufferedStatement.execute();
if (isBegin(index)) {
transactionMode = TransactionMode.EXPLICIT;
connectionState = ConnectionState.TRANSACTION;
currentTransactionId = UUID.randomUUID();
} else if (isCommit(index) || isRollback(index)) {
if (isCommit(index)) {
sessionState.commit();
} else {
sessionState.rollback();
}
closeAllPortals.run();
clearCurrentTransaction();
}
index++;
}
}
} catch (Exception exception) {
// The connection should not transition to the ABORTED state if a COMMIT or ROLLBACK fails.
if (isCommit(index) || isRollback(index)) {
clearCurrentTransaction();
} else {
connectionState = ConnectionState.ABORTED;
}
closeAllPortals.run();
sessionState.rollback();
if (spannerConnection.isInTransaction()) {
if (spannerConnection.isDmlBatchActive()) {
spannerConnection.abortBatch();
}
spannerConnection.setStatementTag(null);
spannerConnection.execute(ROLLBACK);
} else if (spannerConnection.isDdlBatchActive()) {
spannerConnection.abortBatch();
}
} finally {
bufferedStatements.clear();
}
}