以一个数据库事务:阶隐式标识
def create(model: Model, orderNum: String) = {
db.handle withSession { implicit ss: Session=>
ss.withTransaction { // auto commit now set to false
val result = for {
uid <- repo.user.create(model)
mid <- repo.membership.create(uid)
oid <- repo.orders.create(model, uid, orderNum)
} yield uid
result fold(
e=> { ss.rollback; Left(e) },
s=> { Cache.remove("member.directory"); Right(s) }
)
}
}
}
如果库用户创建的实现需要一个隐含的会议,是相同会议的withTransaction启用会议上面,或者是隐含价值“是一个“而不是”身份?“
def create[T <: User](t: T)(implicit ss: Session) = // what Session is this?
for {
uid <- either(Users.insert(Users(t)), i18n("user not created"))
ur <- either(UserRoles.insert(UserRole(uid, t.role)), i18n("user role not created"))
} yield uid
我能通过在会议明确repo.user.create(model)(ss)
和具有创造采取明确的会议,但我想知道是否更简洁/便利隐含的方式提供了相同的结果,的交易启用会话。
+1,有趣。如您所见,正在尝试通过将会话隐式传递到外部查询方法来在本地事务块之外运行查询。通过明确地传递会话(我在我的“真实”代码中实际做的),我的印象是ScalaQuery将使用显式传递的会话(应该将自动提交设置为false)。我确实通过显式会话传递运行了一些测试,并且在本地事务块中的任何一点失败都会回滚所有执行的查询 – virtualeyes 2012-08-03 15:46:04
再次看SQ源,您是正确的;但是,我没有在外部查询方法中使用withSession或withTransaction;他们收到已经存在的启用事务的会话,这就是为什么当失败时,本地事务块会回滚所有外部执行的查询 – virtualeyes 2012-08-03 15:53:02
这是正确的。如果您在单个事务内朗读多个查询并且一个失败,则所有查询都会回滚。这是Acid – Edmondo1984 2012-08-03 17:37:27