Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning db connection to HikariCP pool with Slick 3.1.x

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?

like image 437
novon Avatar asked Jul 06 '16 23:07

novon


1 Answers

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.

like image 83
marcospereira Avatar answered Sep 21 '22 10:09

marcospereira