public class SingleConnectionFactory extends DelegatingConnectionFactory implements 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 (e.g. 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 ConnectionFactory implemented by e.g.
r2dbc-pool.
create(),
Connection.close(),
ConnectionFactoryUtils.releaseConnection(Connection, ConnectionFactory)| Constructor and Description |
|---|
SingleConnectionFactory(io.r2dbc.spi.Connection target,
io.r2dbc.spi.ConnectionFactoryMetadata metadata,
boolean suppressClose)
|
SingleConnectionFactory(io.r2dbc.spi.ConnectionFactory targetConnectionFactory)
Constructor for bean-style configuration.
|
SingleConnectionFactory(String url,
boolean suppressClose)
Create a new
SingleConnectionFactory using a R2DBC connection URL. |
| Modifier and Type | Method and Description |
|---|---|
reactor.core.publisher.Mono<? extends io.r2dbc.spi.Connection> |
create() |
void |
destroy()
Close the underlying
Connection. |
protected Boolean |
getAutoCommitValue()
Return whether the returned
Connection's "autoCommit" setting should be overridden. |
protected io.r2dbc.spi.Connection |
getCloseSuppressingConnectionProxy(io.r2dbc.spi.Connection target)
Wrap the given
Connection with a proxy that delegates every method call to it
but suppresses close calls. |
protected boolean |
isSuppressClose()
Return whether the returned
Connection will be a close-suppressing proxy
or the physical Connection. |
protected reactor.core.publisher.Mono<io.r2dbc.spi.Connection> |
prepareConnection(io.r2dbc.spi.Connection connection)
Prepare the
Connection before using it. |
reactor.core.publisher.Mono<Void> |
resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.
|
void |
setAutoCommit(boolean autoCommit)
Set whether the returned
Connection's "autoCommit" setting should be overridden. |
void |
setSuppressClose(boolean suppressClose)
Set whether the returned
Connection should be a close-suppressing proxy
or the physical Connection. |
getMetadata, getTargetConnectionFactory, unwrappublic SingleConnectionFactory(io.r2dbc.spi.ConnectionFactory targetConnectionFactory)
public SingleConnectionFactory(String url, boolean suppressClose)
SingleConnectionFactory using a R2DBC connection URL.url - the R2DBC URL to use for accessing ConnectionFactory discovery.suppressClose - if the returned Connection should be a close-suppressing proxy
or the physical Connection.ConnectionFactories.get(String)public SingleConnectionFactory(io.r2dbc.spi.Connection target,
io.r2dbc.spi.ConnectionFactoryMetadata metadata,
boolean suppressClose)
target - underlying target Connection.metadata - ConnectionFactory metadata to be associated with this ConnectionFactory.suppressClose - if the Connection should be wrapped with a Connection that suppressespublic void setSuppressClose(boolean suppressClose)
Connection should be a close-suppressing proxy
or the physical Connection.protected boolean isSuppressClose()
Connection will be a close-suppressing proxy
or the physical Connection.public void setAutoCommit(boolean autoCommit)
Connection's "autoCommit" setting should be overridden.@Nullable protected Boolean getAutoCommitValue()
Connection's "autoCommit" setting should be overridden.null if none to be appliedpublic reactor.core.publisher.Mono<? extends io.r2dbc.spi.Connection> create()
create in interface io.r2dbc.spi.ConnectionFactorycreate in class DelegatingConnectionFactorypublic void destroy()
Connection.
The provider of this ConnectionFactory needs to care for proper shutdown.
As this bean implements DisposableBean, a bean factory will automatically
invoke this on destruction of its cached singletons.
destroy in interface DisposableBeanpublic reactor.core.publisher.Mono<Void> resetConnection()
protected reactor.core.publisher.Mono<io.r2dbc.spi.Connection> prepareConnection(io.r2dbc.spi.Connection connection)
Connection before using it.
Applies auto-commit settings if configured.connection - the requested Connection.Connection.protected io.r2dbc.spi.Connection getCloseSuppressingConnectionProxy(io.r2dbc.spi.Connection target)
Connection with a proxy that delegates every method call to it
but suppresses close calls.target - the original Connection to wrap.