Working with DataSources
It is also possible to provide a javax.sql.DataSource
to the Database.connect
function. This allows you to use more advanced features like connection pooling, and lets you set configuration options like maximum number of connections, connection timeouts, etc.
val db = Database.connect(dataSource)
Example with HikariCP
To use a JDBC connection pool like HikariCP, first set up a HikariConfig
class. This example uses the MySQL JDBC driver (see the official reference for MySQL configuration details):
val config = HikariConfig().apply {
jdbcUrl = "jdbc:mysql://localhost/dbname"
driverClassName = "com.mysql.cj.jdbc.Driver"
username = "username"
password = "password"
maximumPoolSize = 6
// as of version 0.46.0, if these options are set here, they do not need to be duplicated in DatabaseConfig
isReadOnly = false
transactionIsolation = "TRANSACTION_SERIALIZABLE"
}
// Gradle
implementation "mysql:mysql-connector-java:8.0.33"
implementation "com.zaxxer:HikariCP:4.0.3"
Then instantiate a HikariDataSource
with this configuration class and provide it to Database.connect()
:
val dataSource = HikariDataSource(config)
Database.connect(
datasource = dataSource,
databaseConfig = DatabaseConfig {
// set other parameters here
}
)
Last modified: 30 October 2024