是的,它會做到這一點。
默認情況下,Grails中的事務是在服務方法級別處理的。如果方法正常返回,則事務將被提交,如果拋出RuntimeException,事務將被回滾。
注意這意味着即使您在服務器方法中保存對象時使用flush:true on,如果拋出RuntimeException,數據庫更改仍會回滾。
例如:
class MyService {
def fiddle(id,id2){
def domain = Domain.findById(id)
domain.stuff = "A change"
domain.save(flush:true) // will cause hibernate to perform the update statements
def otherDomain = OtherDomain.findById(id2)
otherDomain.name = "Fiddled"
if(!otherDomain.save(flush:true)){ // will also write to the db
// the transaction will be roled back
throw new RuntimeException("Panic what the hell happened")
}
}
}
什麼我不是100%的透明與Grails的是,如果一個檢查的異常是在直java的拋出會發生什麼/彈簧世界上默認行爲是事務攔截器提交交易,通過這可以在配置中重寫。
注意:有一個警告,那就是您的數據庫必須支持您正在更新的表上的事務。是的,這是戳在MySQL :)
這也適用於Domain.withTransaction
方法。
太棒了,謝謝! – RyanLynch 2010-06-05 14:31:03
很高興能成爲服務 – 2010-06-05 14:33:44
擴展RuntimeException的自定義異常應該沒問題吧? 此外,您可以通過設置: dialect = org.hibernate.dialect啓用MySQL中的事務。MySQLInnoDBDialect in dataSource :) – RyanLynch 2010-06-05 15:09:10