I'm setting up a slick Database object from typesafe config like so:
import com.typesafe.config.Config
class DatabaseService(configKey: String, config: Config) {
val driver = slick.driver.MySQLDriver
import driver.api._
val db = Database.forConfig(configKey, config)
}
The config object tells Slick to use HikariCP, like so:
db {
numThreads = 5
connectionTimeout = 30000
maximumPoolSize = 26
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/some_db?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false"
user = "root"
password = "root"
connectionPool = "HikariCP"
}
After instantiating the DatabaseService I can then run queries by running dbService.db.run(someQuery)
.
The first question is do I need to do something to get a connection from the pool or does that happen behind the scenes when I invoke db.run()
?
Secondly, once that query or queries execute how do I return the current database connection to the connection pool?
The first question is do I need to do something to get a connection from the pool or does that happen behind the scenes when I invoke db.run()?
That happens behind the scenes.
Secondly, once that query or queries execute how do I return the current database connection to the connection pool?
Thats also happens behind the scenes.
Here is the relevant code for both questions . Basically, is acquires a session, executes the given action (in your case, someQuery
) and then release the session (closes it). Digging a little more at the code, you can see that the JDBC implementation creates a BaseSession
which holds a connection and also closes it when the close method is invoked .
Also, from the docs :
Database connections and transactions are managed automatically by Slick. By default connections are acquired and released on demand and used in auto-commit mode.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.