在开发过程中遇到过多种事务配置。
是的 ,其实也一直没能体会事务的奥妙,但是加了事务以后就会觉得安心许多,没错 , 是安心;
手动事务管理配置:
TransactionTemplate template = new TransactionTemplate (backendFinanceDataTxManager);
template.setPropagationBehavior (TransactionDefinition.PROPAGATION_REQUIRES_NEW);
return (Integer) template.execute (new TransactionCallback () {
//针对单个方法的事务
//针对单个方法的事务
public Integer doInTransaction (TransactionStatus arg0) {
try {
return _-----执行方法
}
catch (Exception e) {
log.error (e.getMessage (), e);
throw new RuntimeException (e.getMessage (), e);
}
}
});
----------------单个事务回滚的写法, 因为是批量操作之后提交审核的处理,希望一行数据处理完之后即时后面有问题也不回滚前面的操作--------------------------------------
@Override
public void saveCostRefundOrders(final List<CostRefundOrderDto> costRefundOrderDtos) {
TransactionTemplate template = new TransactionTemplate (backendFinanceDataTxManager);
template.setPropagationBehavior (TransactionDefinition.PROPAGATION_REQUIRES_NEW);
template.execute (new TransactionCallbackWithoutResult () {
public void doInTransactionWithoutResult (TransactionStatus arg0) {
try {
for (final CostRefundOrderDto costRefundOrderDto : costRefundOrderDtos) {
//保存并提交审核
saveCostRefundOrderAndSendToCheck(costRefundOrderDto);
}
} catch (Exception e) {
log.error (e.getMessage (), e);
throw new RuntimeException (e.getMessage (), e);
}
}
});
}
----------------单个事务回滚的写法, 因为是批量操作之后提交审核的处理,希望一行数据处理完之后即时后面有问题也不回滚前面的操作--------------------------------------
@Override
public void saveCostRefundOrders(final List<CostRefundOrderDto> costRefundOrderDtos) {
TransactionTemplate template = new TransactionTemplate (backendFinanceDataTxManager);
template.setPropagationBehavior (TransactionDefinition.PROPAGATION_REQUIRES_NEW);
template.execute (new TransactionCallbackWithoutResult () {
public void doInTransactionWithoutResult (TransactionStatus arg0) {
try {
for (final CostRefundOrderDto costRefundOrderDto : costRefundOrderDtos) {
//保存并提交审核
saveCostRefundOrderAndSendToCheck(costRefundOrderDto);
}
} catch (Exception e) {
log.error (e.getMessage (), e);
throw new RuntimeException (e.getMessage (), e);
}
}
});
}