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

public Class SingleConnectionFactory

extends DelegatingConnectionFactory
implements DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean
Imports
java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.concurrent.atomic.AtomicReference, io.r2dbc.spi.Connection, .ConnectionFactories, .ConnectionFactory, .ConnectionFactoryMetadata, .Wrapped, org.reactivestreams.Publisher, reactor.core.publisher.Mono, org.springframework.beans.factory.DisposableBean, org.springframework.lang.Nullable, org.springframework.util.Assert

Implementation of 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 pooling ConnectionFactory such as one from r2dbc-pool.

Author
Mark Paluch
Since
5.3
See Also
create(), Connection#close(), ConnectionFactoryUtils#releaseConnection(Connection, ConnectionFactory)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SingleConnectionFactory.CloseSuppressingInvocationHandler

Invocation handler that suppresses close calls on R2DBC Connections.

Field Summary

Modifier and TypeField and Description
private Boolean
autoCommit

Override auto-commit state?.

private Connection
connection

Proxy Connection.

private final Mono<? extends Connection>
private boolean
suppressClose

Create a close-suppressing proxy?.

private final AtomicReference<Connection>
target

Wrapped Connection.

Constructor Summary

AccessConstructor and Description
public
SingleConnectionFactory(ConnectionFactory targetConnectionFactory)

Constructor for bean-style configuration.

public
SingleConnectionFactory(String
the R2DBC URL to use for accessing ConnectionFactory discovery
url
,
boolean
if the returned Connection should be a close-suppressing proxy or the physical Connection
suppressClose
)

Create a new SingleConnectionFactory using an R2DBC connection URL.

public
SingleConnectionFactory(Connection
underlying target Connection
target
,
ConnectionFactoryMetadata
ConnectionFactory metadata to be associated with this ConnectionFactory
metadata
,
boolean
true if the Connection should be wrapped with a Connection that suppresses close() calls (to allow for normal close() usage in applications that expect a pooled Connection)
suppressClose
)

Create a new SingleConnectionFactory with a given Connection and ConnectionFactoryMetadata.

Method Summary

Modifier and TypeMethod and Description
public Mono<? extends Connection>
public void
protected Boolean

Returns:

the "autoCommit" value, or null if none to be applied
getAutoCommitValue
()

Return whether the returned Connection's "autoCommit" setting should be overridden.

protected Connection

Returns:

the wrapped Connection
getCloseSuppressingConnectionProxy
(Connection
the original Connection to wrap
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 Mono<Connection>

Returns:

the prepared Connection
prepareConnection
(Connection
the requested Connection
connection
)

Prepare the Connection before using it.

public Mono<Void>
resetConnection()

Reset the underlying shared Connection, to be reinitialized on next access.

public void
setAutoCommit(boolean autoCommit)

Set whether the returned Connection's "autoCommit" setting should be overridden.

public void
setSuppressClose(boolean suppressClose)

Set whether the returned Connection should be a close-suppressing proxy or the physical Connection.

Inherited from org.springframework.r2dbc.connection.DelegatingConnectionFactory:
getMetadatagetTargetConnectionFactoryunwrap

Field Detail

autoCommitback to summary
private Boolean autoCommit

Override auto-commit state?.

Annotations
@Nullable
connectionback to summary
private Connection connection

Proxy Connection.

Annotations
@Nullable
connectionEmitterback to summary
private final Mono<? extends Connection> connectionEmitter
suppressCloseback to summary
private boolean suppressClose

Create a close-suppressing proxy?.

targetback to summary
private final AtomicReference<Connection> target

Wrapped Connection.

Constructor Detail

SingleConnectionFactoryback to summary
public SingleConnectionFactory(ConnectionFactory targetConnectionFactory)

Constructor for bean-style configuration.

SingleConnectionFactoryback to summary
public SingleConnectionFactory(String url, boolean suppressClose)

Create a new SingleConnectionFactory using an R2DBC connection URL.

Parameters
url:String

the R2DBC URL to use for accessing ConnectionFactory discovery

suppressClose:boolean

if the returned Connection should be a close-suppressing proxy or the physical Connection

See Also
ConnectionFactories#get(String)
SingleConnectionFactoryback to summary
public SingleConnectionFactory(Connection target, ConnectionFactoryMetadata metadata, boolean suppressClose)

Create a new SingleConnectionFactory with a given Connection and ConnectionFactoryMetadata.

Parameters
target:Connection

underlying target Connection

metadata:ConnectionFactoryMetadata

ConnectionFactory metadata to be associated with this ConnectionFactory

suppressClose:boolean

true if the Connection should be wrapped with a Connection that suppresses close() calls (to allow for normal close() usage in applications that expect a pooled Connection)

Method Detail

createback to summary
public Mono<? extends Connection> create()

Overrides org.springframework.r2dbc.connection.DelegatingConnectionFactory.create.

Implements io.r2dbc.spi.ConnectionFactory.create.

Doc from io.r2dbc.spi.ConnectionFactory.create.

Creates a new Connection.

Returns:Mono<? extends Connection>

the newly created Connection

Annotations
@Override
destroyback to summary
public void destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Close the underlying 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.

Annotations
@Override
getAutoCommitValueback to summary
protected Boolean getAutoCommitValue()

Return whether the returned Connection's "autoCommit" setting should be overridden.

Returns:Boolean

the "autoCommit" value, or null if none to be applied

Annotations
@Nullable
getCloseSuppressingConnectionProxyback to summary
protected Connection getCloseSuppressingConnectionProxy(Connection target)

Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.

Parameters
target:Connection

the original Connection to wrap

Returns:Connection

the wrapped Connection

isSuppressCloseback to summary
protected boolean isSuppressClose()

Return whether the returned Connection will be a close-suppressing proxy or the physical Connection.

prepareConnectionback to summary
protected Mono<Connection> prepareConnection(Connection connection)

Prepare the Connection before using it. Applies auto-commit settings if configured.

Parameters
connection:Connection

the requested Connection

Returns:Mono<Connection>

the prepared Connection

resetConnectionback to summary
public Mono<Void> resetConnection()

Reset the underlying shared Connection, to be reinitialized on next access.

setAutoCommitback to summary
public void setAutoCommit(boolean autoCommit)

Set whether the returned Connection's "autoCommit" setting should be overridden.

setSuppressCloseback to summary
public void setSuppressClose(boolean suppressClose)

Set whether the returned Connection should be a close-suppressing proxy or the physical Connection.

org.springframework.r2dbc.connection back to summary

private Class SingleConnectionFactory.CloseSuppressingInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.r2dbc.connection.SingleConnectionFactory.CloseSuppressingInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Invocation handler that suppresses close calls on R2DBC Connections.
See Also
Connection#close()

Field Summary

Modifier and TypeField and Description
private final Connection

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

targetback to summary
private final Connection target

Constructor Detail

CloseSuppressingInvocationHandlerback to summary
pack-priv CloseSuppressingInvocationHandler(Connection target)

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
@Nullable
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.