@@ -143,69 +143,74 @@ private WriteConcern getWriteConcern(@Nullable final TimeoutContext timeoutConte
143143
144144 @ Override
145145 public Publisher <Void > commitTransaction () {
146- if (transactionState == TransactionState .ABORTED ) {
147- throw new IllegalStateException ("Cannot call commitTransaction after calling abortTransaction" );
148- }
149- if (transactionState == TransactionState .NONE ) {
150- throw new IllegalStateException ("There is no transaction started" );
151- }
152- if (!messageSentInCurrentTransaction ) {
153- cleanupTransaction (TransactionState .COMMITTED );
154- return Mono .create (MonoSink ::success );
155- } else {
156- ReadConcern readConcern = transactionOptions .getReadConcern ();
157- if (readConcern == null ) {
158- throw new MongoInternalException ("Invariant violated. Transaction options read concern can not be null" );
146+ return Mono .defer (() -> {
147+ if (transactionState == TransactionState .ABORTED ) {
148+ return Mono .error (new IllegalStateException ("Cannot call commitTransaction after calling abortTransaction" ));
159149 }
160- boolean alreadyCommitted = commitInProgress || transactionState == TransactionState .COMMITTED ;
161- commitInProgress = true ;
162- resetTimeout ();
163- TimeoutContext timeoutContext = getTimeoutContext ();
164- WriteConcern writeConcern = assertNotNull (getWriteConcern (timeoutContext ));
165- return executor
166- .execute (
167- new CommitTransactionOperation (writeConcern , alreadyCommitted )
168- .recoveryToken (getRecoveryToken ()), readConcern , this )
169- .doOnTerminate (() -> {
170- commitInProgress = false ;
171- transactionState = TransactionState .COMMITTED ;
172- })
173- .doOnError (MongoException .class , this ::clearTransactionContextOnError );
174- }
150+ if (transactionState == TransactionState .NONE ) {
151+ return Mono .error (new IllegalStateException ("There is no transaction started" ));
152+ }
153+ if (!messageSentInCurrentTransaction ) {
154+ cleanupTransaction (TransactionState .COMMITTED );
155+ return Mono .create (MonoSink ::success );
156+ } else {
157+ ReadConcern readConcern = transactionOptions .getReadConcern ();
158+ if (readConcern == null ) {
159+ return Mono .error (new MongoInternalException ("Invariant violated. Transaction options read concern can not be null" ));
160+ }
161+ boolean alreadyCommitted = commitInProgress || transactionState == TransactionState .COMMITTED ;
162+ commitInProgress = true ;
163+ resetTimeout ();
164+ TimeoutContext timeoutContext = getTimeoutContext ();
165+ WriteConcern writeConcern = assertNotNull (getWriteConcern (timeoutContext ));
166+ return executor
167+ .execute (
168+ new CommitTransactionOperation (writeConcern , alreadyCommitted )
169+ .recoveryToken (getRecoveryToken ()), readConcern , this )
170+ .doOnTerminate (() -> {
171+ commitInProgress = false ;
172+ transactionState = TransactionState .COMMITTED ;
173+ })
174+ .doOnError (MongoException .class , this ::clearTransactionContextOnError );
175+ }
176+ });
175177 }
176178
179+
177180 @ Override
178181 public Publisher <Void > abortTransaction () {
179- if (transactionState == TransactionState .ABORTED ) {
180- throw new IllegalStateException ("Cannot call abortTransaction twice" );
181- }
182- if (transactionState == TransactionState .COMMITTED ) {
183- throw new IllegalStateException ("Cannot call abortTransaction after calling commitTransaction" );
184- }
185- if (transactionState == TransactionState .NONE ) {
186- throw new IllegalStateException ("There is no transaction started" );
187- }
188- if (!messageSentInCurrentTransaction ) {
189- cleanupTransaction (TransactionState .ABORTED );
190- return Mono .create (MonoSink ::success );
191- } else {
192- ReadConcern readConcern = transactionOptions .getReadConcern ();
193- if (readConcern == null ) {
194- throw new MongoInternalException ("Invariant violated. Transaction options read concern can not be null" );
182+ return Mono .defer (() -> {
183+ if (transactionState == TransactionState .ABORTED ) {
184+ throw new IllegalStateException ("Cannot call abortTransaction twice" );
195185 }
196-
197- resetTimeout ();
198- TimeoutContext timeoutContext = getTimeoutContext ();
199- WriteConcern writeConcern = assertNotNull (getWriteConcern (timeoutContext ));
200- return executor
201- .execute (new AbortTransactionOperation (writeConcern )
202- .recoveryToken (getRecoveryToken ()), readConcern , this )
203- .onErrorResume (Throwable .class , (e ) -> Mono .empty ())
204- .doOnTerminate (() -> {
205- clearTransactionContext ();
206- cleanupTransaction (TransactionState .ABORTED );
207- });
208- }
186+ if (transactionState == TransactionState .COMMITTED ) {
187+ throw new IllegalStateException ("Cannot call abortTransaction after calling commitTransaction" );
188+ }
189+ if (transactionState == TransactionState .NONE ) {
190+ throw new IllegalStateException ("There is no transaction started" );
191+ }
192+ if (!messageSentInCurrentTransaction ) {
193+ cleanupTransaction (TransactionState .ABORTED );
194+ return Mono .create (MonoSink ::success );
195+ } else {
196+ ReadConcern readConcern = transactionOptions .getReadConcern ();
197+ if (readConcern == null ) {
198+ throw new MongoInternalException ("Invariant violated. Transaction options read concern can not be null" );
199+ }
200+
201+ resetTimeout ();
202+ TimeoutContext timeoutContext = getTimeoutContext ();
203+ WriteConcern writeConcern = assertNotNull (getWriteConcern (timeoutContext ));
204+ return executor
205+ .execute (new AbortTransactionOperation (writeConcern )
206+ .recoveryToken (getRecoveryToken ()), readConcern , this )
207+ .onErrorResume (Throwable .class , (e ) -> Mono .empty ())
208+ .doOnTerminate (() -> {
209+ clearTransactionContext ();
210+ cleanupTransaction (TransactionState .ABORTED );
211+ });
212+ }
213+ });
209214 }
210215
211216 private void clearTransactionContextOnError (final MongoException e ) {
0 commit comments