Using ConnectionFactory
Spring establishes an R2DBC connection to the database via ConnectionFactory
. ConnectionFactory
is part of the R2DBC specification and provides a common entry point for drivers. It allows the container or framework to hide the problems of connection pooling and transaction management from the application code. As a developer, you don't need to know in detail about how to connect to a database. This is the responsibility of the administrator who installs ConnectionFactory
. You likely play both roles as you develop and test code, but you don't necessarily need to know how the production data source is configured.
If you are using the R2DBC layer from Spring, you can configure your own using a connection pooling implementation provided by a third party. A popular implementation is the R2DBC pool (r2dbc-pool
). The implementations in the Spring distribution are for testing purposes only and do not provide pooling.
To configure ConnectionFactory
:
-
Establish a connection to the
ConnectionFactory
in the same way you would normally obtain aConnectionFactory
from R2DBC. -
Specify the R2DBC URL (check your driver documentation for the correct value).
The following example shows how to configure ConnectionFactory
:
ConnectionFactory factory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
val factory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
Using ConnectionFactoryUtils
The ConnectionFactoryUtils
class is a convenient and full-featured helper class that provides static methods for establishing connections from ConnectionFactory
and closing connections (if necessary).
It supports connections bound to the Context
of the subscriber, such as R2dbcTransactionManager
.
Using SingleConnectionFactory
The SingleConnectionFactory
class is an implementation of the DelegatingConnectionFactory
interface that wraps a single Connection
that is not closed after each use.
If any client code calls close
under the assumption that a connection pool exists (as when using persistence support tools), you should set the suppressClose
property to true
. This parameter returns a proxy that suppresses closures that wraps the physical connection. Note that you can no longer cast it to a native Connection
or similar object.
SingleConnectionFactory
is primarily a test class and can be used for specific requirements such as pipelining if your R2DBC driver allows such use. Unlike a ConnectionFactory
pool, it allows you to reuse the same connection all the time, which avoids over-creation of physical connections.
Using TransactionAwareConnectionFactoryProxy
TransactionAwareConnectionFactoryProxy
is a proxy for the target ConnectionFactory
. The proxy wraps this target ConnectionFactory
to improve compatibility with Spring-managed transactions.
ConnectionFactoryUtils
for resource management.
For more information, see the javadoc at TransactionAwareConnectionFactoryProxy
.
Using R2dbcTransactionManager
The Class R2dbcTransactionManager
is an implementation of the ReactiveTransactionManager
for individual R2DBC data sources. It binds an R2DBC connection from the specified connection factory to the Context
of the subscriber, potentially allowing one subscriber connection per connection factory.
Application code is required to obtain an R2DBC connection via ConnectionFactoryUtils.getConnection(ConnectionFactory)
instead of the standard R2DBC ConnectionFactory.create()
.
All framework classes (for example, DatabaseClient
) use this strategy implicitly. If the search strategy is not used with this transaction manager, then it behaves exactly the same as a regular one. Thus, it can be used in any case.
The R2dbcTransactionManager
class supports custom isolation levels that are applied to the connection.
GO TO FULL VERSION