Class AbstractRoutingSessionFactory
- All Implemented Interfaces:
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 TypeMethodDescriptionvoidprotected abstract ObjectDetermine the current lookup key.protected SessionFactoryRetrieve the current targetSessionFactory.com.datastax.oss.driver.api.core.CqlSessionAttempts to establish aCqlSessionwith the connection infrastructure that thisSessionFactoryobject represents.protected ObjectresolveSpecifiedLookupKey(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 SessionFactoryresolveSpecifiedSessionFactory(Object sessionFactory) Resolve the specifiedsessionFactoryobject into aSessionFactoryinstance.voidsetDefaultTargetSessionFactory(Object defaultTargetSessionFactory) Specify the default targetSessionFactory, if any.voidsetLenientFallback(boolean lenientFallback) Specify whether to apply a lenient fallback to the defaultSessionFactoryif no specificSessionFactorycould be found for the current lookup key.voidsetSessionFactoryLookup(SessionFactoryLookup sessionFactoryLookup) Set theSessionFactoryLookupimplementation to use for resolving session factory name Strings in thesetTargetSessionFactories(Map)map.voidsetTargetSessionFactories(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
SessionFactoryinstance 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
SessionFactoryinstance or a data source name String (to be resolved via asetSessionFactoryLookup(SessionFactoryLookup)).This
SessionFactorywill 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 defaultSessionFactoryif no specificSessionFactorycould be found for the current lookup key.Default is true, accepting lookup keys without a corresponding entry in the target
SessionFactorymap - simply falling back to the defaultSessionFactoryin 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
SessionFactoryentry will then lead to anIllegalStateException.- Parameters:
lenientFallback- true to accepting lookup keys without a corresponding entry in the target.- See Also:
-
setSessionFactoryLookup
Set theSessionFactoryLookupimplementation 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 toMapSessionFactoryLookupif null.
-
getSession
public com.datastax.oss.driver.api.core.CqlSession getSession()Description copied from interface:SessionFactoryAttempts to establish aCqlSessionwith the connection infrastructure that thisSessionFactoryobject represents.- Specified by:
getSessionin interfaceSessionFactory- Returns:
- a
CqlSessionto Apache Cassandra. - See Also:
-
CqlSession
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
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 specifiedsessionFactoryobject into aSessionFactoryinstance.The default implementation handles
SessionFactoryinstances 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 SessionFactoryif 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.
-