-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support direct shard query routing in Spring JDBC #30988
Support direct shard query routing in Spring JDBC #30988
Conversation
This commit introduces a DataSource proxy, that changes the behavior of the getConnection method to use the `createConnectionBuilder()` api to acquire direct shard connections. The shard connection is acquired by specifying a `ShardingKey` that is correspondent to the wanted shard. The sharding key can be specified either by setting the thread bound sharding key value, which will be used later by the `getConnection()` method, or by defining a `ShardingKeyProvider` which is used in the `getConnection` method to get the ShardingKey. If both ways are used at the same time, the thread-bound sharding key is the one that will be used. The goal of binding the sharding key to the current thread, is that we want to specify the sharding key at one level, and want it to be used later in another level, without the need to propagate it, for example set the thread-bound sharding key, and execute a `JdbcTemplate.query(SQL)` method that that will call `DataSource.getConnection()`, and as the sharding key is bound to the thread, it can be acquired to get the shard connection.
… shard. This commit introduces a set of classes with the goal of providing an API to execute queries using direct shard routing. `DirectShardCallbackTemplate` provides `execute` methods that take a sharding key and a callback, so that the jdbc queries within that callback will be executed on the shard determined by the specified sharding key.
While sharding would definitely be an interesting addition to the Spring JDBC module, there are concerns about transaction integration: The common connection pools out there do not support the JDBC 4.3 As for |
Concerning the approach using a @Override
public Connection getConnection() throws SQLException {
try {
return createConnectionBuilder().build();
}
catch (SQLFeatureNotSupportedException ex) {
Connection con = obtainDataSource().getConnection();
con.setShardingKey(key);
return con;
}
} Is that an acceptable solution for this issue ? |
I will continue the work on this feature in a new PR #31506. |
This PR adds Spring JDBC support for direct database operation routing in sharded databases. The main goal is to enable the routing of queries directly to specific shards. This functionality is achieved by acquiring a shard connection through the JDBC API
DataSource.createConnectionBuilder().shardingKey(key).build()
.Note: The sharding key is a value that determines which shard a particular data entry should be stored on or retrieved from within the sharded database.
The PR introduces two methods for executing direct shard queries: