17
17
18
18
import io .r2dbc .spi .Connection ;
19
19
import io .r2dbc .spi .ConnectionFactory ;
20
+ import reactor .core .publisher .Mono ;
21
+ import reactor .util .function .Tuple2 ;
22
+ import reactor .util .function .Tuples ;
23
+
20
24
import org .apache .commons .logging .Log ;
21
25
import org .apache .commons .logging .LogFactory ;
22
26
import org .reactivestreams .Publisher ;
27
+
23
28
import org .springframework .core .Ordered ;
24
29
import org .springframework .dao .DataAccessResourceFailureException ;
25
30
import org .springframework .lang .Nullable ;
26
31
import org .springframework .transaction .NoTransactionException ;
27
32
import org .springframework .transaction .reactive .TransactionSynchronization ;
28
33
import org .springframework .transaction .reactive .TransactionSynchronizationManager ;
29
34
import org .springframework .util .Assert ;
30
- import reactor .core .publisher .Mono ;
31
- import reactor .util .function .Tuple2 ;
32
- import reactor .util .function .Tuples ;
33
35
34
36
/**
35
37
* Helper class that provides static methods for obtaining R2DBC Connections from a
@@ -50,8 +52,7 @@ public abstract class ConnectionFactoryUtils {
50
52
51
53
private static final Log logger = LogFactory .getLog (ConnectionFactoryUtils .class );
52
54
53
- private ConnectionFactoryUtils () {
54
- }
55
+ private ConnectionFactoryUtils () {}
55
56
56
57
/**
57
58
* Obtain a {@link io.r2dbc.spi.Connection} from the given {@link io.r2dbc.spi.ConnectionFactory}. Translates
@@ -85,7 +86,7 @@ public static Mono<Tuple2<Connection, ConnectionFactory>> doGetConnection(Connec
85
86
86
87
Assert .notNull (connectionFactory , "ConnectionFactory must not be null!" );
87
88
88
- return TransactionSynchronizationManager .currentTransaction ().flatMap (synchronizationManager -> {
89
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (synchronizationManager -> {
89
90
90
91
ConnectionHolder conHolder = (ConnectionHolder ) synchronizationManager .getResource (connectionFactory );
91
92
if (conHolder != null && (conHolder .hasConnection () || conHolder .isSynchronizedWithTransaction ())) {
@@ -180,8 +181,7 @@ private static Mono<Tuple2<Connection, ConnectionFactory>> obtainConnection(
180
181
return Tuples .of (it , connectionFactory );
181
182
}));
182
183
183
- return Mono .justOrEmpty (resource )
184
- .flatMap (ConnectionFactoryUtils ::createConnection )
184
+ return Mono .justOrEmpty (resource ).flatMap (ConnectionFactoryUtils ::createConnection )
185
185
.switchIfEmpty (attachNewConnection );
186
186
}
187
187
@@ -212,7 +212,7 @@ private static Mono<Connection> fetchConnection(ConnectionFactory connectionFact
212
212
*
213
213
* @param con the {@link io.r2dbc.spi.Connection} to close if necessary.
214
214
* @param connectionFactory the {@link ConnectionFactory} that the Connection was obtained from (may be
215
- * {@literal null}).
215
+ * {@literal null}).
216
216
* @see #getConnection
217
217
*/
218
218
public static Mono <Void > releaseConnection (@ Nullable io .r2dbc .spi .Connection con ,
@@ -228,13 +228,13 @@ public static Mono<Void> releaseConnection(@Nullable io.r2dbc.spi.Connection con
228
228
*
229
229
* @param con the {@link io.r2dbc.spi.Connection} to close if necessary.
230
230
* @param connectionFactory the {@link ConnectionFactory} that the Connection was obtained from (may be
231
- * {@literal null}).
231
+ * {@literal null}).
232
232
* @see #doGetConnection
233
233
*/
234
234
public static Mono <Void > doReleaseConnection (@ Nullable io .r2dbc .spi .Connection con ,
235
235
@ Nullable ConnectionFactory connectionFactory ) {
236
236
237
- return TransactionSynchronizationManager .currentTransaction ().flatMap (it -> {
237
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (it -> {
238
238
239
239
ConnectionHolder conHolder = (ConnectionHolder ) it .getResource (connectionFactory );
240
240
if (conHolder != null && connectionEquals (conHolder , con )) {
@@ -302,7 +302,7 @@ public static Mono<Void> doCloseConnection(Connection connection, @Nullable Conn
302
302
* {@link reactor.util.context.Context}.
303
303
*
304
304
* @throws NoTransactionException if no active {@link ReactiveTransactionSynchronization} is associated with the
305
- * current subscription.
305
+ * current subscription.
306
306
* @see Mono#subscriberContext()
307
307
* @see ReactiveTransactionSynchronization
308
308
*/
@@ -319,7 +319,7 @@ public static Mono<ReactiveTransactionSynchronization> currentReactiveTransactio
319
319
* {@link reactor.util.context.Context}.
320
320
*
321
321
* @throws NoTransactionException if no active {@link ReactiveTransactionSynchronization} is associated with the
322
- * current subscription.
322
+ * current subscription.
323
323
* @see Mono#subscriberContext()
324
324
* @see ReactiveTransactionSynchronization
325
325
*/
@@ -339,7 +339,7 @@ public static Mono<ReactiveTransactionSynchronization> currentActiveReactiveTran
339
339
*/
340
340
public static Mono <ConnectionFactory > currentConnectionFactory (ConnectionFactory connectionFactory ) {
341
341
342
- return TransactionSynchronizationManager .currentTransaction ()
342
+ return TransactionSynchronizationManager .forCurrentTransaction ()
343
343
.filter (TransactionSynchronizationManager ::isSynchronizationActive ).filter (it -> {
344
344
345
345
ConnectionHolder conHolder = (ConnectionHolder ) it .getResource (connectionFactory );
@@ -357,7 +357,7 @@ public static Mono<ConnectionFactory> currentConnectionFactory(ConnectionFactory
357
357
*
358
358
* @param conHolder the {@link ConnectionHolder} for the held Connection (potentially a proxy)
359
359
* @param passedInCon the {@link Connection} passed-in by the user (potentially a target {@link Connection} without
360
- * proxy)
360
+ * proxy)
361
361
* @return whether the given Connections are equal
362
362
* @see #getTargetConnection
363
363
*/
@@ -420,13 +420,13 @@ private static Mono<? extends ConnectionFactory> obtainDefaultConnectionFactory(
420
420
421
421
TransactionResources currentSynchronization = synchronization .getCurrentTransaction ();
422
422
return currentSynchronization .getResource (ConnectionFactory .class );
423
- }).switchIfEmpty (Mono .error (new DataAccessResourceFailureException (
424
- "Cannot extract ConnectionFactory from current TransactionContext!" )));
423
+ }).switchIfEmpty (Mono .error (
424
+ new DataAccessResourceFailureException ( "Cannot extract ConnectionFactory from current TransactionContext!" )));
425
425
}
426
426
427
427
/**
428
- * Create a {@link Connection} via the given {@link ConnectionFactory#create() factory} and return a {@link Tuple2} associating the
429
- * {@link Connection} with its creating {@link ConnectionFactory}.
428
+ * Create a {@link Connection} via the given {@link ConnectionFactory#create() factory} and return a {@link Tuple2}
429
+ * associating the {@link Connection} with its creating {@link ConnectionFactory}.
430
430
*
431
431
* @param factory must not be {@literal null}.
432
432
* @return never {@literal null}
@@ -437,8 +437,7 @@ private static Mono<Tuple2<Connection, ConnectionFactory>> createConnection(Conn
437
437
logger .debug ("Fetching resumed R2DBC Connection from ConnectionFactory" );
438
438
}
439
439
440
- return Mono .from (factory .create ())
441
- .map (connection -> Tuples .of (connection , factory ));
440
+ return Mono .from (factory .create ()).map (connection -> Tuples .of (connection , factory ));
442
441
}
443
442
444
443
/**
@@ -469,7 +468,7 @@ public int getOrder() {
469
468
public Mono <Void > suspend () {
470
469
if (this .holderActive ) {
471
470
472
- return TransactionSynchronizationManager .currentTransaction ().flatMap (it -> {
471
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (it -> {
473
472
474
473
it .unbindResource (this .connectionFactory );
475
474
if (this .connectionHolder .hasConnection () && !this .connectionHolder .isOpen ()) {
@@ -490,7 +489,7 @@ public Mono<Void> suspend() {
490
489
@ Override
491
490
public Mono <Void > resume () {
492
491
if (this .holderActive ) {
493
- return TransactionSynchronizationManager .currentTransaction ().doOnNext (it -> {
492
+ return TransactionSynchronizationManager .forCurrentTransaction ().doOnNext (it -> {
494
493
it .bindResource (this .connectionFactory , this .connectionHolder );
495
494
}).then ();
496
495
}
@@ -506,7 +505,7 @@ public Mono<Void> beforeCompletion() {
506
505
// to avoid issues with strict transaction implementations that expect
507
506
// the close call before transaction completion.
508
507
if (!this .connectionHolder .isOpen ()) {
509
- return TransactionSynchronizationManager .currentTransaction ().flatMap (it -> {
508
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (it -> {
510
509
511
510
it .unbindResource (this .connectionFactory );
512
511
this .holderActive = false ;
@@ -529,7 +528,7 @@ public Mono<Void> afterCompletion(int status) {
529
528
if (this .holderActive ) {
530
529
// The thread-bound ConnectionHolder might not be available anymore,
531
530
// since afterCompletion might get called from a different thread.
532
- return TransactionSynchronizationManager .currentTransaction ().flatMap (it -> {
531
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (it -> {
533
532
534
533
it .unbindResourceIfPossible (this .connectionFactory );
535
534
this .holderActive = false ;
0 commit comments