如何在 Slick 中为 MySQL 连接设置 socketTimeout?

How to set socketTimeout in Slick for MySQL connection?

HikariCP 在 Slick 3 库中用于配置连接池。

根据HikariCP Rapid-Recovery in order to be able "to recover from database restart or network partition events" its recommended to set socketTimeout as its disabled by default.

问题是如何在 slick 中设置 socketTimeout?

在 application.conf 中添加 socketTimeout 作为 JDBC URL

的一部分
db {
  profile = "slick.jdbc.MySQLProfile$"
  db {
    url = "jdbc:mysql://localhost:3306/dbname?socketTimeout=30000"
    driver = com.mysql.cj.jdbc.Driver
    user = "<user>"
    password = "<password>"
  }
}

来自MySQL Connector/J 8.0 documentation

Configuration properties can be set in one of the following ways:

  • Using the set*() methods on MySQL implementations of jdbc url java.sql.DataSource (which is the preferred method when using implementations of java.sql.DataSource)
  • As a key-value pair in the java.util.Properties instance passed to DriverManager.getConnection() or Driver.connect()
  • As a JDBC URL parameter in the URL