140
140
import com .google .spanner .executor .v1 .SpannerAsyncActionResponse ;
141
141
import com .google .spanner .executor .v1 .StartBatchTransactionAction ;
142
142
import com .google .spanner .executor .v1 .StartTransactionAction ;
143
+ import com .google .spanner .executor .v1 .TransactionExecutionOptions ;
143
144
import com .google .spanner .executor .v1 .UpdateCloudBackupAction ;
144
145
import com .google .spanner .executor .v1 .UpdateCloudDatabaseDdlAction ;
145
146
import com .google .spanner .executor .v1 .UpdateCloudInstanceAction ;
@@ -227,13 +228,16 @@ private static class ReadWriteTransaction {
227
228
private Mode finishMode ;
228
229
private SpannerException error ;
229
230
private final String transactionSeed ;
231
+ private final boolean optimistic ;
230
232
// Set to true when the transaction runner completed, one of these three could happen: runner
231
233
// committed, abandoned or threw an error.
232
234
private boolean runnerCompleted ;
233
235
234
- public ReadWriteTransaction (DatabaseClient dbClient , String transactionSeed ) {
236
+ public ReadWriteTransaction (
237
+ DatabaseClient dbClient , String transactionSeed , boolean optimistic ) {
235
238
this .dbClient = dbClient ;
236
239
this .transactionSeed = transactionSeed ;
240
+ this .optimistic = optimistic ;
237
241
this .runnerCompleted = false ;
238
242
}
239
243
@@ -318,7 +322,10 @@ public void startRWTransaction() throws Exception {
318
322
Runnable runnable =
319
323
() -> {
320
324
try {
321
- runner = dbClient .readWriteTransaction ();
325
+ runner =
326
+ optimistic
327
+ ? dbClient .readWriteTransaction (Options .optimisticLock ())
328
+ : dbClient .readWriteTransaction ();
322
329
LOGGER .log (Level .INFO , String .format ("Ready to run callable %s\n " , transactionSeed ));
323
330
runner .run (callable );
324
331
transactionSucceeded (runner .getCommitTimestamp ().toProto ());
@@ -537,7 +544,8 @@ public synchronized void startReadOnlyTxn(
537
544
}
538
545
539
546
/** Start a read-write transaction. */
540
- public synchronized void startReadWriteTxn (DatabaseClient dbClient , Metadata metadata )
547
+ public synchronized void startReadWriteTxn (
548
+ DatabaseClient dbClient , Metadata metadata , TransactionExecutionOptions options )
541
549
throws Exception {
542
550
if ((rwTxn != null ) || (roTxn != null ) || (batchTxn != null )) {
543
551
throw SpannerExceptionFactory .newSpannerException (
@@ -548,7 +556,7 @@ public synchronized void startReadWriteTxn(DatabaseClient dbClient, Metadata met
548
556
String .format (
549
557
"There's no active transaction, safe to create rwTxn: %s\n " , getTransactionSeed ()));
550
558
this .metadata = metadata ;
551
- rwTxn = new ReadWriteTransaction (dbClient , transactionSeed );
559
+ rwTxn = new ReadWriteTransaction (dbClient , transactionSeed , options . getOptimistic () );
552
560
LOGGER .log (
553
561
Level .INFO ,
554
562
String .format (
@@ -2246,7 +2254,7 @@ private Status executeStartTxn(
2246
2254
Level .INFO ,
2247
2255
"Starting read-write transaction %s\n " ,
2248
2256
executionContext .getTransactionSeed ());
2249
- executionContext .startReadWriteTxn (dbClient , metadata );
2257
+ executionContext .startReadWriteTxn (dbClient , metadata , action . getExecutionOptions () );
2250
2258
}
2251
2259
executionContext .setDatabaseClient (dbClient );
2252
2260
executionContext .initReadState ();
0 commit comments