LocalTx

227 views
Skip to first unread message

Gabriel Raineri

unread,
Nov 1, 2013, 2:31:02 PM11/1/13
to scalikejdbc...@googlegroups.com
Hey Kaz!

What will happen if I run the following code?

db localTx { implicit session
  db localTx {
    ...
  }
}

Will the nested localTx attempt to create a new Transaction? Or will it reuse the existing one? 
In case it creates a new one, will it fail on those RDBMS not supporting nested transactions?

Thanks a lot!

Cheers,
G.

Gabriel Raineri

unread,
Nov 1, 2013, 2:41:57 PM11/1/13
to scalikejdbc...@googlegroups.com
I've just taken a look at the code and it seems it will only set autoCommit in false when calling tx.begin() (in DB.scala file)

So I guess it will reuse the existing transaction. Am I correct?

Kazuhiro Sera

unread,
Nov 1, 2013, 8:20:37 PM11/1/13
to Gabriel Raineri, scalikejdbc...@googlegroups.com
Hi,

If you re-use DB instance, same connection will be used. But basically
localTx attempt to create a new transaction within the block, so I
don't recommend such a confusing coding style.

See also:

https://github.com/seratch/scalikejdbc/wiki/GettingStarted#transaction

Thanks,
-Kaz
> --
> 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/groups/opt_out.

Gabriel Raineri

unread,
Nov 3, 2013, 11:49:08 AM11/3/13
to scalikejdbc...@googlegroups.com
Correct! Got it.

The example I sent was just a extreme simplification of services interaction, such as:

GroupService.scala
def deleteGroup(...) = {
  DB localTx { implicit session
    groupDao.deleteGroup(...)
    userService.deleteUsersInGroup(...)
  }
}

UserService.scala
def deleteUsersInGroup(...) = {
  DB localTx { implicit session 
    ...
  }
}

GroupDao.scala
def delete(...) = {
  DB withinTx { ... }
}

Reply all
Reply to author
Forward
0 new messages