Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ ImmutableList<Savepoint> getSavepoints() {
return ImmutableList.copyOf(savepoints);
}

/** Clears all in-memory savepoints recorded during the current transaction attempt. */
void clearSavepoints() {
savepoints.clear();
}

@Override
public void savepoint(@Nonnull String name, @Nonnull Dialect dialect) {
if (dialect != Dialect.POSTGRESQL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.spanner.v1.DirectedReadOptions;
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import com.google.spanner.v1.RequestOptions;
Expand Down Expand Up @@ -1397,9 +1398,96 @@ public <T> T runTransaction(TransactionCallable<T> callable) {
}
}

private static final EndTransactionCallback NO_OP_END_TRANSACTION_CALLBACK =
new EndTransactionCallback() {
@Override
public void onSuccess() {}

@Override
public void onFailure() {}
};

/**
* Restores active transaction state on this connection and resets {@code retryUnitOfWork} before
* {@link TransactionRunnerImpl} executes a retry attempt.
*/
void resetForRetry(UnitOfWork retryUnitOfWork) {
retryUnitOfWork.resetForRetry();
this.currentUnitOfWork = retryUnitOfWork;
if (isBatchActive()) {
abortBatch();
}
if (this.currentUnitOfWork != null && this.currentUnitOfWork != retryUnitOfWork) {
closeUnitOfWorkAfterRunnerFailure(this.currentUnitOfWork);
}
this.connectionState.rollback();
this.statementTag = null;
this.transactionBeginMarked = true;
clearLastTransactionAndSetDefaultTransactionOptions(getDefaultIsolationLevel());
if (isAutocommit()) {
this.inTransaction = true;
}
// Re-apply local RETRY_ABORTS_INTERNALLY = false while currentUnitOfWork is null (cleared
// above) and isInTransaction() is true, before re-attaching retryUnitOfWork.
setLocalConnectionPropertyValue(RETRY_ABORTS_INTERNALLY, false);
try {
retryUnitOfWork.resetForRetry();
} finally {
this.currentUnitOfWork = retryUnitOfWork;
}
}

/**
* Cleans up connection transaction state and closes any active or kept-open aborted unit of work
* when {@link TransactionRunnerImpl} exits with a non-retryable failure or interruption.
*/
void rollbackAfterAbort(UnitOfWork transaction) {
if (isBatchActive()) {
try {
abortBatch();
} catch (Throwable ignored) {
// Best-effort batch cleanup so the primary failure is never masked.
}
}
UnitOfWork current = this.currentUnitOfWork;
this.connectionState.rollback();
this.statementTag = null;
this.transactionBeginMarked = false;
clearLastTransactionAndSetDefaultTransactionOptions(getDefaultIsolationLevel());
if (isAutocommit()) {
this.inTransaction = false;
}
closeUnitOfWorkAfterRunnerFailure(transaction);
if (current != null && current != transaction) {
closeUnitOfWorkAfterRunnerFailure(current);
}
}

private void closeUnitOfWorkAfterRunnerFailure(@Nullable UnitOfWork uow) {
if (uow instanceof AbstractMultiUseTransaction) {
boolean wasInterrupted = Thread.interrupted();
try {
// Drain any in-flight statement or commit task on the single-threaded statementExecutor so
// COMMITTING settles to COMMITTED, COMMIT_FAILED, or ABORTED before inspecting state.
try {
Uninterruptibles.getUninterruptibly(
((AbstractBaseUnitOfWork) uow).getStatementExecutor().submit(() -> null));
} catch (Throwable ignored) {
// Best-effort barrier; proceed to state check.
}
UnitOfWorkState state = uow.getState();
if (state == UnitOfWorkState.STARTED || state == UnitOfWorkState.ABORTED) {
try {
Uninterruptibles.getUninterruptibly(
uow.rollbackAsync(CallType.SYNC, NO_OP_END_TRANSACTION_CALLBACK));
} catch (Throwable ignored) {
// Best-effort rollback/close so the caller's primary exception is never masked.
}
}
} finally {
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
}
}
}

@Override
Expand Down Expand Up @@ -2334,6 +2422,7 @@ UnitOfWork createNewUnitOfWork(
getConnectionPropertyValue(DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE))
.setKeepTransactionAlive(getConnectionPropertyValue(KEEP_TRANSACTION_ALIVE))
.setRetryAbortsInternally(getConnectionPropertyValue(RETRY_ABORTS_INTERNALLY))
.setKeepAbortedTransactionOpen(this.transactionRunnerActive)
.setSavepointSupport(getConnectionPropertyValue(SAVEPOINT_SUPPORT))
.setReturnCommitStats(getConnectionPropertyValue(RETURN_COMMIT_STATS))
.setMaxCommitDelay(getConnectionPropertyValue(MAX_COMMIT_DELAY))
Expand Down
Loading
Loading