事务中处理异常
一个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不会保存。

浙公网安备 33010602011771号