in src/main/java/com/google/cloud/spanner/pgadapter/statements/BackendConnection.java [1228:1281]
private void maybeBeginImplicitTransaction(int index, boolean isSync) {
if (connectionState != ConnectionState.IDLE) {
return;
}
// Only start an implicit transaction if we have more than one statement left. Otherwise, just
// let the Spanner connection execute the statement in auto-commit mode.
if (isSync && index == bufferedStatements.size() - 1) {
return;
}
// Don't start an implicit transaction if this is already a transaction statement.
if (isTransactionStatement(index)) {
return;
}
// No need to start a transaction for DDL or client side statements.
if (bufferedStatements.get(index).parsedStatement.getType() == StatementType.DDL
|| bufferedStatements.get(index).parsedStatement.getType() == StatementType.CLIENT_SIDE) {
return;
}
// If there are only DML statements left, those can be executed as an auto-commit dml batch.
if (isSync && hasOnlyDmlStatementsAfter(index)) {
return;
}
// Do not start a transaction if a batch is already active.
if (spannerConnection.isDdlBatchActive() || spannerConnection.isDmlBatchActive()) {
return;
}
// Do not start an implicit transaction if all that is in the buffer is a DESCRIBE and an
// EXECUTE message for the same statement.
if (isSync
&& bufferedStatements.size() == 2
&& bufferedStatements.get(0) instanceof Execute
&& bufferedStatements.get(1) instanceof Execute
&& ((Execute) bufferedStatements.get(0)).analyze
&& !((Execute) bufferedStatements.get(1)).analyze
&& bufferedStatements
.get(0)
.statement
.getSql()
.equals(bufferedStatements.get(1).statement.getSql())) {
return;
}
// We need to start an implicit transaction.
// Check if a read-only transaction suffices.
spannerConnection.beginTransaction();
if (isSync && !hasUpdateStatementsAfter(index)) {
spannerConnection.setTransactionMode(
com.google.cloud.spanner.connection.TransactionMode.READ_ONLY_TRANSACTION);
}
transactionMode = TransactionMode.IMPLICIT;
connectionState = ConnectionState.TRANSACTION;
currentTransactionId = UUID.randomUUID();
}