Slick无法连接到MSSQL数据库

时间:2018-11-21 13:34:14

标签: sql-server scala jdbc slick

我想使用Slick(3.2.3)连接到MSSQL数据库。

当前,我的项目如下。

somedomain.com中,我有

application.conf

“ somehost”看起来像XX.X.XX.XX,其中X是数字。

我的somedbname = { driver = "slick.jdbc.SQLServerProfile$" db { host = "somehost" port = "someport" databaseName = "Recupel.Datawarehouse" url = "jdbc:sqlserver://"${somedbname.db.host}":"${somedbname.db.port}";databaseName="${somedbname.db.databaseName}";" user = "someuser" password = "somepassword" } } 包含

build.sbt

包含“主要”对象的文件包含

name := "test-slick"

version := "0.1"

scalaVersion in ThisBuild := "2.12.7"

libraryDependencies ++= Seq(
  "com.typesafe.slick" %% "slick" % "3.2.3",
  "com.typesafe.slick" %% "slick-hikaricp" % "3.2.3",
  "org.slf4j" % "slf4j-nop" % "1.6.4",
  "com.microsoft.sqlserver" % "mssql-jdbc" % "7.0.0.jre10"
)

根据我可以找到的所有文档,这应该足以连接到数据库。但是,当我运行它时,我得到了

import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile
import slick.jdbc.SQLServerProfile.api._

import scala.concurrent.Await
import scala.concurrent.duration._


  val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig("somedbname")

  val db: JdbcProfile#Backend#Database = dbConfig.db

  def main(args: Array[String]): Unit = {
    try {

      val future = db.run(sql"SELECT * FROM somettable".as[(Int, String, String, String, String,
        String, String, String, String, String, String, String)])

      println(Await.result(future, 10.seconds))

    } finally {
      db.close()
    }


  }

}

也许相关且令人讨厌,当我第二次(及以后)运行此代码时,却收到以下错误:

[error] (run-main-0) java.sql.SQLTransientConnectionException: somedbname.db - Connection is not available, request timed out after 1004ms.
[error] java.sql.SQLTransientConnectionException: somedbname.db - Connection is not available, request timed out after 1004ms.
[error]         at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
[error]         at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
[error]         at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
[error]         at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
[error]         at slick.jdbc.hikaricp.HikariCPJdbcDataSource.createConnection(HikariCPJdbcDataSource.scala:14)
[error]         at slick.jdbc.JdbcBackend$BaseSession.<init>(JdbcBackend.scala:453)
[error]         at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:46)
[error]         at slick.jdbc.JdbcBackend$DatabaseDef.createSession(JdbcBackend.scala:37)
[error]         at slick.basic.BasicBackend$DatabaseDef.acquireSession(BasicBackend.scala:249)
[error]         at slick.basic.BasicBackend$DatabaseDef.acquireSession$(BasicBackend.scala:248)
[error]         at slick.jdbc.JdbcBackend$DatabaseDef.acquireSession(JdbcBackend.scala:37)
[error]         at slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:274)
[error]         at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
[error]         at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
[error]         at java.base/java.lang.Thread.run(Thread.java:844)
[error] Nonzero exit code: 1

这迫使我每次都要杀死并重新加载sbt。

我在做什么错?值得注意的是:我可以使用类似valentina的软件使用相同的凭据连接到数据库。

1 个答案:

答案 0 :(得分:1)

按照@MarkRotteveel的建议,在this link之后,我找到了解决方案。

首先,我显式设置驱动程序,添加行

driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"

在数据库字典中,password = "somepassword"之后。

第二,默认超时(一秒钟后)对于我的目的来说似乎太短了,因此我添加了这一行 connectionTimeout = "30 seconds" 在上一行driver之后,仍在db字典中。

现在可以了。