Top Description Inners Fields Constructors Methods
org.springframework.r2dbc.connection

public abstract Class ConnectionFactoryUtils

extends Object
Class Inheritance
Imports
java.util.Set, io.r2dbc.spi.Connection, .ConnectionFactory, .R2dbcBadGrammarException, .R2dbcDataIntegrityViolationException, .R2dbcException, .R2dbcNonTransientException, .R2dbcNonTransientResourceException, .R2dbcPermissionDeniedException, .R2dbcRollbackException, .R2dbcTimeoutException, .R2dbcTransientException, .R2dbcTransientResourceException, .Wrapped, reactor.core.publisher.Mono, org.springframework.core.Ordered, org.springframework.dao.CannotAcquireLockException, .DataAccessException, .DataAccessResourceFailureException, .DataIntegrityViolationException, .DuplicateKeyException, .PermissionDeniedDataAccessException, .PessimisticLockingFailureException, .QueryTimeoutException, .TransientDataAccessResourceException, org.springframework.lang.Nullable, org.springframework.r2dbc.BadSqlGrammarException, .UncategorizedR2dbcException, org.springframework.transaction.NoTransactionException, org.springframework.transaction.reactive.TransactionSynchronization, .TransactionSynchronizationManager, org.springframework.util.Assert

Helper class that provides static methods for obtaining R2DBC Connections from a ConnectionFactory.

Used internally by Spring's DatabaseClient, Spring's R2DBC operation objects. Can also be used directly in application code.

Authors
Mark Paluch, Christoph Strobl
Since
5.3
See Also
R2dbcTransactionManager, org.springframework.transaction.reactive.TransactionSynchronizationManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ConnectionFactoryUtils.ConnectionSynchronization

Callback for resource cleanup at the end of a non-native R2DBC transaction.

Field Summary

Modifier and TypeField and Description
public static final int
CONNECTION_SYNCHRONIZATION_ORDER

Order value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.

private static final Set<Integer>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static String

Returns:

the message String to use
buildMessage
(String
readable text describing the task being attempted
task
,
String
the SQL statement that caused the problem
sql
,
R2dbcException
the offending R2dbcException
ex
)

Build a message String for the given R2dbcException.

private static boolean

Returns:

whether the given Connections are equal
connectionEquals
(ConnectionHolder
the ConnectionHolder for the held Connection (potentially a proxy)
conHolder
,
Connection
the Connection passed-in by the user (potentially a target Connection without proxy).
passedInCon
)

Determine whether the given two Connections are equal, asking the target Connection in case of a proxy.

public static DataAccessException

Returns:

the corresponding DataAccessException instance
convertR2dbcException
(String
readable text describing the task being attempted
task
,
String
the SQL query or update that caused the problem (if known)
sql
,
R2dbcException
the offending R2dbcException
ex
)

Translate the given R2dbcException into a generic DataAccessException.

public static Mono<ConnectionFactory>
currentConnectionFactory(ConnectionFactory
the ConnectionFactory that the Connection was obtained from
connectionFactory
)

Obtain the ConnectionFactory from the current TransactionSynchronizationManager.

public static Mono<Connection>

Returns:

a R2DBC Connection from the given ConnectionFactory.
doGetConnection
(ConnectionFactory
the ConnectionFactory to obtain Connections from
connectionFactory
)

Actually obtain a R2DBC Connection from the given ConnectionFactory.

public static Mono<Void>
doReleaseConnection(Connection
the Connection to close if necessary
connection
,
ConnectionFactory
the ConnectionFactory that the Connection was obtained from
connectionFactory
)

Actually close the given Connection, obtained from the given ConnectionFactory.

private static Mono<Connection>

Returns:

a R2DBC Connection from the given ConnectionFactory (never null).
fetchConnection
(ConnectionFactory
the ConnectionFactory to obtain Connections from
connectionFactory
)

Actually fetch a Connection from the given ConnectionFactory.

public static Mono<Connection>

Returns:

a R2DBC Connection from the given ConnectionFactory
getConnection
(ConnectionFactory
the ConnectionFactory to obtain Connections from
connectionFactory
)

Obtain a Connection from the given ConnectionFactory.

private static int

Returns:

the connection synchronization order to use
getConnectionSynchronizationOrder
(ConnectionFactory
the ConnectionFactory to check
connectionFactory
)

Determine the connection synchronization order to use for the given ConnectionFactory.

public static Connection

Returns:

the innermost target Connection, or the passed-in one if not wrapped
getTargetConnection
(Connection
the Connection wrapper to unwrap
con
)

Return the innermost target Connection of the given Connection.

pack-priv static boolean
indicatesDuplicateKey(String
the SQL state value
sqlState
,
int
the error code
errorCode
)

Check whether the given SQL state and the associated error code (in case of a generic SQL state value) indicate a duplicate key exception: either SQL state 23505 as a specific indication, or the generic SQL state 23000 with a well-known vendor code.

public static Mono<Void>
releaseConnection(Connection
the Connection to close if necessary
con
,
ConnectionFactory
the ConnectionFactory that the Connection was obtained from
connectionFactory
)

Close the given Connection, obtained from the given ConnectionFactory, if it is not managed externally (that is, not bound to the subscription).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONNECTION_SYNCHRONIZATION_ORDERback to summary
public static final int CONNECTION_SYNCHRONIZATION_ORDER

Order value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.

DUPLICATE_KEY_ERROR_CODESback to summary
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES

Constructor Detail

ConnectionFactoryUtilsback to summary
public ConnectionFactoryUtils()

Method Detail

buildMessageback to summary
private static String buildMessage(String task, String sql, R2dbcException ex)

Build a message String for the given R2dbcException.

To be called by translator subclasses when creating an instance of a generic org.springframework.dao.DataAccessException class.

Parameters
task:String

readable text describing the task being attempted

sql:String

the SQL statement that caused the problem

ex:R2dbcException

the offending R2dbcException

Returns:String

the message String to use

connectionEqualsback to summary
private static boolean connectionEquals(ConnectionHolder conHolder, Connection passedInCon)

Determine whether the given two Connections are equal, asking the target Connection in case of a proxy. Used to detect equality even if the user passed in a raw target Connection while the held one is a proxy.

Parameters
conHolder:ConnectionHolder

the ConnectionHolder for the held Connection (potentially a proxy)

passedInCon:Connection

the Connection passed-in by the user (potentially a target Connection without proxy).

Returns:boolean

whether the given Connections are equal

See Also
getTargetConnection
convertR2dbcExceptionback to summary
public static DataAccessException convertR2dbcException(String task, String sql, R2dbcException ex)

Translate the given R2dbcException into a generic DataAccessException.

The returned DataAccessException is supposed to contain the original R2dbcException as root cause. However, client code may not generally rely on this due to DataAccessExceptions possibly being caused by other resource APIs as well. That said, a getRootCause() instanceof R2dbcException check (and subsequent cast) is considered reliable when expecting R2DBC-based access to have happened.

Parameters
task:String

readable text describing the task being attempted

sql:String

the SQL query or update that caused the problem (if known)

ex:R2dbcException

the offending R2dbcException

Returns:DataAccessException

the corresponding DataAccessException instance

currentConnectionFactoryback to summary
public static Mono<ConnectionFactory> currentConnectionFactory(ConnectionFactory connectionFactory)

Obtain the ConnectionFactory from the current TransactionSynchronizationManager.

Parameters
connectionFactory:ConnectionFactory

the ConnectionFactory that the Connection was obtained from

See Also
TransactionSynchronizationManager
doGetConnectionback to summary
public static Mono<Connection> doGetConnection(ConnectionFactory connectionFactory)

Actually obtain a R2DBC Connection from the given ConnectionFactory. Same as getConnection, but preserving the original exceptions.

Is aware of a corresponding Connection bound to the current TransactionSynchronizationManager. Will bind a Connection to the TransactionSynchronizationManager if transaction synchronization is active

Parameters
connectionFactory:ConnectionFactory

the ConnectionFactory to obtain Connections from

Returns:Mono<Connection>

a R2DBC Connection from the given ConnectionFactory.

doReleaseConnectionback to summary
public static Mono<Void> doReleaseConnection(Connection connection, ConnectionFactory connectionFactory)

Actually close the given Connection, obtained from the given ConnectionFactory. Same as releaseConnection, but preserving the original exception.

Parameters
connection:Connection

the Connection to close if necessary

connectionFactory:ConnectionFactory

the ConnectionFactory that the Connection was obtained from

See Also
doGetConnection
fetchConnectionback to summary
private static Mono<Connection> fetchConnection(ConnectionFactory connectionFactory)

Actually fetch a Connection from the given ConnectionFactory.

Parameters
connectionFactory:ConnectionFactory

the ConnectionFactory to obtain Connections from

Returns:Mono<Connection>

a R2DBC Connection from the given ConnectionFactory (never null).

Exceptions
IllegalStateException:
if the ConnectionFactory returned a null value.
See Also
ConnectionFactory#create()
getConnectionback to summary
public static Mono<Connection> getConnection(ConnectionFactory connectionFactory)

Obtain a Connection from the given ConnectionFactory. Translates exceptions into the Spring hierarchy of unchecked generic data access exceptions, simplifying calling code and making any exception that is thrown more meaningful.

Is aware of a corresponding Connection bound to the current TransactionSynchronizationManager. Will bind a Connection to the TransactionSynchronizationManager if transaction synchronization is active.

Parameters
connectionFactory:ConnectionFactory

the ConnectionFactory to obtain Connections from

Returns:Mono<Connection>

a R2DBC Connection from the given ConnectionFactory

Exceptions
DataAccessResourceFailureException:
if the attempt to get a Connection failed
See Also
releaseConnection
getConnectionSynchronizationOrderback to summary
private static int getConnectionSynchronizationOrder(ConnectionFactory connectionFactory)

Determine the connection synchronization order to use for the given ConnectionFactory. Decreased for every level of nesting that a ConnectionFactory has, checked through the level of DelegatingConnectionFactory nesting.

Parameters
connectionFactory:ConnectionFactory

the ConnectionFactory to check

Returns:int

the connection synchronization order to use

See Also
CONNECTION_SYNCHRONIZATION_ORDER
getTargetConnectionback to summary
public static Connection getTargetConnection(Connection con)

Return the innermost target Connection of the given Connection. If the given Connection is wrapped, it will be unwrapped until a plain Connection is found. Otherwise, the passed-in Connection will be returned as-is.

Parameters
con:Connection

the Connection wrapper to unwrap

Returns:Connection

the innermost target Connection, or the passed-in one if not wrapped

Annotations
@SuppressWarnings:unchecked
See Also
Wrapped#unwrap()
indicatesDuplicateKeyback to summary
pack-priv static boolean indicatesDuplicateKey(String sqlState, int errorCode)

Check whether the given SQL state and the associated error code (in case of a generic SQL state value) indicate a duplicate key exception: either SQL state 23505 as a specific indication, or the generic SQL state 23000 with a well-known vendor code.

Parameters
sqlState:String

the SQL state value

errorCode:int

the error code

See Also
org.springframework.jdbc.support.SQLStateSQLExceptionTranslator#indicatesDuplicateKey
releaseConnectionback to summary
public static Mono<Void> releaseConnection(Connection con, ConnectionFactory connectionFactory)

Close the given Connection, obtained from the given ConnectionFactory, if it is not managed externally (that is, not bound to the subscription).

Parameters
con:Connection

the Connection to close if necessary

connectionFactory:ConnectionFactory

the ConnectionFactory that the Connection was obtained from

See Also
getConnection
org.springframework.r2dbc.connection back to summary

private Class ConnectionFactoryUtils.ConnectionSynchronization

extends Object
implements TransactionSynchronization, Ordered
Class Inheritance
  • java.lang.Object
  • org.springframework.r2dbc.connection.ConnectionFactoryUtils.ConnectionSynchronization
All Implemented Interfaces
org.springframework.core.Ordered, org.springframework.transaction.reactive.TransactionSynchronization

Callback for resource cleanup at the end of a non-native R2DBC transaction.

Field Summary

Modifier and TypeField and Description
private final ConnectionFactory
private final ConnectionHolder
private boolean
private final int

Constructor Summary

AccessConstructor and Description
pack-priv
ConnectionSynchronization(ConnectionHolder connectionHolder, ConnectionFactory connectionFactory)

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>
afterCompletion(int
completion status according to the STATUS_* constants
status
)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback.
public Mono<Void>
public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.
public Mono<Void>
public Mono<Void>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionFactoryback to summary
private final ConnectionFactory connectionFactory
connectionHolderback to summary
private final ConnectionHolder connectionHolder
holderActiveback to summary
private boolean holderActive
orderback to summary
private final int order

Constructor Detail

ConnectionSynchronizationback to summary
pack-priv ConnectionSynchronization(ConnectionHolder connectionHolder, ConnectionFactory connectionFactory)

Method Detail

afterCompletionback to summary
public Mono<Void> afterCompletion(int status)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion.

Note

The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Parameters
status:int

completion status according to the STATUS_* constants

Annotations
@Override
beforeCompletionback to summary
public Mono<Void> beforeCompletion()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.beforeCompletion.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.beforeCompletion.

Invoked before transaction commit/rollback. Can perform resource cleanup before transaction completion.

This method will be invoked after beforeCommit, even when beforeCommit threw an exception. This callback allows for closing resources before transaction completion, for any outcome.

Annotations
@Override
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
resumeback to summary
public Mono<Void> resume()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.resume.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.resume.

Resume this synchronization. Supposed to rebind resources to TransactionSynchronizationManager if managing any.

Annotations
@Override
suspendback to summary
public Mono<Void> suspend()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.suspend.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.suspend.

Suspend this synchronization. Supposed to unbind resources from TransactionSynchronizationManager if managing any.

Annotations
@Override