在Grails中Service層默認就是支持事務的,事務傳播級別默認是PROPAGATION_REQUIRED. 當然你也可以設置transactional = false.
如果transactional 設置為true, 則在Service層如何方法內部拋出RuntimeException類型異常, 所有操作將會回滾.
class UserService {
boolean transactional = true
public boolean register(User user, UserInfo userInfo) throws RuntimeException {
if (user.save()) {
userInfo.user = user
if (userInfo.save()) {
return true
} else {
throw new RuntimeException ('ServiceException: UserService.register()...');
}
} else {
throw new RuntimeException ('ServiceException: UserService.register()...');
}
}
}
這樣不管哪個保存失敗, 數據都將會回滾!
posted on 2008-07-21 21:29
周銳 閱讀(1700)
評論(0) 編輯 收藏 所屬分類:
Groovy&Grails