Developing applications that use ConnectionBuilder and ShardingKey for efficient data access
You can write applications that use cached ConnectionBuilder
and
ShardingKey
instances for database connection requests. Reusing existing cached
connections is more efficient because a new connection isn't created for each database connection
request.
Before you begin
To use ConnectionBuilder
and sharding, your Liberty application requires:
- A database that supports sharding
- A JDBC driver that supports connectivity to the database at the JDBC 4.3 level, and which also supports sharding
- A Liberty server that is configured to
use the
jdbc-4.3
feature and that runs on Java SE 11
About this task
The JDBC 4.3 specification introduces the ConnectionBuilder
as a new way to
request database connections. ConnectionBuilder
allows a sharding key and super
sharding key to be specified on a connection request, as well as a user and password.
When connecting through a data source that is managed by the application server, the data source
aims to fulfill connection requests by matching and reusing an existing connection from the
connection pool rather than creating a new connection. The JDBC 4.3 specification leaves out an
important detail for matching connections that have a sharding key, in that it places no requirement
on the JDBC driver to implement a ShardingKey.equals
method, such that multiple
instances can be compared for equality. This means that instances of ShardingKey
that logically represent the same key cannot match, causing new connections to be created rather
than reusing existing pooled connections, which later end up being discarded. Some JDBC drivers
might choose to implement the method even though the specification does not require it, so that
connections can match. However, even if the JDBC driver you are using does not do so, you can write
your application to make connection requests efficiently. You can cache a single instance of
ShardingKey
within your application and always use the cached copy when requesting
connections.
In addition to caching the sharding key, you can also cache preconfigured
ConnectionBuilder
instances that you use to make all of your connection requests.
When caching ConnectionBuilder
, ConnectionBuilder
is not
threadsafe for writes. Therefore, perform all the write operations from a single thread prior to
invoking the build method; for example:
conBuilder = dataSource.createConnectionBuilder()
.user(u)
.password(p)
.shardingKey(sk)
.superShardingKey(ssk);
It is then safe to cache and reuse the connection builder instance and to invoke its build method from any number of other threads, including concurrently, as long as you avoid invoking the write methods after this point.