事务中处理异常

一个service在事务中处理异常的例子:

@Service("test")
@Transactional
public class TestImpl implements TestService {
    @Override
    public void test() {
        TestEbo t1 = new TestEbo();
        testDao.saveOk(t1);
        try {
            TestEbo t2 = new TestEbo();
            testDao.saveOk(t2);
            
            TestEbo t3 = new TestEbo();
            testDao.saveFail(t3);
            
            TestEbo t4 = new TestEbo();
            testDao.saveOk(t4);
        } catch (Exception e) {
            log.error("+++++++++++++++++++++++++++");
        }
        
        TestEbo t5 = new TestEbo();
        testDao.saveOk(t5);
    }

}

其中saveFail()为:

    @Override
    public void saveFail(TestEbo test) throws Exception {
        saveOk(test);
        throw new Exception("-------------------------");
    }

那么执行完test()方法后,t1、t2、t3、t5都会保存到数据库中,t4不会保存。

posted @ 2016-11-16 22:56  raindream  阅读(407)  评论(0编辑  收藏  举报