29
29
import io .r2dbc .spi .R2dbcTransientException ;
30
30
import io .r2dbc .spi .R2dbcTransientResourceException ;
31
31
import io .r2dbc .spi .Wrapped ;
32
- import org .apache .commons .logging .Log ;
33
- import org .apache .commons .logging .LogFactory ;
34
32
import reactor .core .publisher .Mono ;
35
33
36
34
import org .springframework .core .Ordered ;
@@ -69,11 +67,6 @@ public abstract class ConnectionFactoryUtils {
69
67
*/
70
68
public static final int CONNECTION_SYNCHRONIZATION_ORDER = 1000 ;
71
69
72
- private static final Log logger = LogFactory .getLog (ConnectionFactoryUtils .class );
73
-
74
-
75
- private ConnectionFactoryUtils () {}
76
-
77
70
78
71
/**
79
72
* Obtain a {@link Connection} from the given {@link ConnectionFactory}.
@@ -112,48 +105,34 @@ public static Mono<Connection> doGetConnection(ConnectionFactory connectionFacto
112
105
if (conHolder != null && (conHolder .hasConnection () || conHolder .isSynchronizedWithTransaction ())) {
113
106
conHolder .requested ();
114
107
if (!conHolder .hasConnection ()) {
115
-
116
- if (logger .isDebugEnabled ()) {
117
- logger .debug ("Fetching resumed R2DBC Connection from ConnectionFactory" );
118
- }
119
108
return fetchConnection (connectionFactory ).doOnNext (conHolder ::setConnection );
120
109
}
121
110
return Mono .just (conHolder .getConnection ());
122
111
}
123
112
// Else we either got no holder or an empty thread-bound holder here.
124
113
125
- if (logger .isDebugEnabled ()) {
126
- logger .debug ("Fetching R2DBC Connection from ConnectionFactory" );
127
- }
128
-
129
114
Mono <Connection > con = fetchConnection (connectionFactory );
130
-
131
115
if (synchronizationManager .isSynchronizationActive ()) {
132
-
133
- return con .flatMap (connection -> {
134
- return Mono .just (connection ).doOnNext (conn -> {
135
-
136
- // Use same Connection for further R2DBC actions within the transaction.
137
- // Thread-bound object will get removed by synchronization at transaction completion.
138
- ConnectionHolder holderToUse = conHolder ;
139
- if (holderToUse == null ) {
140
- holderToUse = new ConnectionHolder (conn );
141
- }
142
- else {
143
- holderToUse .setConnection (conn );
144
- }
145
- holderToUse .requested ();
146
- synchronizationManager
147
- .registerSynchronization (new ConnectionSynchronization (holderToUse , connectionFactory ));
148
- holderToUse .setSynchronizedWithTransaction (true );
149
- if (holderToUse != conHolder ) {
150
- synchronizationManager .bindResource (connectionFactory , holderToUse );
151
- }
152
- }) // Unexpected exception from external delegation call -> close Connection and rethrow.
153
- .onErrorResume (e -> releaseConnection (connection , connectionFactory ).then (Mono .error (e )));
154
- });
116
+ return con .flatMap (connection -> Mono .just (connection ).doOnNext (conn -> {
117
+ // Use same Connection for further R2DBC actions within the transaction.
118
+ // Thread-bound object will get removed by synchronization at transaction completion.
119
+ ConnectionHolder holderToUse = conHolder ;
120
+ if (holderToUse == null ) {
121
+ holderToUse = new ConnectionHolder (conn );
122
+ }
123
+ else {
124
+ holderToUse .setConnection (conn );
125
+ }
126
+ holderToUse .requested ();
127
+ synchronizationManager
128
+ .registerSynchronization (new ConnectionSynchronization (holderToUse , connectionFactory ));
129
+ holderToUse .setSynchronizedWithTransaction (true );
130
+ if (holderToUse != conHolder ) {
131
+ synchronizationManager .bindResource (connectionFactory , holderToUse );
132
+ }
133
+ }) // Unexpected exception from external delegation call -> close Connection and rethrow.
134
+ .onErrorResume (e -> releaseConnection (connection , connectionFactory ).then (Mono .error (e ))));
155
135
}
156
-
157
136
return con ;
158
137
}).onErrorResume (NoTransactionException .class , e -> Mono .from (connectionFactory .create ()));
159
138
}
@@ -356,9 +335,7 @@ public int getOrder() {
356
335
@ Override
357
336
public Mono <Void > suspend () {
358
337
if (this .holderActive ) {
359
- return TransactionSynchronizationManager .forCurrentTransaction ()
360
- .flatMap (synchronizationManager -> {
361
-
338
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (synchronizationManager -> {
362
339
synchronizationManager .unbindResource (this .connectionFactory );
363
340
if (this .connectionHolder .hasConnection () && !this .connectionHolder .isOpen ()) {
364
341
// Release Connection on suspend if the application doesn't keep
@@ -371,7 +348,6 @@ public Mono<Void> suspend() {
371
348
return Mono .empty ();
372
349
});
373
350
}
374
-
375
351
return Mono .empty ();
376
352
}
377
353
@@ -388,11 +364,10 @@ public Mono<Void> resume() {
388
364
389
365
@ Override
390
366
public Mono <Void > beforeCompletion () {
391
- // Release Connection early if the holder is not open anymore
392
- // (that is, not used by another resource
393
- // that has its own cleanup via transaction synchronization),
394
- // to avoid issues with strict transaction implementations that expect
395
- // the close call before transaction completion.
367
+ // Release Connection early if the holder is not open anymore (that is,
368
+ // not used by another resource that has its own cleanup via transaction
369
+ // synchronization), to avoid issues with strict transaction implementations
370
+ // that expect the close call before transaction completion.
396
371
if (!this .connectionHolder .isOpen ()) {
397
372
return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (synchronizationManager -> {
398
373
synchronizationManager .unbindResource (this .connectionFactory );
@@ -414,8 +389,7 @@ public Mono<Void> afterCompletion(int status) {
414
389
if (this .holderActive ) {
415
390
// The bound ConnectionHolder might not be available anymore,
416
391
// since afterCompletion might get called from a different thread.
417
- return TransactionSynchronizationManager .forCurrentTransaction ()
418
- .flatMap (synchronizationManager -> {
392
+ return TransactionSynchronizationManager .forCurrentTransaction ().flatMap (synchronizationManager -> {
419
393
synchronizationManager .unbindResourceIfPossible (this .connectionFactory );
420
394
this .holderActive = false ;
421
395
if (this .connectionHolder .hasConnection ()) {
@@ -426,7 +400,6 @@ public Mono<Void> afterCompletion(int status) {
426
400
return Mono .empty ();
427
401
});
428
402
}
429
-
430
403
this .connectionHolder .reset ();
431
404
return Mono .empty ();
432
405
}
0 commit comments