从java调用的2个存储过程的事务回滚?

时间:2016-01-15 13:57:10

标签: java oracle stored-procedures transactions rollback

我有这个实现org.hibernate.jdbc.Work的存储过程:

public class ContestRegWork implements Work {

private Long inIdContest;
private String userName;
private BigDecimal alteredRegNum;
private Return returnMessage;   


public ContestRegWork(Long inIdContest, String userName) {
    super();
    this.inIdContest = inIdContest;
    this.userName = userName;
}

public void execute(Connection connectionWrapped) throws SQLException {
//      Connection connection = ((WrappedConnection) connectionWrapped).getUnderlyingConnection();
    Connection connection;
    if (connectionWrapped instanceof WrappedConnection) {
        connection = ((WrappedConnection) connectionWrapped).getUnderlyingConnection();
    } else {
        connection = connectionWrapped.unwrap(oracle.jdbc.OracleConnection.class);
    }


    CallableStatement cs = null;
    try{
        cs = connection
                .prepareCall("{call CUSTADM.PKG_MCA.PRO_CONTEST_REG(?, ?, ?, ?) }");
        cs.setBigDecimal(1, BigDecimal.valueOf(inIdContest));
        cs.setString(2, userName);  
        cs.registerOutParameter(3, OracleTypes.NUMBER);
        cs.registerOutParameter(4, OracleTypes.STRUCT, Return.TYPE_NAME);

        cs.execute();

        setAlteredRegNum((BigDecimal)cs.getObject(3));
        setReturnMessage(new Return((Struct) cs.getObject(4)));

    //          connection.commit();
    } catch (Exception e) {
        throw new GenericAdjustmentServiceException(ExceptionCodes.REPOSITORY_EXCEPTION, "", e);
    } finally {
        try {
            cs.close(); 
        } catch (Exception e2) {}
    }

}

第二个程序:

public class ContestActOutWork implements Work {

private Long inIdContest;
private List<ActionData> actionDataList = null;
private Return returnMessage;

public ContestActOutWork(Long inIdContest){
    setInIdContest(inIdContest);
}

public void execute(Connection connection) throws SQLException {
    CallableStatement cs = null;
    try {
        cs = connection
                .prepareCall("{call CUSTADM.PKG_MCA.PRO_CONTEST_ACT_OUT(?, ?, ?) }");
        cs.setBigDecimal(1, BigDecimal.valueOf(inIdContest));

        cs.registerOutParameter(2, OracleTypes.ARRAY, ActionData.TABLE_OF_TYPE_NAME);
        cs.registerOutParameter(3, OracleTypes.STRUCT, Return.TYPE_NAME);

        cs.execute();

        actionDataList = new ArrayList<ActionData>();
        Array actionArray = (Array) cs.getObject(2);
        if (actionArray != null) {
            Object[] actionObject = (Object[]) actionArray.getArray();
            for (Object secao : actionObject) {
                actionDataList.add(new ActionData((Struct) secao));
            }
        }

        returnMessage = new Return((Struct) cs.getObject(3));
 //         connection.commit();
    } catch (Exception e) {
        throw new GenericAdjustmentServiceException(ExceptionCodes.REPOSITORY_EXCEPTION, "", e); 
    } finally {
        try {
            cs.close(); 
        } catch (Exception e2) {}
    }
 }

 public void setInIdContest(Long inIdContest) {
    this.inIdContest = inIdContest;
 }
 public List<ActionData> getActionDataList() {
    return actionDataList;
 }
 public Return getReturnMessage() {
    return returnMessage;
 }
}

这个存储库从hibernate sessionFactory获取会话并调用工作实现:

 @Transactional(propagation=Propagation.REQUIRED)
 @Repository
 public class AdjustmentRepositoryImpl extends BaseRepositoryImpl<Long> implements AdjustmentRepository{

@Override
public ContestRegTO proContestReg(Long idContest, String userName) {
    ContestRegWork crw = new ContestRegWork(idContest, userName);
    getSession().doWork(crw);
    return new ContestRegTO(crw.getAlteredRegNum(), crw.getReturnMessage());
  }

@Override
public ContestActionTO proContestActionOut(Long inIdContest) {
    ContestActOutWork ctaw = new ContestActOutWork(inIdContest);
    getSession().doWork(ctaw);
    return new ContestActionTO(ctaw.getActionDataList(), ctaw.getReturnMessage());
}

}

我的服务:

@Transactional
@Service( "AdjustmentService" )
public class AdjustmentServiceImpl implements AdjustmentService {

private AdjustmentRepository adjustmentRepository;
private SecurityService securityService;
private ParameterService parameterService;

@Autowired
public AdjustmentServiceImpl(AdjustmentRepository adjustmentRepository, SecurityService securityService, ParameterService parameterService) {
    this.adjustmentRepository = adjustmentRepository;
    this.securityService = securityService;
    this.parameterService = parameterService;
}

@Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
@Override
public ConcludeTO conclude(ContestInJsonTO contestInJsonTO) throws Exception{
    ConcludeTO concludeTO = new ConcludeTO();

        ContestRegTO contestRegTO = adjustmentRepository.proContestReg(contestInJsonTO.getIdContest(), contestInJsonTO.getUserName());
        returnMessage = contestRegTO.getReturnMessage();
        BigDecimal alteredRegNum = contestRegTO.getAlteredRegNum();
        if (!returnMessage.getReturnCode().equals(BigDecimal.ZERO)) {
            concludeTO.setReturnMessage(returnMessage);
            throw new Exception("");
        }

        returnMessage = adjustmentRepository.proContestAction(contestInJsonTO.getIdContest(), contestInJsonTO.getUserName());
        if (!returnMessage.getReturnCode().equals(BigDecimal.ZERO)) {
            concludeTO.setReturnMessage(returnMessage);
            throw new Exception("");
        }
        return concludeTO;
     }
}

我希望我的方法“结束”在异常的情况下回滚,但它不起作用。知道在这种情况下我应该做些什么吗?

谢谢

0 个答案:

没有答案