如何在 play 2.3 中尝试修改后的异常代码

How to try modified anorm code in play 2.3

我正在从 Play 迁移我的应用! 2.1 至 2.3。

编译错误消失了,几乎没有失败的测试。

其中一个是异常相关的。

[error]    NullPointerException:   (BatchSql.scala:100)
[error] anorm.BatchSql$class.execute(BatchSql.scala:100)
[error] anorm.BatchSql$Copy.execute(BatchSql.scala:193)
[error] models.StoreUser$.insertByCsv(User.scala:314)

听起来我需要破解 Anorm 代码。我参考以下页面来编译Play!来自来源:

https://www.playframework.com/documentation/2.3.x/BuildingFromSource

$ git clone git://github.com/playframework/playframework.git
$ cd playframework
$ git checkout remotes/origin/2.3.x

作为起点,我尝试改变 playframework/framework/src/anorm/src/main/scala/anorm/BatchSql.scala

  def execute()(implicit connection: Connection): Array[Int] = {
    val s = getFilledStatement(connection)
    println("*** statement = " + s)
Thread.sleep(30000)
    s.executeBatch()
  }

本地编译发布:

$ PLAY_OPTS=-Dscala.version=2.11.6 ./build clean publish-local

删除我的常春藤缓存:

$ rm -rf ~/.ivy2/cache

更改我项目的 project/plugins.sbt:

//addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.8")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3-SNAPSHOT")

Build.sbt 文件:

libraryDependencies ++= Seq(
  "postgresql" % "postgresql" % "9.1-901.jdbc4",
  "com.typesafe.play" %% "play-mailer" % "2.4.0",
...
  jdbc,
//  anorm,
  "com.typesafe.play" %% "anorm" % "2.3-SNAPSHOT",
  cache,
  ws,
  filters
)

重新加载我的项目并再次测试我失败的测试。

但是没有显示任何打印消息('*** statement = xxx'")。

如何使用修改后的 Anorm 代码?

更新:

我 git 仅从 https://github.com/playframework/anorm and invoked publish-local. In this way, now I can use my own compiled anorm in Play!. The root cause is that recent version of anorm requires at least one list of paramters when invoking batch query (https://github.com/playframework/anorm/blob/master/docs/manual/working/scalaGuide/main/sql/ScalaAnorm.md) 克隆了异常。

必须使用至少一个参数列表调用批量更新。如果执行批处理时强制第一个参数列表为空(例如Nil),则只会执行一条语句(不带参数),相当于SQL(statement).executeUpdate()。

如果没有提供参数,将抛出NPE。如果参数列表为空,我修复了我的应用程序不调用 execute() 方法。

所以您的代码似乎引发了与 issue #14 相同的误用:不应该在没有参数的情况下调用批处理。

下一个版本将阻止此类调用。