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

public abstract Class AbstractRoutingConnectionFactory

extends Object
implements ConnectionFactory, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, io.r2dbc.spi.ConnectionFactory
Imports
java.util.Map, io.r2dbc.spi.Connection, .ConnectionFactory, .ConnectionFactoryMetadata, reactor.core.publisher.Mono, org.springframework.beans.factory.InitializingBean, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils

Abstract ConnectionFactory implementation that routes create() calls to one of various target factories based on a lookup key. The latter is typically (but not necessarily) determined from some subscriber context.

Allows to configure a default target ConnectionFactory as a fallback.

Calls to getMetadata() are routed to the default ConnectionFactory if configured.

Authors
Mark Paluch, Jens Schauder
Since
5.3
See Also
setTargetConnectionFactories, setDefaultTargetConnectionFactory, determineCurrentLookupKey()

Field Summary

Modifier and TypeField and Description
private ConnectionFactoryLookup
private Object
private static final Object
private boolean
private Map<Object, ConnectionFactory>
private ConnectionFactory
private Map<?, ?>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public Mono<Connection>
protected abstract Mono<Object>

Returns:

Mono emitting the lookup key. May complete without emitting a value if no lookup key available
determineCurrentLookupKey
()

Determine the current lookup key.

protected Mono<ConnectionFactory>

Returns:

Mono that emits the current ConnectionFactory as per determineCurrentLookupKey()
determineTargetConnectionFactory
()

Retrieve the current target ConnectionFactory.

public ConnectionFactoryMetadata
getMetadata()

Implements io.r2dbc.spi.ConnectionFactory.getMetadata.

Returns the ConnectionFactoryMetadata about the product this ConnectionFactory is applicable to.

public void
initialize()

Initialize the internal state of this AbstractRoutingConnectionFactory by resolving the configured target ConnectionFactories.

protected ConnectionFactory

Returns:

the resolved ConnectionFactory (never null)
resolveSpecifiedConnectionFactory
(Object
the connection factory value object as specified in the targetConnectionFactories map
connectionFactory
)

Resolve the specified connection factory object into a ConnectionFactory instance.

protected Object

Returns:

the lookup key as needed for matching.
resolveSpecifiedLookupKey
(Object
the lookup key object as specified by the user
lookupKey
)

Resolve the given lookup key object, as specified in the targetConnectionFactories map, into the actual lookup key to be used for matching with the current lookup key.

public void
setConnectionFactoryLookup(ConnectionFactoryLookup connectionFactoryLookup)

Set the ConnectionFactoryLookup implementation to use for resolving connection factory name Strings in the targetConnectionFactories map.

public void
setDefaultTargetConnectionFactory(Object defaultTargetConnectionFactory)

Specify the default target ConnectionFactory, if any.

public void
setLenientFallback(boolean lenientFallback)

Specify whether to apply a lenient fallback to the default ConnectionFactory if no specific ConnectionFactory could be found for the current lookup key.

public void
setTargetConnectionFactories(Map<?, ?> targetConnectionFactories)

Specify the map of target ConnectionFactories, with the lookup key as key.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionFactoryLookupback to summary
private ConnectionFactoryLookup connectionFactoryLookup
defaultTargetConnectionFactoryback to summary
private Object defaultTargetConnectionFactory
Annotations
@Nullable
FALLBACK_MARKERback to summary
private static final Object FALLBACK_MARKER
lenientFallbackback to summary
private boolean lenientFallback
resolvedConnectionFactoriesback to summary
private Map<Object, ConnectionFactory> resolvedConnectionFactories
Annotations
@Nullable
resolvedDefaultConnectionFactoryback to summary
private ConnectionFactory resolvedDefaultConnectionFactory
Annotations
@Nullable
targetConnectionFactoriesback to summary
private Map<?, ?> targetConnectionFactories
Annotations
@Nullable

Constructor Detail

AbstractRoutingConnectionFactoryback to summary
public AbstractRoutingConnectionFactory()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Delegates to initialize().

Annotations
@Override
createback to summary
public Mono<Connection> create()

Implements io.r2dbc.spi.ConnectionFactory.create.

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

Creates a new Connection.

Returns:Mono<Connection>

the newly created Connection

Annotations
@Override
determineCurrentLookupKeyback to summary
protected abstract Mono<Object> determineCurrentLookupKey()

Determine the current lookup key. This will typically be implemented to check a subscriber context. Allows for arbitrary keys. The returned key needs to match the stored lookup key type, as resolved by the resolveSpecifiedLookupKey method.

Returns:Mono<Object>

Mono emitting the lookup key. May complete without emitting a value if no lookup key available

determineTargetConnectionFactoryback to summary
protected Mono<ConnectionFactory> determineTargetConnectionFactory()

Retrieve the current target ConnectionFactory. Determines the current lookup key, performs a lookup in the targetConnectionFactories map, falls back to the specified default target ConnectionFactory if necessary.

Returns:Mono<ConnectionFactory>

Mono that emits the current ConnectionFactory as per determineCurrentLookupKey()

See Also
determineCurrentLookupKey()
getMetadataback to summary
public ConnectionFactoryMetadata getMetadata()

Implements io.r2dbc.spi.ConnectionFactory.getMetadata.

Doc from io.r2dbc.spi.ConnectionFactory.getMetadata.

Returns the ConnectionFactoryMetadata about the product this ConnectionFactory is applicable to.

Returns:ConnectionFactoryMetadata

the ConnectionFactoryMetadata about the product this ConnectionFactory is applicable to

Annotations
@Override
initializeback to summary
public void initialize()

Initialize the internal state of this AbstractRoutingConnectionFactory by resolving the configured target ConnectionFactories.

Exceptions
IllegalArgumentException:
if the target ConnectionFactories have not been configured
Since
6.1
See Also
setTargetConnectionFactories(Map), setDefaultTargetConnectionFactory(Object)
resolveSpecifiedConnectionFactoryback to summary
protected ConnectionFactory resolveSpecifiedConnectionFactory(Object connectionFactory) throws IllegalArgumentException

Resolve the specified connection factory object into a ConnectionFactory instance.

The default implementation handles ConnectionFactory instances and connection factory names (to be resolved via a ConnectionFactoryLookup).

Parameters
connectionFactory:Object

the connection factory value object as specified in the targetConnectionFactories map

Returns:ConnectionFactory

the resolved ConnectionFactory (never null)

Exceptions
IllegalArgumentException:
in case of an unsupported value type
resolveSpecifiedLookupKeyback to summary
protected Object resolveSpecifiedLookupKey(Object lookupKey)

Resolve the given lookup key object, as specified in the targetConnectionFactories map, into the actual lookup key to be used for matching with the current lookup key.

The default implementation simply returns the given key as-is.

Parameters
lookupKey:Object

the lookup key object as specified by the user

Returns:Object

the lookup key as needed for matching.

setConnectionFactoryLookupback to summary
public void setConnectionFactoryLookup(ConnectionFactoryLookup connectionFactoryLookup)

Set the ConnectionFactoryLookup implementation to use for resolving connection factory name Strings in the targetConnectionFactories map.

setDefaultTargetConnectionFactoryback to summary
public void setDefaultTargetConnectionFactory(Object defaultTargetConnectionFactory)

Specify the default target ConnectionFactory, if any.

The mapped value can either be a corresponding ConnectionFactory instance or a connection factory name String (to be resolved via a ConnectionFactoryLookup).

This ConnectionFactory will be used as target if none of the keyed targetConnectionFactories match the current lookup key.

setLenientFallbackback to summary
public void setLenientFallback(boolean lenientFallback)

Specify whether to apply a lenient fallback to the default ConnectionFactory if no specific ConnectionFactory could be found for the current lookup key.

Default is true, accepting lookup keys without a corresponding entry in the target ConnectionFactory map - simply falling back to the default ConnectionFactory in that case.

Switch this flag to false if you would prefer the fallback to only apply when no lookup key was emitted. Lookup keys without a ConnectionFactory entry will then lead to an IllegalStateException.

See Also
setTargetConnectionFactories, setDefaultTargetConnectionFactory, determineCurrentLookupKey()
setTargetConnectionFactoriesback to summary
public void setTargetConnectionFactories(Map<?, ?> targetConnectionFactories)

Specify the map of target ConnectionFactories, with the lookup key as key. The mapped value can either be a corresponding ConnectionFactory instance or a connection factory name String (to be resolved via a ConnectionFactoryLookup).

The key can be of arbitrary type; this class implements the generic lookup process only. The concrete key representation will be handled by resolveSpecifiedLookupKey(Object) and determineCurrentLookupKey().