3 笔交易 [英] Slick 3 Transactions
本文介绍了3 笔交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对光滑的 3 文档描述交易的方式感到困惑.我有 2 条看起来像这样的代码:
I'm confused by the way the slick 3 documentation describes transactions. I have slick 2 code that looks like this:
def doSomething(???) = DB.withTransaction { implicit session =>
userDao.doSomething(???)
addressDao.doSomething(???)
contactDao.doSomething(???)
}
如何在 slick 3 中跨事务?
How can i span a transaction in slick 3?
推荐答案
请查看这里的文档 http://slick.typesafe.com/doc/3.0.0/dbio.html#transactions-and-pinned-sessions
我们的想法是将一系列 IO 操作包装到一个 transactionally
中,如下例所示:
The idea is that you wrap a sequence of IO operations into a transactionally
like shown in this example:
val a = (for {
ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
_ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
val f: Future[Unit] = db.run(a)
这种方式 Slick 仍然以被动方式处理所有操作,但它会在一个事务中按顺序运行所有操作.
This way Slick still process all the operations reactively, but it runs them all in one transaction sequentially.
所以你的例子看起来像这样:
So your example would look like this:
def doSomething(???) = (for {
_ <- userDao.doSomething(???)
_ <- addressDao.doSomething(???)
_ <- contactDao.doSomething(???)
} yield()).transactionally
这篇关于3 笔交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文