使用Play和Slick连接到MSSQL(jtds)

时间:2013-08-02 20:01:43

标签: sql-server scala playframework jtds slick

我有一个项目已经完成了大约一个月,我使用Play 2.1.1,光滑和MySQL。现在我需要将其转移到MSSQL,我遇到了一些问题。

我的应用程序编译正常,并找到驱动程序,但每当我尝试建立连接时,我在网站上得到[RuntimeException: java.lang.ExceptionInInitializerError],并且我的控制台中出现了更详细的错误:

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.ExceptionInInitializerError]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
    at play.api.DefaultApplication.handleError(Application.scala:383) ~[play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:326) ~[play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:324) ~[play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.1]java.lang.RuntimeException: java.lang.ExceptionInInitializerError
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:222) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:108) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:106) ~[play_2.10.jar:2.1.1]Caused by: java.lang.ExceptionInInitializerError: null
    at controllers.Application$$anonfun$addLicense$1.apply(Application.scala:18) ~[na:na]
    at controllers.Application$$anonfun$addLicense$1.apply(Application.scala:17) ~[na:na]
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:217) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.1.1]
Caused by: play.api.Configuration$$anon$1: Configuration error[Slick error : Unknown jdbc driver found in application.conf: [net.sourceforge.jtds.jdbc.Driver]]
    at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:80) ~[play_2.10.jar:2.1.1]
    at play.api.Configuration.reportError(Configuration.scala:558) ~[play_2.10.jar:2.1.1]
    at play.api.db.slick.Config$.driver(Config.scala:21) ~[play-slick_2.10-0.3.3.jar:0.3.3]
    at play.api.db.slick.Config$.driver$lzycompute(Config.scala:7) ~[play-slick_2.10-0.3.3.jar:0.3.3]
    at play.api.db.slick.Config$.driver(Config.scala:7) ~[play-slick_2.10-0.3.3.jar:0.3.3]
    at models.License$.<init>(License.scala:38) ~[na:na]

我发现的最有趣的事情就是这条线:

Caused by: play.api.Configuration$$anon$1: Configuration error[Slick error : Unknown jdbc driver found in application.conf: [net.sourceforge.jtds.jdbc.Driver]]

它找到了驱动程序,因为如果我输入了错误的名称,它会给我一个错误;输入它就不会。我的lib目录中有jtds驱动程序。

我也在启动时连接到db

[info] play - database [default] connected at jdbc:jtds:sqlserver://ServerAddress/EGLC

其他一些信息:

application.conf

db.default.url="jdbc:jtds:sqlserver://ServerAddress/EGLC"
db.default.driver=net.sourceforge.jtds.jdbc.Driver
db.default.user="sa"
db.default.password="pass"
slick.default="models.*"

Build.scala

val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
"mysql" % "mysql-connector-java" % "5.1.25",
"net.sourceforge.jtds" % "jtds" % "1.3.1",
"com.typesafe.play" %% "play-slick" % "0.3.3"
)

编辑当我刷新页面时虽然我得到一个不同的例外,但是一组新的错误甚至没有提到jtds

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class models.Location$]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
    at play.api.DefaultApplication.handleError(Application.scala:383) ~[play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:326) ~[play_2.10.jar:2.1.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:324) ~[play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.1]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.1]java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class models.Location$
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:222) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:18) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:108) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:106) ~[play_2.10.jar:2.1.1]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class models.Location$
    at controllers.Application$$anonfun$locations$1$$anonfun$apply$12$$anonfun$apply$13.apply(Application.scala:47) ~[na:na]
    at controllers.Application$$anonfun$locations$1$$anonfun$apply$12$$anonfun$apply$13.apply(Application.scala:46) ~[na:na]
    at controllers.Secured$$anonfun$IsAuthenticated$3$$anonfun$apply$3.apply(Auth.scala:38) ~[na:na]
    at controllers.Secured$$anonfun$IsAuthenticated$3$$anonfun$apply$3.apply(Auth.scala:38) ~[na:na]
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:217) ~[play_2.10.jar:2.1.1]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109) ~[play_2.10.jar:2.1.1]

3 个答案:

答案 0 :(得分:5)

play-slick不知道jTDS driver。一般情况下光滑supports SQL server,播放光滑makes a connection from driver class name to Slick driver

private def driverByName : String => Option[ExtendedDriver] = Map(
   "org.apache.derby.jdbc.EmbeddedDriver" -> DerbyDriver
   ,"org.h2.Driver" -> H2Driver
   ,"org.hsqldb.jdbcDriver" -> HsqldbDriver
   ,"com.mysql.jdbc.Driver" -> MySQLDriver
   ,"org.postgresql.Driver" -> PostgresDriver
   ,"org.sqlite.JDBC" -> SQLiteDriver
   ,"com.microsoft.sqlserver.jdbc.SQLServerDriver" -> SQLServerDriver
).get(_)

正如您所看到的,支持Microsoft的驱动程序,而jTDS则不支持。您现在可以使用Microsoft的JDBC驱动程序或修改play-slick。变化很简单。只需添加一行

即可
,"net.sourceforge.jtds.jdbc.Driver" -> SQLServerDriver

它应该有用。

我在项目的GitHub页面上看到了issue that you have raised。我发了一个pull request that should resolve this issue。也许它会被合并到play-slick的下一个版本中。

答案 1 :(得分:1)

我知道这是一个很老的帖子,但只是想帮助谁来到这里。使用Slick 3.2.3使用Microsoft SQL Server真的很容易。对于您的SBT项目,以下配置需要转到application.conf文件

sqlserver = {
 driver = "slick.jdbc.SQLServerProfile$"
 db {
  host = ${?SQLSERVER_HOST}
  port = ${?SQLSERVER_PORT}
  databaseName = ${?SQLSERVER_DB_NAME}

  url = "jdbc:sqlserver://"${sqlserver.db.host}":"${sqlserver.db.port}";databaseName="${sqlserver.db.databaseName}
  user = ${?SQLSERVER_USERNAME}
  password = ${?SQLSERVER_PASSWORD}
 }
}

以下代码片段可用于获取数据库实例

val dbConfig: DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig("sqlserver")
val db: JdbcProfile#Backend#Database = dbConfig.db

为了为FRM启用MS SQL API,可以使用以下导入

 import slick.jdbc.SQLServerProfile.api._

我认为使用一个工作示例很容易理解设置,这可以在这里找到: Using Microsoft SQL Server with Scala Slick

答案 2 :(得分:0)

我应该在项目中添加jtds驱动程序作为依赖项:

val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
"com.typesafe.play" %% "play-slick" % "0.3.3",
"net.sourceforge.jtds" % "jtds" % "1.3.0"
)