问题
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