使用Slick 3进行会话配置/超时

时间:2017-02-24 04:10:04

标签: postgresql scala slick

有没有办法在Slick 3中明确处理会话?我目前有一些看起来像

的代码
def findUserByEmail(email: String): Option[User] = {

    val users = TableQuery[Users]
    val action = users.filter(_.email === email).result.headOption

    val result = db.run(action.transactionally)

    Await.result(result, Duration.Inf)
}

我运行它的前几次工作正常,但后来我开始遇到看起来像连接/会话被打开的问题(见下文)。这段代码在aws lambda函数中运行,我认为我需要更明确地处理会话。我如何在Slick 3中做到这一点?

"errorMessage": "Timeout after 5000ms of waiting for a connection.",
    "errorType": "java.sql.SQLTimeoutException",
    "stackTrace": [
      "com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:233)",
      "com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183)",
      "com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:93)",
      "slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:18)",
      "slick.jdbc.JdbcBackend$BaseSession.<init>(JdbcBackend.scala:424)",
      "slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:47)",
      "slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:38)",
      "slick.basic.BasicBackend$DatabaseDef.acquireSession(BasicBackend.scala:218)",
      "slick.basic.BasicBackend$DatabaseDef.acquireSession$(BasicBackend.scala:217)",
      "slick.jdbc.JdbcBackend$DatabaseDef.acquireSession(JdbcBackend.scala:38)",
      "slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:239)",
      "java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)",
      "java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)",
      "java.lang.Thread.run(Thread.java:745)"
    ],
    "cause": {
      "errorMessage": "FATAL: remaining connection slots are reserved for non-replication superuser connections",
      "errorType": "org.postgresql.util.PSQLException",
      "stackTrace": [
        "org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)",
        "org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2586)",
        "org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:113)",
        "org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)",
        "org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:52)",
        "org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:216)",
        "org.postgresql.Driver.makeConnection(Driver.java:404)",
        "org.postgresql.Driver.connect(Driver.java:272)",

1 个答案:

答案 0 :(得分:0)

您可以尝试设置查询超时。像这样:

db.run(action.transactionally.withStatementParameters(statementInit = st => st.setQueryTimeout(100)))

您还可以在Hikari连接池上设置不同的属性,如下所示:

slick {
      // https://github.com/slick/slick/blob/master/slick-hikaricp/src/main/scala/slick/jdbc/hikaricp/HikariCPJdbcDataSource.scala
      dataSourceClass = "slick.jdbc.DriverDataSource"
      user = ${database.user}
      password = ${database.password}
      url = ${database.url}
      connectionPool = HikariCP
      maxConnections = 50
      numThreads = 10
      queueSize = 5000
      connectionInitSql = "SELECT 1;"
      connectionTestQuery = "SELECT 1;"
      registerMbeans = true

      properties = {
        driver = ${database.driver}
        url = ${database.url}
      }
 }