日期:2014-05-20  浏览次数:20738 次

关于java事务的问题!
try {
//得到结账流水号
String accountBalanceID = SequenceService.getNewSequenceID("account_charge_no", dataSource); //TODO:改为业务主键生成方式
ChargeBalance charge = new ChargeBalance();
charge.setAccountDate(acctDate);
charge.setAccountNo(accountBalanceID);
charge.setOperator(operatorName);
charge.setMaxRcptNo(maxRcptNo);
charge.setMinRcptNo(minRcptNo);
charge.setFreeNum(0);
charge.setChargeNum(Integer.parseInt(chargeNum));
charge.setReturnNum(Integer.parseInt(returnNum));
charge.setChargeFee(Double.parseDouble(chargeFee));
charge.setChargeCost(Double.parseDouble(chargeCost));
charge.setReturnFee(Double.parseDouble(returnFee));
charge.setXNHCharge(Double.parseDouble(XNHCharge));
//收费结账结算
AccountBalanceOfChargeService service = new AccountBalanceOfChargeService(dataSource);
service.doChargeOfBalanceByTime(hospitalID, charge, startTime, endTime);
context.addDataField(Constants.JSON_RESULT_NAME, accountBalanceID);
} catch (Exception e) {
//设置为回滚状态
this.getTransaction().setRollbackOnly(true); log.error("DoAccountOfChargeAction is Error!",e);
}


请问如果 service里面的sql没有插入成功这种事务处理可以吗?

------解决方案--------------------
要不你做添加的时候做下判断,添加成功TRUE,否则FALSE
然后通过这个值来做事务的回滚操作
------解决方案--------------------
我觉得你这个只是一个异常捕捉..如果插入不成功而没有抛异常的话..肯定不能回滚.
------解决方案--------------------
这种写法太陌生了,好久没用过了,所以不敢说对错。现在我都用spring框架里面的事务处理事务
Java code

TransactionManager trans = new TransactionManager();// 框架自动回滚
try {
    trans.start();
    AccountBalanceOfChargeService service = new AccountBalanceOfChargeService(dataSource);
    service.doChargeOfBalanceByTime(hospitalID, charge, startTime, endTime);
    context.addDataField(Constants.JSON_RESULT_NAME, accountBalanceID);    
    trans.commit();

    } finally {
        trans.end();
    }