原生servlet事物处理


/**
* 将业务加入事务控制
* @param service
* @throws Exception
*/
public static void transaction(TransactionService service) throws Exception{
Connection conn = getConnection();
Integer level = TRANSACTION_LEVEL.get();
Boolean autoCommit = null;
if(level==null){
autoCommit = conn.getAutoCommit();
level = 1;
}else{
level = level + 1;
}
TRANSACTION_LEVEL.set(level);
try {
if(conn.getAutoCommit()){
conn.setAutoCommit(false);
}
service.execute();
level = level - 1;
TRANSACTION_LEVEL.set(level);
if(level==0){
conn.commit();
TRANSACTION_LEVEL.remove();
}
}catch (Exception e){
if(conn!=null){
try {
conn.rollback();
/**
* 数据回滚的时候,要清除此次操作中涉及到的缓存,
* 只对使用了CacheUtils的Pojo有效,并且Pojo中的cacheUtils需要设置keepCacheOfThisThread为true
*/
CacheUtils.clearCacheOfThisThread();
} catch (SQLException e1) {
throw new DBException(e);
}
}
if(e instanceof SQLException){
throw new DBException(e);
}else{
throw e;
}
} finally {
if(autoCommit!=null){
conn.setAutoCommit(autoCommit);
}
}
}
QueryHelper.transaction(new TransactionService() {
@Override
public void execute() throws Exception {
// 买家流水
long id1 = PayWater.ME.saveIt(order.getBuyer_id(), PayWater.TYPE_IN_RETURN, l_money, order.getSeller_id(), PayOrder.STATUS_CREATE, 0, order.getId(),
order.getOrder_type(), PayOrder.PAY_TYPE_BALANCE, order.getBuyer_id());
PayWater.ME.modRemark(id1, remark, 0, user.getId());
// 卖家流水
long id2 = PayWater.ME.saveIt(order.getSeller_id(), PayWater.TYPE_OUT_RETURN, l_money, order.getBuyer_id(), PayOrder.STATUS_CREATE, id1, order.getId(),
order.getOrder_type(), PayOrder.PAY_TYPE_BALANCE, order.getBuyer_id());
PayWater.ME.modRemark(id2, remark, 0, user.getId());
}
});


0
0

暂无评论