6、事务注解Transactional

大家都知道可以用@Transactional来注解业务层来实现声明式事务

@service

@Transactional

public class Service {

public void doSome() {

//执行数据库save操作

throw new Exception();

}

}



if (txInfo.transactionAttribute.rollbackOn(ex)) {

try {

txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());

}

catch (TransactionSystemException ex2) {

logger.error("Application exception overridden by rollback exception", ex);

ex2.initApplicationException(ex);

throw ex2;

}

catch (RuntimeException ex2) {

logger.error("Application exception overridden by rollback exception", ex);

throw ex2;

}

catch (Error err) {

logger.error("Application exception overridden by rollback error", ex);

throw err;

}

}


@Override

public boolean rollbackOn(Throwable ex) {

return (ex instanceof RuntimeException || ex instanceof Error);

}



事务并不会回滚,而如果 throw new RuntimeException(),则事务会回滚。

你可能感兴趣的:(6、事务注解Transactional)