Class ShardingKeyDataSourceAdapter
java.lang.Object
org.springframework.jdbc.datasource.DelegatingDataSource
org.springframework.jdbc.datasource.ShardingKeyDataSourceAdapter
- All Implemented Interfaces:
- Wrapper,- CommonDataSource,- DataSource,- InitializingBean
An adapter for a target 
DataSource, designed to apply sharding keys, if specified,
 to every standard #getConnection call, returning a direct connection to the shard
 corresponding to the specified sharding key value. All other methods simply delegate
 to the corresponding methods of the target DataSource.
 The target DataSource must implement the createConnectionBuilder() method;
 otherwise, a SQLFeatureNotSupportedException will be thrown when attempting
 to acquire shard connections.
 
This adapter needs to be configured with a ShardingKeyProvider callback which is
 used to get the current sharding keys for every #getConnection call, for example:
 
 ShardingKeyDataSourceAdapter dataSourceAdapter = new ShardingKeyDataSourceAdapter(dataSource);
 dataSourceAdapter.setShardingKeyProvider(() -> dataSource.createShardingKeyBuilder()
     .subkey(SecurityContextHolder.getContext().getAuthentication().getName(), JDBCType.VARCHAR).build());
 - Since:
- 6.1.2
- Author:
- Mohamed Lahyane (Anir), Juergen Hoeller
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionShardingKeyDataSourceAdapter(DataSource dataSource) Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.ShardingKeyDataSourceAdapter(DataSource dataSource, ShardingKeyProvider shardingKeyProvider) Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.
- 
Method SummaryModifier and TypeMethodDescriptionCreate a new instance ofConnectionBuilderusing the targetDataSource'screateConnectionBuilder()method and set the appropriate sharding keys from the configuredShardingKeyProvider.Obtain a connection to the database shard using the provided sharding key and super sharding key (if available).getConnection(String username, String password) Obtain a connection to the database shard using the provided username and password, considering the sharding keys (if available) and the given credentials.voidsetShardingKeyProvider(ShardingKeyProvider shardingKeyProvider) Set theShardingKeyProviderfor this adapter.Methods inherited from class org.springframework.jdbc.datasource.DelegatingDataSourceafterPropertiesSet, createShardingKeyBuilder, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
- 
Constructor Details- 
ShardingKeyDataSourceAdapterCreate a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.- Parameters:
- dataSource- the target- DataSourceto be wrapped
 
- 
ShardingKeyDataSourceAdapterCreate a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.- Parameters:
- dataSource- the target- DataSourceto be wrapped
- shardingKeyProvider- the- ShardingKeyProviderused to get the sharding keys
 
 
- 
- 
Method Details- 
setShardingKeyProviderSet theShardingKeyProviderfor this adapter.
- 
getConnectionObtain a connection to the database shard using the provided sharding key and super sharding key (if available).The sharding key is obtained from the configured ShardingKeyProvider.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Returns:
- a Connectionobject representing a direct shard connection
- Throws:
- SQLException- if an error occurs while creating the connection
- See Also:
 
- 
getConnectionObtain a connection to the database shard using the provided username and password, considering the sharding keys (if available) and the given credentials.The sharding key is obtained from the configured ShardingKeyProvider.- Specified by:
- getConnectionin interface- DataSource
- Overrides:
- getConnectionin class- DelegatingDataSource
- Parameters:
- username- the database user on whose behalf the connection is being made
- password- the user's password
- Returns:
- a Connectionobject representing a direct shard connection
- Throws:
- SQLException- if an error occurs while creating the connection
 
- 
createConnectionBuilderCreate a new instance ofConnectionBuilderusing the targetDataSource'screateConnectionBuilder()method and set the appropriate sharding keys from the configuredShardingKeyProvider.- Specified by:
- createConnectionBuilderin interface- DataSource
- Overrides:
- createConnectionBuilderin class- DelegatingDataSource
- Returns:
- a ConnectionBuilder object representing a builder for direct shard connections
- Throws:
- SQLException- if an error occurs while creating the ConnectionBuilder
 
 
-