ScalikeJDBC Autorollback not working

137 views
Skip to first unread message

Jun Abbott

unread,
May 21, 2018, 1:24:22 PM5/21/18
to ScalikeJDBC Users Group
Hello,

I would like to write a simple scalatest with auto-rollback but unable to make it work:

trait TransactionService {

 
def createTransaction(...)(implicit session: DBSession): Int = {
   
DB localTx { implicit session =>
      sql"...".update().apply()
     
sql"...".update().apply()
     
sql"...".update().apply()
   
}
 }
}

import org.scalatest.{Matchers, fixture}
import scalikejdbc.{DBSession, NamedDB}
import scalikejdbc.config.DBs
import scalikejdbc.scalatest.AutoRollback

class TransactionServiceTest extends fixture.FlatSpec with AutoRollback with Matchers with TransactionService {

 
override def db =  {
   
DBs.setup('default)
   
NamedDB('default).toDB()
 
}

 
override def fixture(implicit session: FixtureParam): Unit = {
 
}

 
"TransactionService" should "be able to create a transaction" in { implicit session: DBSession =>
    createTransaction
(...) should be > 0
  }
}

I'm using latest mariadb with following settings:
# JDBC settings
db.default.driver="com.mysql.cj.jdbc.Driver"
db.default.url="jdbc:mysql://localhost:3306/..."
db.default.user=...
db.default.password=...
# Connection Pool settings
db.default.poolInitialSize=10
db.default.poolMaxSize=20
db.default.poolConnectionTimeoutMillis=1000
db.default.poolValidationQuery="select 1 as one"
db.default.poolFactoryName="commons-dbcp2"

scalikejdbc.global.loggingSQLAndTime.enabled=true
scalikejdbc.global.loggingSQLAndTime.logLevel=debug
scalikejdbc
.global.loggingSQLAndTime.singleLineMode=true

my build.sbt:
"org.scalikejdbc" %% "scalikejdbc" % "3.2.2",
"org.scalikejdbc" %% "scalikejdbc-config" % "3.2.2",
"mysql" % "mysql-connector-java" % "8.0.11",
"org.scalatest" %% "scalatest" % "3.0.1" % Test,
"org.scalikejdbc" %% "scalikejdbc-test" % "3.2.2" % Test

What am I missing? :(

Thanks,
Jun
Message has been deleted

Kazuhiro Sera

unread,
Jan 6, 2019, 4:31:41 PM1/6/19
to Andrew T, ScalikeJDBC Users Group
Hi Jun,

Sorry for the late reply. Your code has the following part:

DB localTx { implicit session =>

The `implicit session` is another session. Thus, the auto-rollback
feature doesn't affect the session. You can test the logic by
extracting the internal logic to another method which doesn't have a
local transaction inside.

Best,
Kaz

On Tue, Oct 23, 2018 at 11:50 PM Andrew T <scy...@gmail.com> wrote:
>
> Would the AutoRollback trait work for you?
>
> http://scalikejdbc.org/documentation/testing.html
> --
> You received this message because you are subscribed to the Google Groups "ScalikeJDBC Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to scalikejdbc-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages