org.springframework.jdbc.datasource.lookup

Class AbstractRoutingDataSource

    • Constructor Detail

      • AbstractRoutingDataSource

        public AbstractRoutingDataSource()
    • Method Detail

      • setTargetDataSources

        public void setTargetDataSources(java.util.Map<java.lang.Object,java.lang.Object> targetDataSources)
        Specify the map of target DataSources, with the lookup key as key. The mapped value can either be a corresponding DataSource instance or a data source name String (to be resolved via a DataSourceLookup).

        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().

      • setDefaultTargetDataSource

        public void setDefaultTargetDataSource(java.lang.Object defaultTargetDataSource)
        Specify the default target DataSource, if any.

        The mapped value can either be a corresponding DataSource instance or a data source name String (to be resolved via a DataSourceLookup).

        This DataSource will be used as target if none of the keyed targetDataSources match the determineCurrentLookupKey() current lookup key.

      • setLenientFallback

        public void setLenientFallback(boolean lenientFallback)
        Specify whether to apply a lenient fallback to the default DataSource if no specific DataSource could be found for the current lookup key.

        Default is "true", accepting lookup keys without a corresponding entry in the target DataSource map - simply falling back to the default DataSource 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 DataSource entry will then lead to an IllegalStateException.

        See Also:
        setTargetDataSources(java.util.Map<java.lang.Object, java.lang.Object>), setDefaultTargetDataSource(java.lang.Object), determineCurrentLookupKey()
      • setDataSourceLookup

        public void setDataSourceLookup(@Nullable
                                        DataSourceLookup dataSourceLookup)
        Set the DataSourceLookup implementation to use for resolving data source name Strings in the targetDataSources map.

        Default is a JndiDataSourceLookup, allowing the JNDI names of application server DataSources to be specified directly.

      • afterPropertiesSet

        public void afterPropertiesSet()
        Description copied from interface: InitializingBean
        Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

        This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

        Specified by:
        afterPropertiesSet in interface InitializingBean
      • resolveSpecifiedLookupKey

        protected java.lang.Object resolveSpecifiedLookupKey(java.lang.Object lookupKey)
        Resolve the given lookup key object, as specified in the targetDataSources 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 - the lookup key object as specified by the user
        Returns:
        the lookup key as needed for matching
      • resolveSpecifiedDataSource

        protected javax.sql.DataSource resolveSpecifiedDataSource(java.lang.Object dataSource)
                                                           throws java.lang.IllegalArgumentException
        Resolve the specified data source object into a DataSource instance.

        The default implementation handles DataSource instances and data source names (to be resolved via a DataSourceLookup).

        Parameters:
        dataSource - the data source value object as specified in the targetDataSources map
        Returns:
        the resolved DataSource (never null)
        Throws:
        java.lang.IllegalArgumentException - in case of an unsupported value type
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        Specified by:
        getConnection in interface javax.sql.DataSource
        Throws:
        java.sql.SQLException
      • getConnection

        public java.sql.Connection getConnection(java.lang.String username,
                                                 java.lang.String password)
                                          throws java.sql.SQLException
        Specified by:
        getConnection in interface javax.sql.DataSource
        Throws:
        java.sql.SQLException
      • unwrap

        public <T> T unwrap(java.lang.Class<T> iface)
                     throws java.sql.SQLException
        Specified by:
        unwrap in interface java.sql.Wrapper
        Overrides:
        unwrap in class AbstractDataSource
        Throws:
        java.sql.SQLException
      • isWrapperFor

        public boolean isWrapperFor(java.lang.Class<?> iface)
                             throws java.sql.SQLException
        Specified by:
        isWrapperFor in interface java.sql.Wrapper
        Overrides:
        isWrapperFor in class AbstractDataSource
        Throws:
        java.sql.SQLException
      • determineCurrentLookupKey

        @Nullable
        protected abstract java.lang.Object determineCurrentLookupKey()
        Determine the current lookup key. This will typically be implemented to check a thread-bound transaction context.

        Allows for arbitrary keys. The returned key needs to match the stored lookup key type, as resolved by the resolveSpecifiedLookupKey(java.lang.Object) method.