2014-12-11 41 views
0

我想在功能上回滚事务说某某()回滚函数中的交易调用另一个函数

该XYZ调用另一个函数说ABC()内,它有它自己的事务TX。如果由于异常Abc()回退它的事务,因此xyz()中的事务也应该回滚。 如何在xyz()中回滚事务?

这是我的代码。

public String execute() throws Exception { 
     // viewReadOnly(); 
     Session sf=null; 
     Transaction tx=null; 

     try { 
       sf = HibernateUtil.getSessionFactory().getCurrentSession(); 
       tx = sf.getTransaction(); 

      tx.begin(); 

      //sosFile.setCmaId(1); 
      //sosFile.setSosFileId(1); 
      //sosFile.setHeadImage(new ImageService().readImageOldWay(headFile)); 
      //sosFile.setFootImage(new ImageService().readImageOldWay(footFile)); 

      //if(new ImageService().readImageOldWay(headFile) != null) 

      System.out.println("FLAG="+flag); 

      if(headFile!=null) 
     { 

      for (int i = 0; i < headFile.length; i++) { 

      if (headFile != null) { 
       sosOrder = new SOSCustomerOrderFile(); 
       sosOrder.setCmaId(cmaId); 



       sosOrder.setFileData(new ImageService().readImageOldWay(headFile[i])); 
       sosOrder.setFileName(new File(sosorderfilename[i]).getName()); 
       sosOrder.setSosStage(flag); 

       sf.saveOrUpdate(sosOrder); 
       } 

      } 
     } 
      if(footFile!=null) 
      { 

      for (int i = 0; i < footFile.length; i++) { 

      if (footFile != null) { 
       sosCheque.setCmaId(cmaId); 
       sosCheque.setFileData(new ImageService().readImageOldWay(footFile[i])); 
       sosCheque.setFileName(new File(soschequefilename[i]).getName()); 
       sosCheque.setSosStage(flag); 

       sf.saveOrUpdate(sosCheque); 

      } 
      } 
     } 

     // tx.begin(); 


     // sf.close(); 
/*Session sf1 = HibernateUtil.getSessionFactory().getCurrentSession(); 
      Transaction tx1 = sf1.getTransaction(); 
      tx1.begin();*/ 

      if (cheque_no_hidden != null || cheque_amount_hidden != null || cheque_bank_hidden != null || cheque_date_hidden != null) { 
       for (int i = 0; i < chequeCounter; i++) { 



        cheque_no = cheque_no_hidden.split(","); 
        cheque_amount = cheque_amount_hidden.split(","); 
        cheque_bank = cheque_bank_hidden.split(","); 
        cheque_date = cheque_date_hidden.split(","); 

        SOSChequeDetails sosChequeD = new SOSChequeDetails(); 
        sosChequeD.setChequeNo(cheque_no[i]); 
        sosChequeD.setChequeAmount(cheque_amount[i]); 
        sosChequeD.setChequeBank(cheque_bank[i]); 
        sosChequeD.setChequeDate(cheque_date[i]); 
        sosChequeD.setCmaId(cmaId); 
        sosChequeD.setSosStage(flag); 

        sf.saveOrUpdate(sosChequeD); 
       //  tx1.begin(); 


       // sf1.close(); 

       } 

      } 



if(saveSOSValue.saveSOS(sosValues, cmaId,flag,sf,tx)) 
{ 
    sosValues.setFlag(1); 
} 
else 
{ 
    sosValues.setFlag(2); 
} 

if(flag.equalsIgnoreCase("sosSUBMIT")) 
{ 
CmaDetails cmaD = (CmaDetails) sf.get(CmaDetails.class, cmaId); 

/* for(int j=0;j<5;j++){ 
    LeaveManagementManager manager=new LeaveManagementManager(); 
    Userinfo userinfoLeave=manager.getUserInfoObjforsos(sosWorkflow_status); 

    workflow.setFromDesignation(userinfoLeave.getWorkflowdesignation().getWorkflowDesignationId()); 
    List<Workflow> workflowListTemp=new ArrayList<Workflow>(); 
    workflowListTemp=(new WorkflowService().performAction(workflow)); 

    if(userinfoLeave.getOutOfOffice()!=null && userinfoLeave.getOutOfOffice().equals("OOO")){ 
     date.setSeconds(date.getSeconds()+1); 
     wfs=makeEntryInWorkflow(sdm,formater,now, date, workflowListTemp, cmaDetails, query, i, ToEmailIdTemp, ToEmailId,wfs,null); 
    }else{ 
     j=5; 
    } 
    }*/ 

boolean decideRollback=approveSOSvalue.ApprovalSOSWorkflow(sosWorkflow_status,sosValues,cmaD,"create",1,"flag"); 
if(decideRollback==false){ 
tx.rollback(); 

} 
} 
     } catch (Exception e) { 
      if (tx!=null) tx.rollback(); 

      e.printStackTrace(); 
     } 




     viewDocuments(); 
     viewCheques(); 
     /* ComplianceCMA complianceCMA = new ComplianceCMA(); 
     cmaDetails = complianceCMA.getCma(cmaId); 
     cmaDetails.setStage(Constants.STAGE_SOS_UPLOAD); 
     UpdateCMA updateStage = new UpdateCMA(); 
     updateStage.performAction(cmaDetails);*/ 


     ViewSOSDetails viewsos=new ViewSOSDetails(); 
     viewsos.home(); 
     return SUCCESS; 
    } 

这个函数调用

public boolean ApprovalSOSWorkflow(SOSWorkflow_status sosWorkflowstatuscomment,SOSValues sosValues,CmaDetails cmaId,String Action,int bpaID,String flag) 

功能。 请帮忙。

回答

0

解决此问题的最佳方法是不使用abc()中的新事务。以xyz开始交易,并使用abc()中所需的交易协议。这样,abc()中的Tx将在xyz()开始的Tx上重叠,如果abc引发异常,则捕获它并回滚Tx,以使Xyz中的Tx也回滚。你在使用Spring吗?如果不是的话,那么Spring应该使用注解非常容易地解决这种情况。

+0

请将'edits'添加到上面的原始问题中。评论并不意味着代码包。 – Nazgul 2014-12-11 06:08:06

+0

我已添加我的代码。 我得到另一个异常说我的交易没有成功开始。 – iamP 2014-12-11 06:34:13

0

你可以有事务Abc()返回一个布尔值,说明它是否必须自己回滚,然后在xyz()中使用该布尔值来决定是否回滚它。