How to handle reversal with JPA?

136 Views Asked by At

I am using Spring and JPA for a financial data handling project. I have to handle scheduling transactions for future dates. In a daily run quartz cron job and execute all schedule transaction and persist to real table.

My problem is, when going to execute trigger for a given time, one record failed due to some reason, then all other records were not executed.

I need to execute all other transactions and failed transactions should rollback.

Is there a way to handle these things?

following code block get all the schedule job

 public void bankingPaymentSchedullerRun(){

// get all pending job
List<BankScheduler> bankSchedulers = bankSchedulerDao.findByStatus(ScheduleStatus.PENDING);
Calendar currentDate = DateUtil.getFormatedCalenderDate(DateUtil.currentDate(), "yyyy-MM-dd");
if (bankSchedulers != null) {
  for (BankScheduler bankScheduler : bankSchedulers) {
    LOGGER.info("bankingPaymentSchedullerRun " + bankScheduler.toString());
    //compare from date and to date
    if ((bankScheduler.getFromDate().compareTo(currentDate) <= 0)
        && (bankScheduler.getToDate().compareTo(currentDate)) >= 0) {

             if (bankScheduler.getSchedulerType().equals(SchedulerType.FUND_TRANSFER.toString())) {
                scheduleFundTransfer(bankScheduler);
             }else {
                 scheduleUtilityPayment(bankScheduler);
             }

        }
    }
}

}

Fund transfer execution code block

public PaymentResponse scheduleFundTransfer(final BankScheduler bankScheduler){

    FundTransfer fundTransfer = new FundTransfer();

    fundTransfer.setBranchName(bankScheduler.getBankSchedulerFundTransfer().getBeneficiaryBranchName());
    fundTransfer.setBeneficiaryBankName(bankScheduler.getBankSchedulerFundTransfer().getBeneficiaryBankName());
    fundTransfer.setBeneficiaryType(bankScheduler.getBankSchedulerFundTransfer().getBeneficiaryType());
    fundTransfer.setBeneficiaryName(bankScheduler.getBankSchedulerFundTransfer().getBeneficiaryName());
    fundTransfer.setCurrency(bankScheduler.getBankSchedulerFundTransfer().getCurrency());
    fundTransfer.setNarration(bankScheduler.getDetail());
    fundTransfer.setThirdPartyType(bankScheduler.getBankSchedulerFundTransfer().getThirdPartyType());
    fundTransfer.setTransferedAmount(bankScheduler.getBankSchedulerFundTransfer().getAmount());
    fundTransfer.setUserAccountNumber(bankScheduler.getBankSchedulerFundTransfer().getAccountNumber());
    fundTransfer.setUserName(bankScheduler.getBankSchedulerFundTransfer().getUserName());

    FundTransfer fundTransferUpdate = null;
    //commit to fundtransfer real table
    try {
        fundTransferUpdate = fundTransferDao.create(fundTransfer);
    } catch (Exception e) {
        LOGGER.error("Exception occur when call update fund transfer in fundTransfer()", e);
    }

     // getting from currecy Decimals
    BankCurrecyInfor currecyInfo =
    bankCurrecyInforDao.getDecimalpointsByCurrecy(fundTransfer.getUserAccount().getCurrencyCode());
    fundTransferUpdate.setDecimalAmt(String.valueOf(currecyInfo.getNoOfDecimal()));
    //call to bank back-end to update 
     PaymentResponse response = accountServiceInvoker.fundTransfer(fundTransferUpdate);

     //for sending sms
    SmsCriteria smsCriteria = new SmsCriteria(); 
    smsCriteria.setBank_name("ABC");
    messageServiceInvoker.sentIbSms(smsCriteria);

    return response;
 }
1

There are 1 best solutions below

0
On

Yes there is, use proper exception handling so when single transaction fails, other will still (try to) execute.

Pseudocode

for(scheduled task from all scheduled tasks) {
   try{
      begin transaction
      do your stuff with jpa 
      commit transaction
   }catch(Exception e){
      rollback transaction, log error and stuff
    }finally{
      release resources
   }

}