2016-04-01 10 views
0
def saveProcessDetail = { 

    //def employeeLoanInstance = EmployeeLoan?.findById(params?.long("id"),[lock: true]) 
    //lock(params?.long("id")) 
    def employeeLoanInstance = EmployeeLoan?.get(params?.long("id")) 
    if(employeeLoanInstance){ 
     def instance 
     if(params?.modeOfPayment){ 
      if(params.modeOfPayment == 'DD'){ 
       params.ddDate = params.ddDate ? DateUtility?.parseDate(params.ddDate) : params.ddDate 
       instance = new Dd() 
      } 
      if(params.modeOfPayment == 'Cheque'){ 
       params.chequeDate = params.chequeDate ? DateUtility?.parseDate(params.chequeDate) : params.chequeDate 
       instance = new Cheque() 
      } 
      if(params.modeOfPayment == 'Cash'){ 
       params.paidDate = params.paidDate ? DateUtility?.parseDate(params.paidDate) : params.paidDate 
       instance = new Cash() 
      } 
      instance?.properties = params 
     } 

     params.deductionDate = params.deductionDate ? DateUtility?.parseLoanDate(params.deductionDate) : params.deductionDate 
     params.sanctionedDate = params.sanctionedDate ? DateUtility?.parseDate(params.sanctionedDate) : params.sanctionedDate 

     //println " test : "+DateUtility.getFormattedMonthAndYear(params.deductionDate) 
     //println " test zx : "+DateUtility.getFormattedMonthAndYear(params.sanctionedDate) 
     def formattedDeductionDate = DateUtility.getFormattedMonthAndYear(params.deductionDate) 
     def formattedSanctionedDate = DateUtility.getFormattedMonthAndYear(params.sanctionedDate) 


     //println " less : "+(formattedDeductionDate < formattedSanctionedDate) 

     //println " = : "+(formattedDeductionDate == formattedSanctionedDate) 


     //println " greater : "+(formattedDeductionDate > formattedSanctionedDate) 

    // formattedDeductionDate.before(formattedSanctionedDate) && !(formattedDeductionDate.equals(formattedSanctionedDate)) 


     employeeLoanInstance?.properties = params 
     instance?.validate() 
     if(params.modeOfPayment == 'Cash' && employeeLoanInstance?.sanctionedAmount && instance?.cashAmount && (instance?.cashAmount != employeeLoanInstance?.sanctionedAmount)) 
      instance.errors.rejectValue ("cashAmount", "cash.cashAmount.invalid.message", [ 
       message(code : 'cash.cashAmount.label', default : 'Amount')] 
      as Object[], message(code : 'cash.cashAmount.invalid.message')) 
     if(!instance?.hasErrors()) 
      employeeLoanInstance.loanId = employeeLoanInstance.loanId ? employeeLoanInstance.loanId : loanService?.getLoanId(employeeLoanInstance?.employee,employeeLoanInstance?.createdBranch) 
     employeeLoanInstance?.validate() 
     /* Emi Amount Validation */ 
     if(employeeLoanInstance?.noOfInstallments && employeeLoanInstance?.emiAmount && employeeLoanInstance?.sanctionedAmount){ 
      def amt = (employeeLoanInstance?.noOfInstallments - 1) * (employeeLoanInstance?.emiAmount) 
      def sanctionedAmt = (employeeLoanInstance?.sanctionedAmount - amt) 
      if(amt > employeeLoanInstance?.sanctionedAmount) 
       employeeLoanInstance.errors.rejectValue ("emiAmount", "employeeLoan.emiAmount.invalid.message", [ 
        message(code : 'employeeLoan.emiAmount.label', default : 'Emi Amount')] 
       as Object[], message(code : 'employeeLoan.emiAmount.invalid.message')) 
      if(sanctionedAmt > employeeLoanInstance?.emiAmount) 
       employeeLoanInstance.errors.rejectValue ("emiAmount", "employeeLoan.emiAmount.invalid.message", [ 
        message(code : 'employeeLoan.emiAmount.label', default : 'Emi Amount')] 
       as Object[], message(code : 'employeeLoan.emiAmount.invalid.message')) 
     } 
     /* End */ 
     if(!(employeeLoanInstance?.hasErrors()) && !(instance?.hasErrors())){ 
      instance?.save(flush : true) 
      employeeLoanInstance.paymentId = instance?.id 
      employeeLoanInstance.outstandingAmount = employeeLoanInstance?.sanctionedAmount 
      employeeLoanInstance?.save(flush : true) 
      println"employeeLoanInstance?.noOfInstallments"+employeeLoanInstance?.noOfInstallments 
      println "-------loan instance----------->"+employeeLoanInstance?.errors 
      /* For saving EmiAmount Details */ 
      def emiMonth = params.deductionDate 
      def amt = 0 
      if(employeeLoanInstance?.noOfInstallments){ 
      for(int i = 0; i < Integer?.valueOf(employeeLoanInstance?.noOfInstallments); i++){ 
       amt = employeeLoanInstance?.emiAmount 
       if(i > 0){ 
        Calendar calendar = GregorianCalendar.getInstance() 
        Integer year = emiMonth?.year+1900 
        Integer month = (emiMonth?.month) + 1 
        Integer date = emiMonth.getAt(Calendar.DAY_OF_MONTH) 
        //calendar.set (year, month, date) 
        calendar.set(year, month, date, 0, 0, 0) 
        emiMonth = calendar?.getTime() 
       } 
       if(i == (employeeLoanInstance?.noOfInstallments - 1)){ 
        amt = employeeLoanInstance?.sanctionedAmount - (employeeLoanInstance?.emiAmount * (employeeLoanInstance?.noOfInstallments - 1)) 
       } 
       def emiAmountInstance = new EMIAmountDetails() 
       emiAmountInstance?.emiMonth = emiMonth 
       emiAmountInstance?.emiAmount = amt 
       emiAmountInstance?.loanId = employeeLoanInstance.loanId 
       auditService.beforeSave(emiAmountInstance) 
       emiAmountInstance?.save(flush : true) 
      } 
      } 
      /* End */ 

      flash.message = "${message(code : 'loanApproval.created.message', args : [employeeLoanInstance?.loanType,loanService?.getName(employeeLoanInstance.employee.empPersonalDetails),message(code : 'employeeLoan.loanStatus.'+employeeLoanInstance?.loanStatus)])}" 
      redirect(action : "list") 
     } 
     else{ 
      render(template : "processLoan", model : [screenName : params?.screenName,instance:instance,employeeLoanInstance : employeeLoanInstance, methodName : params?.methodName]) 
     } 
    } 
    else{ 
     flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'employeeLoan.label', default: 'EmployeeLoan'), params.id])}" 
     redirect(action : "list") 
    } 
} 

注:私は、様々なscenario.butときにより局所的にこの問題を再現することはできません私は上記のプロダクションサーバーのログファイルを見て、上記の問題が繰り返し発生しました。私は、トランザクション管理のためのサービスレイヤーに同期されたコードブロックを作成します。ラインインスタンス?.save(flush:true)org.hibernate.StaleObjectStateException:行を更新または削除によって、別のトランザクション(または未保存と値のマッピングが間違っていました)して、[COM#117591]

答えて

1

あなたは確実にデータベースアクセスをトランザクションサービスに移行する必要があります。しかし、必ずしもこの問題を防ぐわけではありません。また、同期は間違いなくここに行く方法ではありません。

これは楽観的なロック失敗です。 (https://grails.github.io/grails-doc/latest/guide/GORM.html#lockingを参照してください)

あなたが与えた情報では、私は決定的な答えを出すことはできませんが、前に見たことに基づいて、あなたはダブルクリック問題があると思われます。最初のアクションが完了する前にもう一度アクションを実行します。最初のアクションが完了し、影響を受けたオブジェクトのバージョンが更新されます。 2番目のアクションがデータベースに書き込むと、オブジェクトがロードされてからオブジェクトのバージョンが更新されたため、失敗します。

これが問題であるかどうかを確認するには、プロダクションシステムにログを追加して、このメソッドの開始時刻とそのIDを確認する必要があります。ほぼ同じタイムスタンプの同じオブジェクトに対して2つ(またはそれ以上)のログエントリがある場合、それが問題です。

解決するには、ダブルクリックをブロックするようにWebページを変更するか、StaleObjectStateExceptionを受け入れるようにコードを変更します。

+0

あなたの返信ありがとうございます。@ foundart..私はダブルクリックissue.butについて確認していることは何もありません..すでにダブルクリックのための方法はありません...だから私はログシステムを使用してwarファイルを展開することを計画した私は問題を正確に見つけることができます.. –

関連する問題