posts - 431,  comments - 344,  trackbacks - 0
在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 周锐 阅读(1675) 评论(0)  编辑  收藏 所属分类: Groovy&Grails

只有注册用户登录后才能发表评论。


网站导航: