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 ofConnectionBuilder
using 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.void
setShardingKeyProvider
(ShardingKeyProvider shardingKeyProvider) Set theShardingKeyProvider
for this adapter.
-
Constructor Details
-
ShardingKeyDataSourceAdapter
Create a new instance ofShardingKeyDataSourceAdapter
, wrapping the givenDataSource
.- Parameters:
dataSource
- the targetDataSource
to be wrapped
-
ShardingKeyDataSourceAdapter
Create a new instance ofShardingKeyDataSourceAdapter
, wrapping the givenDataSource
.- Parameters:
dataSource
- the targetDataSource
to be wrappedshardingKeyProvider
- theShardingKeyProvider
used to get the sharding keys
-
-
Method Details
-
setShardingKeyProvider
Set theShardingKeyProvider
for 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:
getConnection
in interfaceDataSource
- Overrides:
getConnection
in classDelegatingDataSource
- Returns:
- a
Connection
object 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:
getConnection
in interfaceDataSource
- Overrides:
getConnection
in classDelegatingDataSource
- Parameters:
username
- the database user on whose behalf the connection is being madepassword
- the user's password- Returns:
- a
Connection
object representing a direct shard connection - Throws:
SQLException
- if an error occurs while creating the connection
-
createConnectionBuilder
Create a new instance ofConnectionBuilder
using the targetDataSource
'screateConnectionBuilder()
method and set the appropriate sharding keys from the configuredShardingKeyProvider
.- Specified by:
createConnectionBuilder
in interfaceDataSource
- Overrides:
createConnectionBuilder
in classDelegatingDataSource
- Returns:
- a ConnectionBuilder object representing a builder for direct shard connections
- Throws:
SQLException
- if an error occurs while creating the ConnectionBuilder
-