以一個數據庫事務:階隱式標識
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