Server load balancing
You can configure the pool with a list of servers instead of a single server.
MSSQLPool pool = MSSQLPool.pool(Arrays.asList(server1, server2, server3), options);
The pool uses a round-robin load balancing when a connection is created to select different servers.
|
Note
|
this provides load balancing when the connection is created and not when the connection is borrowed from the pool. |
Pool connection initialization
You can use the connectHandler to interact with a connection after it
has been created and before it is inserted in the pool.
pool.connectHandler(conn -> {
conn.query(sql).execute().onSuccess(res -> {
// Release the connection to the pool, ready to be used by the application
conn.close();
});
});
Once you are done with the connection, you should simply close it to signal the pool to use it.
Dynamic connection provider
By default, the pool create connections using ConnectionFactory#connect.
But you can provide your own implementation in Pool#connectionProvider.
Since the provider is asynchronous, it can be used to provide dynamic pool configuration (e.g. password rotation).
pool.connectionProvider(ctx -> {
Future<MSSQLConnectOptions> fut = retrieveOptions();
return fut.compose(connectOptions -> {
// Do not forget to close later
ConnectionFactory factory = MSSQLDriver.INSTANCE.createConnectionFactory(vertx, connectOptions);
return factory.connect(ctx);
});
});
|
Caution
|
When the connection factory becomes useless (e.g. because of a new configuration) it must be closed to release its resources. |