Class AbstractRoutingSessionFactory
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
,SessionFactory
SessionFactory
implementation that routes getSession()
calls to one of various target
factories
based on a lookup key. The latter is usually (but not necessarily) determined
through some thread-bound transaction context.- Since:
- 2.0
- Author:
- Mark Paluch
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected abstract Object
Determine the current lookup key.protected SessionFactory
Retrieve the current targetSessionFactory
.com.datastax.oss.driver.api.core.CqlSession
Attempts to establish aCqlSession
with the connection infrastructure that thisSessionFactory
object represents.protected Object
resolveSpecifiedLookupKey
(Object lookupKey) Resolve the given lookup key object, as specified in thesetTargetSessionFactories(Map)
map, into the actual lookup key to be used for matching with thecurrent lookup key
.protected SessionFactory
resolveSpecifiedSessionFactory
(Object sessionFactory) Resolve the specifiedsessionFactory
object into aSessionFactory
instance.void
setDefaultTargetSessionFactory
(Object defaultTargetSessionFactory) Specify the default targetSessionFactory
, if any.void
setLenientFallback
(boolean lenientFallback) Specify whether to apply a lenient fallback to the defaultSessionFactory
if no specificSessionFactory
could be found for the current lookup key.void
setSessionFactoryLookup
(SessionFactoryLookup sessionFactoryLookup) Set theSessionFactoryLookup
implementation to use for resolving session factory name Strings in thesetTargetSessionFactories(Map)
map.void
setTargetSessionFactories
(Map<Object, Object> targetSessionFactories) Specify the map of target session factories, with the lookup key as key.
-
Constructor Details
-
AbstractRoutingSessionFactory
public AbstractRoutingSessionFactory()
-
-
Method Details
-
setTargetSessionFactories
Specify the map of target session factories, with the lookup key as key.The mapped value can either be a corresponding
SessionFactory
instance or a data source name String (to be resolved via asetSessionFactoryLookup(SessionFactoryLookup)
).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)
anddetermineCurrentLookupKey()
. -
setDefaultTargetSessionFactory
Specify the default targetSessionFactory
, if any.The mapped value can either be a corresponding
SessionFactory
instance or a data source name String (to be resolved via asetSessionFactoryLookup(SessionFactoryLookup)
).This
SessionFactory
will be used as target if none of the keyedsetTargetSessionFactories(Map)
match thedetermineCurrentLookupKey()
current lookup key. -
setLenientFallback
public void setLenientFallback(boolean lenientFallback) Specify whether to apply a lenient fallback to the defaultSessionFactory
if no specificSessionFactory
could be found for the current lookup key.Default is true, accepting lookup keys without a corresponding entry in the target
SessionFactory
map - simply falling back to the defaultSessionFactory
in that case.Switch this flag to false if you would prefer the fallback to only apply if the lookup key was null. Lookup keys without a
SessionFactory
entry will then lead to anIllegalStateException
.- Parameters:
lenientFallback
- true to accepting lookup keys without a corresponding entry in the target.- See Also:
-
setSessionFactoryLookup
Set theSessionFactoryLookup
implementation to use for resolving session factory name Strings in thesetTargetSessionFactories(Map)
map.Default is a
MapSessionFactoryLookup
, allowing a string keyed map ofsession factories
.- Parameters:
sessionFactoryLookup
- theSessionFactoryLookup
. Defaults toMapSessionFactoryLookup
if null.
-
getSession
public com.datastax.oss.driver.api.core.CqlSession getSession()Description copied from interface:SessionFactory
Attempts to establish aCqlSession
with the connection infrastructure that thisSessionFactory
object represents.- Specified by:
getSession
in interfaceSessionFactory
- Returns:
- a
CqlSession
to Apache Cassandra. - See Also:
-
CqlSession
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
resolveSpecifiedLookupKey
Resolve the given lookup key object, as specified in thesetTargetSessionFactories(Map)
map, into the actual lookup key to be used for matching with thecurrent lookup key
.The default implementation simply returns the given key as-is.
- Parameters:
lookupKey
- the lookup key object as specified by the user- Returns:
- the lookup key as needed for matching
-
resolveSpecifiedSessionFactory
protected SessionFactory resolveSpecifiedSessionFactory(Object sessionFactory) throws IllegalArgumentException Resolve the specifiedsessionFactory
object into aSessionFactory
instance.The default implementation handles
SessionFactory
instances and session factory names (to be resolved via asetSessionFactoryLookup(SessionFactoryLookup)
).- Parameters:
sessionFactory
- the session factory value object as specified in thesetTargetSessionFactories(Map)
map- Returns:
- the resolved
SessionFactory
- Throws:
IllegalArgumentException
- in case of an unsupported value type.SessionFactoryLookupFailureException
- if the lookup failed.
-
determineTargetSessionFactory
Retrieve the current targetSessionFactory
. Determines thecurrent lookup key
, performs a lookup in thesetTargetSessionFactories(Map)
map, falls back to the specifieddefault target SessionFactory
if necessary.- See Also:
-
determineCurrentLookupKey
Determine the current lookup key. This will typically be implemented to check a thread-bound context.Allows for arbitrary keys.
- Returns:
- the current lookup key. The returned key needs to match the stored lookup key type.
-