Cannot roll back session in auto-commit mode with Slick

会有一股神秘感。 提交于 2019-12-12 02:26:40

问题


import scala.slick.driver.PostgresDriver
private val postgres = PostgresDriver.simple.Database.forURL(url, driver = driver)


def myMethod(testCode: Session => Any) {
  val session = postgres.createSession()

  session.conn.setAutoCommit(false)
  try {
  testCode(session)
  } finally {
  session.rollback()
  session.close()
}

sbt has following dependencies

"com.typesafe.slick" %% "slick" % "2.1.0",
"com.github.tminglei" %% "slick-pg" % "0.6.3"

Though I have set autocomit to false I get following. I must be missing something?

scala.slick.SlickException: Cannot roll back session in auto-commit mode
at scala.slick.jdbc.JdbcBackend$BaseSession.rollback(JdbcBackend.scala:415)
at mycompany.tests.DatabaseSpec$class.withSession(DatabaseSpec.scala:29)

API that's using myMethod has

tableQuery.insertOrUpdate(tableRow)

above error specific to postgres driver and insertOrUpdate? if I use insert API it rolls back fine with no issue. is there a


回答1:


the session.rollback() implementation in Slick is

def rollback() {
  if(conn.getAutoCommit) throw new SlickException("Cannot roll back session in auto-commit mode")
  doRollback = true
}

Seems like your connections stays in auto commit mode for some reason. Slick doesn't seem to be the cause.



来源:https://stackoverflow.com/questions/25674987/cannot-roll-back-session-in-auto-commit-mode-with-slick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!