标签云

微信群

扫码加入我们

WeChat QR Code

My requirement is, if any exception comes while saving entries in database, it should not stop there rather it should continue in loop. But currently, the behaviour is very unusual. If any exception is thrown, even then it create/update entry in database.

In my code i have added the line if(i==1) throw new Exception("Exception Occured"). I want if any exception occurs, it should go to catch block and apppends to my error string and after that it doesnt terminate and transaction loop goes till the end. It should not terminate and all transactions should not roll back except the exception one transaction (i==1).

Currently All investmentPlans are getting created/updated even if exception is thrown.

Please suggest.

     @Entity
        @Table( name = "user_goal_investment_plans")
        @Where(clause = "deleted='false'")
        public class UserGoalInvestmentPlanDO {
            @Id
            @GeneratedValue
            @Column(name="id")
            private Long id;

            @ManyToOne(fetch=FetchType.LAZY , targetEntity = UserGoalDO.class)
            @JoinColumn(name = "user_goal_id" , nullable=false)
            @Where(clause = "deleted='false'")
            private UserGoalDO userGoal;

            @Column(name="investment_value")
            private Long investmentValue;

            @Enumerated(EnumType.STRING)
            @Column(name="sip_frequency")
            private UserSIPFrequencyEnum sipFrequency;

            @Column(name="num_of_installments")
            private Integer numOfInstallments;

            @Temporal(TemporalType.TIMESTAMP)
            @Column(name="next_sip_date", length = 19)
            private Date nextSipDate;

            @Column(name = "status")
            @Enumerated(EnumType.STRING)
            private InvestmentPlanStatusEnum status;

            @Column(name = "transaction_status")
            @Enumerated(EnumType.STRING)
            private TransactionalStatusEnum transactionalStatus;

            @ManyToOne(fetch=FetchType.LAZY, targetEntity = UserNachDO.class)
            @JoinColumn(name = "user_nach_id")
            @Where(clause = "deleted='false'")
            private UserNachDO userNach;

            @ManyToOne(fetch=FetchType.LAZY , targetEntity = SchemeCodeDO.class)
            @JoinColumn(name = "scheme_code_id" , nullable=false)
            @Where(clause = "deleted='false'")
            private SchemeCodeDO schemeCode;

            @Column(name="transaction_type")
            @Enumerated(EnumType.STRING)
            private TransactionTypeEnum transactionType;

            @Column(name = "sub_transaction_type")
            @Enumerated(EnumType.STRING)
            private SubTransactionTypeEnum subTransactionType;

            @Column(name = "next_installment_number")
            private Integer nextInstallmentNumber;

            @Temporal(TemporalType.TIMESTAMP)
            @Column(name = "sip_registration_date", length = 19)
            private Date sipRegistrationDate;

            @Column(name = "sip_registration_number")
            private Integer sipRegistrationNumber;

            @Column(name = "euin")
            private String euin;

            @Column(name = "is_pause")
            private Boolean pause;

            @Column(name = "mode")
            @Enumerated(EnumType.STRING)
            private ModeEnum mode; 

            @Column(name = "relation_serial_id")
            private Long relationSerialId;

            @Column(name="deleted")
            private Boolean deleted;

            @Temporal(TemporalType.TIMESTAMP)
            @Column(name="created_at", length = 19)
            private Date createdAt;

            @Temporal(TemporalType.TIMESTAMP)
            @Column(name = "updated_at", length = 19)
            private Date updatedAt;

            @Column(name="ip_address")
            private String ipAddress;

            @Column(name = "is_full_redemption")
            private Boolean isFullRedemption;

            @Column(name = "user_amc_folio_number")
            private String userAmcFolioNumber;

            @ManyToOne(fetch=FetchType.LAZY, targetEntity=UserAmcFolioDO.class)
            @JoinColumn(name = "user_amc_folio_id", nullable = false)
            @Where(clause = "deleted='false'")
            private UserAmcFolioDO userAmcFolio;

            @OneToMany(fetch = FetchType.LAZY, mappedBy = "userGoalInvestmentPlan", cascade = CascadeType.ALL)
            @Where(clause = "deleted='false'")
            private List<UserGoalAmcTransactionDO> userGoalTransactionAmcs;

            @OneToMany(fetch = FetchType.LAZY, mappedBy = "userGoalInvestmentPlan", cascade = CascadeType.ALL)
            @Where(clause = "deleted='false'")
            private List<UserGoalTransactionDO> userGoalTransactions;

            /**
             * @return the id
             */
            public Long getId() {
                return id;
            }

            /**
             * @param id the id to set
             */
            public void setId(Long id) {
                this.id = id;
            }

            /**
             * @return the userGoal
             */
            public UserGoalDO getUserGoal() {
                return userGoal;
            }

            /**
             * @param userGoal the userGoal to set
             */
            public void setUserGoal(UserGoalDO userGoal) {
                this.userGoal = userGoal;
            }

            /**
             * @return the investmentValue
             */
            public Long getInvestmentValue() {
                return investmentValue;
            }

            /**
             * @param investmentValue the investmentValue to set
             */
            public void setInvestmentValue(Long investmentValue) {
                this.investmentValue = investmentValue;
            }

            /**
             * @return the sipFrequency
             */
            public UserSIPFrequencyEnum getSipFrequency() {
                return sipFrequency;
            }

            /**
             * @param sipFrequency the sipFrequency to set
             */
            public void setSipFrequency(UserSIPFrequencyEnum sipFrequency) {
                this.sipFrequency = sipFrequency;
            }

            /**
             * @return the numOfInstallments
             */
            public Integer getNumOfInstallments() {
                return numOfInstallments;
            }

            /**
             * @param numOfInstallments the numOfInstallments to set
             */
            public void setNumOfInstallments(Integer numOfInstallments) {
                this.numOfInstallments = numOfInstallments;
            }

            /**
             * @return the nextSipDate
             */
            public Date getNextSipDate() {
                return nextSipDate;
            }

            /**
             * @param nextSipDate the nextSipDate to set
             */
            public void setNextSipDate(Date nextSipDate) {
                this.nextSipDate = nextSipDate;
            }

            /**
             * @return the status
             */
            public InvestmentPlanStatusEnum getStatus() {
                return status;
            }

            /**
             * @param status the status to set
             */
            public void setStatus(InvestmentPlanStatusEnum status) {
                this.status = status;
            }

            /**
             * @return the schemeCode
             */
            public SchemeCodeDO getSchemeCode() {
                return schemeCode;
            }

            /**
             * @param schemeCode the schemeCode to set
             */
            public void setSchemeCode(SchemeCodeDO schemeCode) {
                this.schemeCode = schemeCode;
            }

            /**
             * @return the transactionType
             */
            public TransactionTypeEnum getTransactionType() {
                return transactionType;
            }

            /**
             * @param transactionType the transactionType to set
             */
            public void setTransactionType(TransactionTypeEnum transactionType) {
                this.transactionType = transactionType;
            }

            /**
             * @return the subTransactionType
             */
            public SubTransactionTypeEnum getSubTransactionType() {
                return subTransactionType;
            }

            /**
             * @param subTransactionType the subTransactionType to set
             */
            public void setSubTransactionType(SubTransactionTypeEnum subTransactionType) {
                this.subTransactionType = subTransactionType;
            }

            /**
             * @return the nextInstallmentNumber
             */
            public Integer getNextInstallmentNumber() {
                return nextInstallmentNumber;
            }

            /**
             * @param nextInstallmentNumber the nextInstallmentNumber to set
             */
            public void setNextInstallmentNumber(Integer nextInstallmentNumber) {
                this.nextInstallmentNumber = nextInstallmentNumber;
            }

            /**
             * @return the sipRegistrationNumber
             */
            public Integer getSipRegistrationNumber() {
                return sipRegistrationNumber;
            }

            /**
             * @param sipRegistrationNumber the sipRegistrationNumber to set
             */
            public void setSipRegistrationNumber(Integer sipRegistrationNumber) {
                this.sipRegistrationNumber = sipRegistrationNumber;
            }

            /**
             * @return the euin
             */
            public String getEuin() {
                return euin;
            }

            /**
             * @param euin the euin to set
             */
            public void setEuin(String euin) {
                this.euin = euin;
            }

            /**
             * @return the pause
             */
            public Boolean getPause() {
                return pause;
            }

            /**
             * @param pause the pause to set
             */
            public void setPause(Boolean pause) {
                this.pause = pause;
            }

            /**
             * @return the deleted
             */
            public Boolean getDeleted() {
                return deleted;
            }

            /**
             * @param deleted the deleted to set
             */
            public void setDeleted(Boolean deleted) {
                this.deleted = deleted;
            }

            /**
             * @return the createdAt
             */
            public Date getCreatedAt() {
                return createdAt;
            }

            /**
             * @param createdAt the createdAt to set
             */
            public void setCreatedAt(Date createdAt) {
                this.createdAt = createdAt;
            }

            /**
             * @return the updatedAt
             */
            public Date getUpdatedAt() {
                return updatedAt;
            }

            /**
             * @param updatedAt the updatedAt to set
             */
            public void setUpdatedAt(Date updatedAt) {
                this.updatedAt = updatedAt;
            }

            /**
             * @return the ipAddress
             */
            public String getIpAddress() {
                return ipAddress;
            }

            /**
             * @param ipAddress the ipAddress to set
             */
            public void setIpAddress(String ipAddress) {
                this.ipAddress = ipAddress;
            }

            /**
             * @return the sipRegistrationDate
             */
            public Date getSipRegistrationDate() {
                return sipRegistrationDate;
            }

            /**
             * @param sipRegistrationDate the sipRegistrationDate to set
             */
            public void setSipRegistrationDate(Date sipRegistrationDate) {
                this.sipRegistrationDate = sipRegistrationDate;
            }

            /**
             * @return the userGoalTransactionAmcs
             */
            public List<UserGoalAmcTransactionDO> getUserGoalTransactionAmcs() {
                return userGoalTransactionAmcs;
            }

            /**
             * @param userGoalTransactionAmcs the userGoalTransactionAmcs to set
             */
            public void setUserGoalTransactionAmcs(List<UserGoalAmcTransactionDO> userGoalTransactionAmcs) {
                this.userGoalTransactionAmcs = userGoalTransactionAmcs;
            }

            /**
             * @return the userGoalTransactions
             */
            public List<UserGoalTransactionDO> getUserGoalTransactions() {
                return userGoalTransactions;
            }

            /**
             * @param userGoalTransactions the userGoalTransactions to set
             */
            public void setUserGoalTransactions(List<UserGoalTransactionDO> userGoalTransactions) {
                this.userGoalTransactions = userGoalTransactions;
            }

            /**
             * @return the mode
             */
            public ModeEnum getMode() {
                return mode;
            }

            /**
             * @param mode the mode to set
             */
            public void setMode(ModeEnum mode) {
                this.mode = mode;
            }

            /**
             * @return the relationSerialId
             */
            public Long getRelationSerialId() {
                return relationSerialId;
            }

            /**
             * @param relationSerialId the relationSerialId to set
             */
            public void setRelationSerialId(Long relationSerialId) {
                this.relationSerialId = relationSerialId;
            }

            /**
             * @return the transactionalStatus
             */
            public TransactionalStatusEnum getTransactionalStatus() {
                return transactionalStatus;
            }

            /**
             * @param transactionalStatus the transactionalStatus to set
             */
            public void setTransactionalStatus(TransactionalStatusEnum transactionalStatus) {
                this.transactionalStatus = transactionalStatus;
            }

            public UserNachDO getUserNach() {
                return userNach;
            }

            public void setUserNach(UserNachDO userNach) {
                this.userNach = userNach;
            }

            public Boolean getFullRedemption() {
                return isFullRedemption;
            }

            public void setFullRedemption(Boolean fullRedemption) {
                isFullRedemption = fullRedemption;
            }

            public String getUserAmcFolioNumber() {
                return userAmcFolioNumber;
            }

            public void setUserAmcFolioNumber(String userAmcFolioNumber) {
                this.userAmcFolioNumber = userAmcFolioNumber;
            }

            public UserAmcFolioDO getUserAmcFolio() {
                return userAmcFolio;
            }

            public void setUserAmcFolio(UserAmcFolioDO userAmcFolio) {
                this.userAmcFolio = userAmcFolio;
            }
        }
   **SERVICE LAYER HAVING @TRANSACTIONAL**     
         @Transactional
        @Override
          public Boolean saveOrUpdateUserGoalInvestmentPlans(List<UserGoalInvestmentPlanBO> userGoalInvestmentPlans) {
    if (userGoalInvestmentPlanDao.saveOrUpdateUserGoalInvestmentPlansForNachScheduler(userGoalInvestmentPlansForNach)) {
                return true;
            } else {
                return false;
            }
    }

**DAO Layer**
       @Override
            public Boolean saveOrUpdateUserGoalInvestmentPlansForNachScheduler(List<UserGoalInvestmentPlanDO> userGoalInvestmentPlans) {
                log.info("Saving the user goal investment plans.. User goal investment value is :: {}");
                try {
                    long userGoalInvestmentPlanId = 0;
                    if (userGoalInvestmentPlans != null && !userGoalInvestmentPlans.isEmpty()) {
                        log.info("User goal investment plans exists..", userGoalInvestmentPlans.toString());
                        final StringBuilder errorString = new StringBuilder();
                        for (int i = 0; i < userGoalInvestmentPlans.size(); i++) {
                            try {
                                if (i == 1)
                                    throw new Exception("Exception Occured");

                                if (userGoalInvestmentPlans.get(i).getId() != null) {
                                    if (userGoalInvestmentPlans.get(i).getId() != null)
                                        hibernateTemplate.update(userGoalInvestmentPlans.get(i));
                                        // Need to check whether to create a new entry of ugip in case it is not there
                                    else
                                        hibernateTemplate.save(userGoalInvestmentPlans.get(i));

                                    userGoalInvestmentPlanId = userGoalInvestmentPlans.get(i).getId();
                                } else {
                                    userGoalInvestmentPlanId = (long) hibernateTemplate.save(userGoalInvestmentPlans.get(i));
                                }
                            } catch (Throwable ex) {
                                log.error("Exception occured due to {} " + ExceptionUtils.getRootCauseMessage(ex));
                                errorString.append(ExceptionUtils.getRootCauseMessage(ex) + "  userGoalInvestmentId => " + String.valueOf(userGoalInvestmentPlans.get(i).getId()) + ",  ");
                            }
                        }
                        if (errorString.length() > 0) {
                            System.out.println(errorString);
                            saveExceptionsInUserEmail(errorString.toString());
                            return false;
                        }

                        if (userGoalInvestmentPlanId != 0) {
                            return true;
                        }

                    } else {
                        log.error("User goal investment plan is null");
                    }
                } catch (DataAccessException e) {
                    log.error("DataAccessException Occured :- " + e.getMessage());
                    throw new BWCommonException("Error occured while saving user goals. Please try again later.");
                }
                return false;
            }