CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 139
Configuring and Managing Connections
In most real-world scenarios creating connections manually is not a good practice. Each connection can only execute one query at a time and connection establishment has high overhead.
In order to be able to execute multiple queries simultaneously one should use a ConnectionPool
.
The way jasync-sql is configured by default is using a connection pool.
ConnectionPool
is responsible to manage connections, borrow them for query execution and validate they are still alive. Aside from construction, ConnectionPool
has the same interface as a Connection
. Here is how a connection pool is constructed:
// Connection to MySQL DB
Connection connection = new MySQLConnectionBuilder.createConnectionPool(
"jdbc:mysql://$host:$port/$database?user=$username&password=$password");
// Connection to PostgreSQL DB
Connection connection = PostgreSQLConnectionBuilder.createConnectionPool(
"jdbc:postgresql://$host:$port/$database?user=$username&password=$password");
The builder has other flavors. the most detailed one accept a ConnectionPoolConfiguration
with the following properties:
/**
*
* Contains the configuration necessary to connect to a database.
*
* @param host database host, defaults to "localhost"
* @param port database port, defaults to 5432
* @param database database name, defaults to no database
* @param username database username
* @param password password, defaults to no password
* @param maxActiveConnections how many conncetions this pool will keep live
* @param maxIdleTime number of milliseconds for which the objects are going to be kept as idle (not in use by clients of the pool)
* @param maxPendingQueries when there are no more connections, the pool can queue up requests to serve later then there
* are connections available, this is the maximum number of enqueued requests
* @param connectionValidationInterval pools will use this value as the timer period to validate idle objects.
* @param connectionCreateTimeout the timeout for connecting to servers
* @param connectionTestTimeout the timeout for connection tests performed by pools
* @param queryTimeout the optional query timeout
* @param eventLoopGroup the netty event loop group - use this to select native/nio transport.
* @param executionContext the thread pool to run the callbacks on
* @param coroutineDispatcher thread pool for the actor operations of the connection pool
* @param ssl ssl configuration
* @param charset charset for the connection, defaults to UTF-8, make sure you know what you are doing if you
* change this
* @param maximumMessageSize the maximum size a message from the server could possibly have, this limits possible
* OOM or eternal loop attacks the client could have, defaults to 16 MB. You can set this
* to any value you would like but again, make sure you know what you are doing if you do
* change it.
* @param allocator the netty buffer allocator to be used
* @param applicationName optional name to be passed to the database for reporting
* @param interceptors optional delegates to call on query execution
* @param maxConnectionTtl number of milliseconds an object in this pool should be kept alive, negative values mean no aging out
* @param currentSchema optional search_path for the database
* @param socketPath path to unix domain socket file (on the local machine)
*
*/
data class ConnectionPoolConfiguration @JvmOverloads constructor(
val host: String = "localhost",
val port: Int = 5432,
val database: String? = null,
val username: String = "dbuser",
val password: String? = null,
val maxActiveConnections: Int = 1,
val maxIdleTime: Long = TimeUnit.MINUTES.toMillis(1),
val maxPendingQueries: Int = Int.MAX_VALUE,
val connectionValidationInterval: Long = 5000,
val connectionCreateTimeout: Long = 5000,
val connectionTestTimeout: Long = 5000,
val queryTimeout: Long? = null,
val eventLoopGroup: EventLoopGroup = NettyUtils.DefaultEventLoopGroup,
val executionContext: Executor = ExecutorServiceUtils.CommonPool,
val coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default,
val ssl: SSLConfiguration = SSLConfiguration(),
val charset: Charset = CharsetUtil.UTF_8,
val maximumMessageSize: Int = 16777216,
val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT,
val applicationName: String? = null,
val interceptors: List<Supplier<QueryInterceptor>> = emptyList(),
val maxConnectionTtl: Long? = null,
val currentSchema: String? = null,
val socketPath: String? = null
)
In order to help with constructing this object, ConnectionPoolConfigurationBuilder
can be used.
When working with a connection pool, the user no longer responsible to open and close connections manually. So sendQuery()
and sendPreparedStatement()
are automatically acquiring an open connection and give it back to the pool after usage.
The recommended way to use it is to call disconnect().get()
on the shutdown of the server. Then there is no need to call connect
or disconnect
on the actual connections.
The configurations below are more esoteric, hence configured by a system property:
- jasyncDoNotInterceptChecks (
-DjasyncDoNotInterceptChecks=true
) will tell the driver not to intercept test queries. See #188 for more details.