Top Description Fields Constructors Methods
org.springframework.jdbc.datasource

public Class ShardingKeyDataSourceAdapter

extends DelegatingDataSource
Class Inheritance
Imports
java.sql.Connection, .ConnectionBuilder, .SQLException, .ShardingKey, javax.sql.DataSource, org.springframework.lang.Nullable

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 java.sql.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());
Authors
Mohamed Lahyane (Anir), Juergen Hoeller
Since
6.1.2
See Also
getConnection, createConnectionBuilder(), UserCredentialsDataSourceAdapter

Field Summary

Modifier and TypeField and Description
private ShardingKeyProvider

Constructor Summary

AccessConstructor and Description
public
ShardingKeyDataSourceAdapter(DataSource
the target DataSource to be wrapped
dataSource
)

Create a new instance of ShardingKeyDataSourceAdapter, wrapping the given DataSource.

public
ShardingKeyDataSourceAdapter(DataSource
the target DataSource to be wrapped
dataSource
,
ShardingKeyProvider
the ShardingKeyProvider used to get the sharding keys
shardingKeyProvider
)

Create a new instance of ShardingKeyDataSourceAdapter, wrapping the given DataSource.

Method Summary

Modifier and TypeMethod and Description
public ConnectionBuilder

Returns:

a ConnectionBuilder object representing a builder for direct shard connections
createConnectionBuilder
()

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.createConnectionBuilder.

Overrides default javax.sql.DataSource.createConnectionBuilder.

Create a new instance of ConnectionBuilder using the target DataSource's createConnectionBuilder() method and set the appropriate sharding keys from the configured ShardingKeyProvider.

public Connection

Returns:

a Connection object representing a direct shard connection
getConnection
()

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Obtain a connection to the database shard using the provided sharding key and super sharding key (if available).

public Connection

Returns:

a Connection object representing a direct shard connection
getConnection
(String
the database user on whose behalf the connection is being made
username
,
String
the user's password
password
)

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.getConnection.

Obtain a connection to the database shard using the provided username and password, considering the sharding keys (if available) and the given credentials.

public void
setShardingKeyProvider(ShardingKeyProvider shardingKeyProvider)

Set the ShardingKeyProvider for this adapter.

Inherited from org.springframework.jdbc.datasource.DelegatingDataSource:
afterPropertiesSetcreateShardingKeyBuildergetLoginTimeoutgetLogWritergetParentLoggergetTargetDataSourceisWrapperForobtainTargetDataSourcesetLoginTimeoutsetLogWritersetTargetDataSourceunwrap

Field Detail

shardingkeyProviderback to summary
private ShardingKeyProvider shardingkeyProvider
Annotations
@Nullable

Constructor Detail

ShardingKeyDataSourceAdapterback to summary
public ShardingKeyDataSourceAdapter(DataSource dataSource)

Create a new instance of ShardingKeyDataSourceAdapter, wrapping the given DataSource.

Parameters
dataSource:DataSource

the target DataSource to be wrapped

ShardingKeyDataSourceAdapterback to summary
public ShardingKeyDataSourceAdapter(DataSource dataSource, ShardingKeyProvider shardingKeyProvider)

Create a new instance of ShardingKeyDataSourceAdapter, wrapping the given DataSource.

Parameters
dataSource:DataSource

the target DataSource to be wrapped

shardingKeyProvider:ShardingKeyProvider

the ShardingKeyProvider used to get the sharding keys

Method Detail

createConnectionBuilderback to summary
public ConnectionBuilder createConnectionBuilder() throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.createConnectionBuilder.

Overrides default javax.sql.DataSource.createConnectionBuilder.

Create a new instance of ConnectionBuilder using the target DataSource's createConnectionBuilder() method and set the appropriate sharding keys from the configured ShardingKeyProvider.

Returns:ConnectionBuilder

a ConnectionBuilder object representing a builder for direct shard connections

Annotations
@Override
Exceptions
SQLException:
if an error occurs while creating the ConnectionBuilder
getConnectionback to summary
public Connection getConnection() throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.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.

Returns:Connection

a Connection object representing a direct shard connection

Annotations
@Override
Exceptions
SQLException:
if an error occurs while creating the connection
See Also
createConnectionBuilder()
getConnectionback to summary
public Connection getConnection(String username, String password) throws SQLException

Overrides org.springframework.jdbc.datasource.DelegatingDataSource.getConnection.

Implements javax.sql.DataSource.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.

Parameters
username:String

the database user on whose behalf the connection is being made

password:String

the user's password

Returns:Connection

a Connection object representing a direct shard connection

Annotations
@Override
Exceptions
SQLException:
if an error occurs while creating the connection
setShardingKeyProviderback to summary
public void setShardingKeyProvider(ShardingKeyProvider shardingKeyProvider)

Set the ShardingKeyProvider for this adapter.