61
61
import java .util .concurrent .Executor ;
62
62
import java .util .concurrent .TimeUnit ;
63
63
import java .util .concurrent .TimeoutException ;
64
- import java .util .concurrent .atomic .AtomicBoolean ;
65
64
import java .util .concurrent .atomic .AtomicInteger ;
66
65
import java .util .logging .Level ;
67
66
import java .util .logging .Logger ;
@@ -152,7 +151,10 @@ public void removeListener(Runnable listener) {
152
151
}
153
152
}
154
153
155
- private final AtomicBoolean committing = new AtomicBoolean ();
154
+ private final Object committingLock = new Object ();
155
+
156
+ @ GuardedBy ("committingLock" )
157
+ private volatile boolean committing ;
156
158
157
159
@ GuardedBy ("lock" )
158
160
private volatile SettableApiFuture <Void > finishedAsyncOperations = SettableApiFuture .create ();
@@ -284,8 +286,15 @@ void commit() {
284
286
volatile ApiFuture <CommitResponse > commitFuture ;
285
287
286
288
ApiFuture <CommitResponse > commitAsync () {
287
- if (committing .getAndSet (true )) {
288
- throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
289
+ List <com .google .spanner .v1 .Mutation > mutationsProto = new ArrayList <>();
290
+ synchronized (committingLock ) {
291
+ if (committing ) {
292
+ throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
293
+ }
294
+ committing = true ;
295
+ if (!mutations .isEmpty ()) {
296
+ Mutation .toProto (mutations , mutationsProto );
297
+ }
289
298
}
290
299
final SettableApiFuture <CommitResponse > res = SettableApiFuture .create ();
291
300
final SettableApiFuture <Void > finishOps ;
@@ -311,11 +320,7 @@ ApiFuture<CommitResponse> commitAsync() {
311
320
finishOps = finishedAsyncOperations ;
312
321
}
313
322
}
314
- if (!mutations .isEmpty ()) {
315
- List <com .google .spanner .v1 .Mutation > mutationsProto = new ArrayList <>();
316
- Mutation .toProto (mutations , mutationsProto );
317
- builder .addAllMutations (mutationsProto );
318
- }
323
+ builder .addAllMutations (mutationsProto );
319
324
finishOps .addListener (
320
325
new CommitRunnable (res , finishOps , builder ), MoreExecutors .directExecutor ());
321
326
return res ;
@@ -608,10 +613,12 @@ public void onDone(boolean withBeginTransaction) {
608
613
609
614
@ Override
610
615
public void buffer (Mutation mutation ) {
611
- if (committing .get ()) {
612
- throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
616
+ synchronized (committingLock ) {
617
+ if (committing ) {
618
+ throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
619
+ }
620
+ mutations .add (checkNotNull (mutation ));
613
621
}
614
- mutations .add (checkNotNull (mutation ));
615
622
}
616
623
617
624
@ Override
@@ -625,11 +632,13 @@ public ApiFuture<Void> bufferAsync(Mutation mutation) {
625
632
626
633
@ Override
627
634
public void buffer (Iterable <Mutation > mutations ) {
628
- if (committing .get ()) {
629
- throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
630
- }
631
- for (Mutation mutation : mutations ) {
632
- this .mutations .add (checkNotNull (mutation ));
635
+ synchronized (committingLock ) {
636
+ if (committing ) {
637
+ throw new IllegalStateException (TRANSACTION_ALREADY_COMMITTED_MESSAGE );
638
+ }
639
+ for (Mutation mutation : mutations ) {
640
+ this .mutations .add (checkNotNull (mutation ));
641
+ }
633
642
}
634
643
}
635
644
0 commit comments