-
Notifications
You must be signed in to change notification settings - Fork 1.1k
spanner-jdbc: Step 21 - ReadWriteTransaction #5898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
spanner-jdbc: Step 21 - ReadWriteTransaction #5898
Conversation
| @Override | ||
| void checkValidTransaction() { | ||
| ConnectionPreconditions.checkState( | ||
| state == UnitOfWorkState.STARTED, "This transaction has status " + state.name()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would slightly change the precondition message to add acceptable status
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
...gle-cloud-spanner-jdbc/src/main/java/com/google/cloud/spanner/jdbc/ReadWriteTransaction.java
Show resolved
Hide resolved
| } | ||
|
|
||
| /** Create a pseudo COMMIT statement to allow it to be cancelled or time out. */ | ||
| private final ParsedStatement commitStatement = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, could you explain what this does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the following javadoc to the field:
/**
* Create a COMMIT statement to use with the {@link #commit()} method to allow it to be
* cancelled, time out or retried.
*
* {@link ReadWriteTransaction} uses the generic methods
* {@link #executeAsync(ParsedStatement, Callable)} and {@link #runWithRetry(Callable)} to
* allow statements to be cancelled, to timeout and to be retried. These methods require a
* {@link ParsedStatement} as input. When the {@link #commit()} method is called directly, we do
* not have a {@link ParsedStatement}, and the method uses this statement instead in order to use
* the same logic as the other statements.
*/| // In case the transaction manager didn't pick up the abort correctly, we'll rollback | ||
| // manually and do another try. This seems to be the case if the transaction was | ||
| // aborted and txContext.executeUpdate(Statement) is called. | ||
| // TODO: Check whether TransactionContextImpl.executeUpdate(Statement) handles |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need this TODO?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this related to a bug that was in the Spanner client library when the initial copy to the private repo was made. It has long been fixed (and if not, it will be picked up by a test case, as that was how it was found in the first place).
It's removed.
| for (RetriableStatement statement : statements) { | ||
| statement.retry(aborted); | ||
| } | ||
| successfulRetries++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be in the loop, or is it per batch of statements?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is per successful retry of an entire transaction, i.e. per batch of statements, so it should be outside of the loop.
53fbe74 to
bb61b31
Compare
| if (retryAbortsInternally) { | ||
| return asyncExecuteStatement( | ||
| statement, | ||
| new Callable<ResultSet>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor - can address in a future PR: let's extract out the Callable into a descriptive variable
* add ReadWriteTransaction * refactor inner classes to separate files * included allowed state to error msg * added javadoc to the COMMIT and RUN BATCH statements * removed todo no longer needed in public repo
Adds ReadWriteTransaction including internal retries if the transaction is aborted. All statements that are executed on a ReadWriteTransaction are recorded internally in the transaction, and a running SHA-256 checksum is kept based on all the query results that are returned to the user. If the transaction is aborted by Cloud Spanner, a retry is automatically started and deemed successful if: