2016-04-01 2 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") 
    } 
} 

Hinweis: Ich kann nicht dieses Problem reproduzieren lokal durch verschiedene scenario.but wenn Ich schaue in die Protokolldatei des Production-Servers oben Problem wiederholt passiert.shall ich mache es, dass der Code als synchronisiert oder sonst Service Ebene für transaction management.line Instanz? .save (flush: true) verursachen das Problem genau..Bitte Geben Sie Ihre Idee weiterorg.hibernate.StaleObjectStateException: Reihe wurde durch eine andere Transaktion aktualisiert oder gelöscht (oder nicht gespeicherte Wertzuordnung falsch war) [com # 117591]

Antwort

1

Sie sollten Ihren Datenbankzugriff definitiv in einen Transaktionsservice verschieben. Das wird dieses Problem allerdings nicht unbedingt verhindern. Außerdem ist die Synchronisation definitiv NICHT der richtige Weg.

Dies ist ein optimistischer Sperrfehler. (Siehe https://grails.github.io/grails-doc/latest/guide/GORM.html#locking)

Mit den von Ihnen angegebenen Informationen kann ich keine definitive Antwort geben, aber basierend auf dem, was ich vorher gesehen habe, vermute ich, dass Sie ein Doppelklick-Problem haben: Der Benutzer löst das aus Aktion ein zweites Mal, bevor die erste Aktion abgeschlossen ist. Die erste Aktion vervollständigt und aktualisiert die Version des betroffenen Objekts. Wenn die zweite Aktion in die Datenbank schreibt, schlägt sie fehl, weil die Version des Objekts seit dem Laden des Objekts aktualisiert wurde.

Um zu überprüfen, ob das das Problem ist, sollten Sie Protokollierung zu Ihrem Produktionssystem hinzufügen, so dass Sie sehen können, wann diese Methode startet und was die ID ist. Wenn Sie zwei (oder mehr) Protokolleinträge für dasselbe Objekt mit fast identischen Zeitstempeln sehen, ist das Ihr Problem.

Um es zu lösen, ändern Sie entweder Ihre Webseite, um Doppelklicks zu blockieren, oder ändern Sie Ihren Code, um die StaleObjectStateException zu akzeptieren.

+0

Vielen Dank für Ihre Antwort @ Foundart..Ich habe in Bezug auf Doppelklick issue.but aber es gibt nichts in der Art .. Schon gibt es keine Möglichkeit für Doppelklick ... so geplant, Kriegsdatei mit Logging-System zu implementieren, so dass Ich kann das Problem genau herausfinden. –

Verwandte Themen