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 java. 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());
getConnection, createConnectionBuilder(), UserCredentialsDataSourceAdapter
| Modifier and Type | Field and Description |
|---|---|
| private ShardingKeyProvider |
| Access | Constructor and Description |
|---|---|
| public | ShardingKeyDataSourceAdapter(DataSource
the target dataSource)DataSource to be wrappedCreate a new instance of |
| public | ShardingKeyDataSourceAdapter(DataSource
the target dataSource, ShardingKeyProvider DataSource to be wrappedthe shardingKeyProvider)ShardingKeyProvider used to get the
sharding keysCreate a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public ConnectionBuilder | Returns: a ConnectionBuilder object representing a builder for direct shard connectionsOverrides org. Overrides default javax. ConnectionBuilder using the target DataSource's
createConnectionBuilder() method and set the appropriate sharding keys
from the configured ShardingKeyProvider.
|
| public Connection | Returns: aConnection object representing a direct shard connectionOverrides org. Implements javax. |
| public Connection | Returns: aConnection object representing a direct shard connectionthe database user on whose behalf the connection is being made username, String the user's password password)Overrides org. Implements javax. |
| public void | setShardingKeyProvider(ShardingKeyProvider shardingKeyProvider)
Set the |
| shardingkeyProvider | back to summary |
|---|---|
| private ShardingKeyProvider shardingkeyProvider
| |
| ShardingKeyDataSourceAdapter | back to summary |
|---|---|
| public ShardingKeyDataSourceAdapter(DataSource dataSource) Create a new instance of
| |
| ShardingKeyDataSourceAdapter | back to summary |
|---|---|
| public ShardingKeyDataSourceAdapter(DataSource dataSource, ShardingKeyProvider shardingKeyProvider) Create a new instance of
| |
| createConnectionBuilder | back to summary |
|---|---|
| public ConnectionBuilder createConnectionBuilder() throws SQLException Overrides org. Overrides default javax. Create a new instance of
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection() throws SQLException Overrides org. Implements javax. 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
| |
| getConnection | back to summary |
|---|---|
| public Connection getConnection(String username, String password) throws SQLException Overrides org. Implements javax. 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
| |
| setShardingKeyProvider | back to summary |
|---|---|
| public void setShardingKeyProvider(ShardingKeyProvider shardingKeyProvider) Set the | |