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 Summary
ConstructorsConstructorDescriptionShardingKeyDataSourceAdapter(DataSource dataSource) Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.ShardingKeyDataSourceAdapter(DataSource dataSource, ShardingKeyProvider shardingKeyProvider) Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource. -
Method Summary
Modifier 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.DelegatingDataSource
afterPropertiesSet, createShardingKeyBuilder, getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, obtainTargetDataSource, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
-
Constructor Details
-
ShardingKeyDataSourceAdapter
Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.- Parameters:
dataSource- the targetDataSourceto be wrapped
-
ShardingKeyDataSourceAdapter
Create a new instance ofShardingKeyDataSourceAdapter, wrapping the givenDataSource.- Parameters:
dataSource- the targetDataSourceto be wrappedshardingKeyProvider- theShardingKeyProviderused to get the sharding keys
-
-
Method Details
-
setShardingKeyProvider
Set theShardingKeyProviderfor this adapter. -
getConnection
Obtain 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 interfaceDataSource- Overrides:
getConnectionin classDelegatingDataSource- Returns:
- a
Connectionobject representing a direct shard connection - Throws:
SQLException- if an error occurs while creating the connection- See Also:
-
getConnection
Obtain 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 interfaceDataSource- Overrides:
getConnectionin classDelegatingDataSource- Parameters:
username- the database user on whose behalf the connection is being madepassword- the user's password- Returns:
- a
Connectionobject representing a direct shard connection - Throws:
SQLException- if an error occurs while creating the connection
-
createConnectionBuilder
Create a new instance ofConnectionBuilderusing the targetDataSource'screateConnectionBuilder()method and set the appropriate sharding keys from the configuredShardingKeyProvider.- Specified by:
createConnectionBuilderin interfaceDataSource- Overrides:
createConnectionBuilderin classDelegatingDataSource- Returns:
- a ConnectionBuilder object representing a builder for direct shard connections
- Throws:
SQLException- if an error occurs while creating the ConnectionBuilder
-