Class SingleConnectionFactory
- All Implemented Interfaces:
ConnectionFactory
,Wrapped<ConnectionFactory>
,DisposableBean
DelegatingConnectionFactory
that wraps a
single R2DBC Connection
which is not closed after use.
Obviously, this is not multi-threading capable.
Note that at shutdown, someone should close the underlying
Connection
via the close()
method. Client code will
never call close on the Connection
handle if it is
SmartConnectionFactory-aware (for example, uses
ConnectionFactoryUtils.releaseConnection(Connection, ConnectionFactory)
).
If client code will call Connection.close()
in the
assumption of a pooled Connection
, like when using persistence tools,
set "suppressClose" to true
. This will return a close-suppressing
proxy instead of the physical Connection.
This is primarily intended for testing and pipelining usage of connections.
For example, it enables easy testing outside an application server for code
that expects to work on a ConnectionFactory
.
Note that this implementation does not act as a connection pool-like utility.
Connection pooling requires a pooling ConnectionFactory
such as one from
r2dbc-pool
.
- Since:
- 5.3
- Author:
- Mark Paluch
- See Also:
-
Constructor Summary
ConstructorDescriptionSingleConnectionFactory
(ConnectionFactory targetConnectionFactory) Constructor for bean-style configuration.SingleConnectionFactory
(Connection target, ConnectionFactoryMetadata metadata, boolean suppressClose) SingleConnectionFactory
(String url, boolean suppressClose) Create a newSingleConnectionFactory
using an R2DBC connection URL. -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<? extends Connection>
create()
void
destroy()
Close the underlyingConnection
.protected Boolean
Return whether the returnedConnection
's "autoCommit" setting should be overridden.protected Connection
Wrap the givenConnection
with a proxy that delegates every method call to it but suppresses close calls.protected boolean
Return whether the returnedConnection
will be a close-suppressing proxy or the physicalConnection
.protected reactor.core.publisher.Mono<Connection>
prepareConnection
(Connection connection) Prepare theConnection
before using it.reactor.core.publisher.Mono<Void>
Reset the underlying shared Connection, to be reinitialized on next access.void
setAutoCommit
(boolean autoCommit) Set whether the returnedConnection
's "autoCommit" setting should be overridden.void
setSuppressClose
(boolean suppressClose) Set whether the returnedConnection
should be a close-suppressing proxy or the physicalConnection
.Methods inherited from class org.springframework.r2dbc.connection.DelegatingConnectionFactory
getMetadata, getTargetConnectionFactory, unwrap
-
Constructor Details
-
SingleConnectionFactory
Constructor for bean-style configuration. -
SingleConnectionFactory
Create a newSingleConnectionFactory
using an R2DBC connection URL.- Parameters:
url
- the R2DBC URL to use for accessingConnectionFactory
discoverysuppressClose
- if the returnedConnection
should be a close-suppressing proxy or the physicalConnection
- See Also:
-
SingleConnectionFactory
public SingleConnectionFactory(Connection target, ConnectionFactoryMetadata metadata, boolean suppressClose) - Parameters:
target
- underlying targetConnection
metadata
-ConnectionFactory
metadata to be associated with thisConnectionFactory
suppressClose
-true
if theConnection
should be wrapped with aConnection
that suppressesclose()
calls (to allow for normalclose()
usage in applications that expect a pooledConnection
)
-
-
Method Details
-
setSuppressClose
public void setSuppressClose(boolean suppressClose) Set whether the returnedConnection
should be a close-suppressing proxy or the physicalConnection
. -
isSuppressClose
protected boolean isSuppressClose()Return whether the returnedConnection
will be a close-suppressing proxy or the physicalConnection
. -
setAutoCommit
public void setAutoCommit(boolean autoCommit) Set whether the returnedConnection
's "autoCommit" setting should be overridden. -
getAutoCommitValue
Return whether the returnedConnection
's "autoCommit" setting should be overridden.- Returns:
- the "autoCommit" value, or
null
if none to be applied
-
create
- Specified by:
create
in interfaceConnectionFactory
- Overrides:
create
in classDelegatingConnectionFactory
-
destroy
public void destroy()Close the underlyingConnection
. The provider of thisConnectionFactory
needs to care for proper shutdown.As this bean implements
DisposableBean
, a bean factory will automatically invoke this on destruction of its cached singletons.- Specified by:
destroy
in interfaceDisposableBean
-
resetConnection
Reset the underlying shared Connection, to be reinitialized on next access. -
prepareConnection
Prepare theConnection
before using it. Applies auto-commit settings if configured.- Parameters:
connection
- the requestedConnection
- Returns:
- the prepared
Connection
-
getCloseSuppressingConnectionProxy
Wrap the givenConnection
with a proxy that delegates every method call to it but suppresses close calls.- Parameters:
target
- the originalConnection
to wrap- Returns:
- the wrapped Connection
-