在 Spring Boot 应用程序中调用存储库方法时出现错误

问题描述 投票:0回答:1

在方法底部调用存储库方法时出现此错误。当这些存储库方法移至其工作方法的顶部时。

这些是我遇到的错误,

org.springframework.orm.jpa.JpaSystemException: could not prepare statement; nested exception is org.hibernate.exception.GenericJDBCException: could not prepare statement

Caused by: java.sql.SQLException: Connection has already been closed.

org.hibernate.exception.JDBCConnectionException: could not extract ResultSet
org.springframework.transaction.TransactionSystemException: Could not roll back JPA transaction; nested exception is javax.persistence.PersistenceException: unexpected error when rollbacking

下面是调用存储库方法的方法,

    public ContractTabBean loadRoomCombinationInfo(Long hotelId) throws Exception {
            System.out.println("############################## loadRoomCombinationInfo : hotelId :" + hotelId);
    
            ContractTabBean contractTabBean = new ContractTabBean();
            try {
                System.out.println("ContractTabBean loadRoomCombinationInfo process start :::::::::: ");
                List<RoomtypeBean> roomTypeBeanList = contractTabBean.getRoomTypeBeanList();
                List<BedtypeBean> bedTypeBeanList = contractTabBean.getBedtypeBeanList();
                List<MealPlanBean> mealPlanBeanList = contractTabBean.getMealplanBeanList();
                contractTabBean.setHotelId(hotelId);
    
    
                List<Room> roomList = (List<Room>)this.roomRepository.findAll(); //2024-01-05
                System.out.println("####334 roomRepository.findByHotelId ==========" + roomList.size()); //2024-01-05
                // geting uniq list..
                List<String> roomtype = this.roomTypeRepository.roomTypeAsc();
                System.out.println("#### this.roomTypeRepository.roomTypeAsc() ==========" + roomtype.size());
                List<Roomtype> roomTypeList = new ArrayList<Roomtype>(0);
                if (Validator.validateListFirstObject(roomtype)) {
                    System.out.println("#### validateListFirstObject roomTypeList.size() ==========" + roomTypeList.size());
                    for (String roomtypestring : roomtype) {
                        System.out.println("#### roomtypestring ====" + roomtypestring);
                        Roomtype roomtypeobj = this.roomTypeRepository.findByRoomtypeAndHotelId(roomtypestring, hotelId);//error point
                        System.out.println("#### this.roomTypeRepository.findByRoomtypeAndHotelId ==========" + roomtypeobj);
                        if (Validator.validateObject(roomtypeobj)) {
                            roomTypeList.add(roomtypeobj);
                            System.out.println("#### validateObject - if ####");
                        } else {
                            List<Roomtype> roomtypeobj_ = this.roomTypeRepository.findByRoomtype(roomtypestring);
                            roomTypeList.add(roomtypeobj_.get(0));
                            System.out.println("#### validateObject - else ####");
                        }
                    }
                }
    
                // Get All room types and set checked as false
                // List<Roomtype> roomTypeList =
                // this.roomTypeRepository.findByOrderByRoomtypeAsc();
                roomTypeList.stream().forEach(roomType -> {
                    // roomTypeBeanList.add(new RoomtypeBean(roomType.getId(),
                    // roomType.getRoomtype(), false, hotelId));
                    RoomtypeBean roomTypeBean = new RoomtypeBean();
                    roomTypeBean.fillBeanFromEntity(roomType);
                    roomTypeBean.setChecked(false);
                    roomTypeBean.setHotelId(roomType.getHotel().getId());
                    roomTypeBeanList.add(roomTypeBean);
                    System.out.println("#### roomTypeList.stream().forEach(roomType -> { ####");
                });
    
                /*
                 * old logic before new requirement bedtype 17 july 2017 //geting uniq list..
                 * List<String> bedtype = this.bedTypeRepository.bedtypeAsc();
                 *
                 * List<Bedtype> bedTypeList = new ArrayList<Bedtype>(0);
                 * if(Validator.validateListFirstObject(bedtype)){ for (String bedtypestring :
                 * bedtype) { Bedtype bedtypeobj =
                 * this.bedTypeRepository.findByBedtypeAndHotelId(bedtypestring, hotelId);
                 * if(Validator.validateObject(bedtypeobj)){ bedTypeList.add(bedtypeobj); }else{
                 * List<Bedtype> bedtypeobj_ =
                 * this.bedTypeRepository.findByBedtype(bedtypestring);
                 * bedTypeList.add(bedtypeobj_.get(0)); } } }
                 */
    
                // geting uniq list..
                List<Bedtype> bedtype = this.bedTypeRepository.getAllbedtype();
                System.out.println("%%%%%%%% this.bedTypeRepository.getAllbedtype() ==========" + bedtype.size());
                System.out.println("%%%%%%%% bedtype.get(0)); ==========" + bedtype.get(0));
                System.out.println("%%%%%%%% bedtype obj ==========" + bedtype.get(0).getBedtype() + " | " +bedtype.get(0).getBedtype());
    
                List<Bedtype> bedTypeList = new ArrayList<Bedtype>(0);
                if (Validator.validateListFirstObject(bedtype)) {
                    for (Bedtype bedtypeDb : bedtype) {
                        Bedtype bedtypeobj = new Bedtype();
                        try {
                            if (null != bedtypeDb.getBedtype() || !bedtypeDb.getBedtype().equalsIgnoreCase("null")) {
                                if (null != hotelId) {
                                    System.out.println("%%%%%%%% not null bedtype ----------> " + hotelId + "| " + bedtypeDb.getBedtype());
                                    bedtypeobj = this.bedTypeRepository.findByBedtypeAndHotelId(bedtypeDb.getBedtype(), hotelId);
                                }
                            }else{
                                System.out.println("######## null bedtype ----------> " + hotelId + "| " + bedtypeDb.getBedtype());
                            }
                        }catch (Exception e){
                            System.out.println("Exce : "+ e);
                        }
                        System.out.println(">>>>>>>> 2 bedtypeobj getBedtype : " + bedtypeobj);
                        if (Validator.validateObject(bedtypeobj)) {
                            System.out.println("#### BT ==========" + bedtypeobj.getBedtype());
                            if (!bedTypeList.contains(new Bedtype(bedtypeDb.getBedtype()))) {
                                bedTypeList.add(bedtypeobj);
                                System.out.println("#### 1 BT size==========" + bedTypeList.size());
                            }
                        } else {
                            /*--2024-01-04
                            List<Bedtype> bedtypeobj_ = this.bedTypeRepository.findByBedtype(bedtypeDb.getBedtype());
                            System.out.println("####2 BT.findByBedtype ==========" + bedtypeobj_.size());
                            if (Validator.validateList(bedtypeobj_)) {
                                System.out.println("#### 2 BT ==========" + bedtypeobj_.size());
                                if (!bedTypeList.contains(new Bedtype(bedtypeobj_.get(0).getBedtype()))) {
                                    bedTypeList.add(bedtypeobj_.get(0));
                                    System.out.println("#### 2 BT size==========" + bedTypeList.size());
                                }
                            }*/
                        }
                        System.out.println("****** BT size==========" + bedTypeList.size());
                    }
                    System.out.println("****** 2 BT size==========" + bedTypeList.size());
                }
                System.out.println("****** 3 BT size==========" + bedTypeList.size());
                //2024-01-04 Collections.sort(bedTypeList);
                System.out.println("#### 3 BT size==========" + bedTypeList.size());
                // Get All Bed types and set checked as false
                // List<Bedtype> bedTypeList = this.bedTypeRepository.findByOrderByBedtypeAsc();
                bedTypeList.stream().forEach(bedType -> {
                    // bedTypeBeanList.add(new BedtypeBean(bedType.getId(),
                    // bedType.getBedtype(), hotelId));
                    BedtypeBean bedtypeBean = new BedtypeBean();
                    bedtypeBean.fillBeanFromEntity(bedType);
                    System.out.println("---------BED type "+bedType.getBedtype()+" ");
                    if(bedType!=null && bedType.getBedtype()!=null && bedType.getHotel()!=null){
                        bedtypeBean.setHotelId(bedType.getHotel().getId());
                        bedTypeBeanList.add(bedtypeBean);
                    }
    
                    System.out.println("####bedTypeBeanList size ==========" + bedTypeBeanList.size());
                });
    
                // Get All meal Plan and set checked as false
                // List<Mealplan> mealPlanList = mealplanRepository.findByOrderByMealplanplanAsc();
                List<Mealplan> mealPlanList = /* mealplanRepository.findByMealPlanCode()*/new ArrayList<>();
                System.out.println("####3 mealplanRepository.findByOrderByMealplanplanAsc() ==========" + mealPlanList.size());
                /*mealPlanList.stream().forEach(mealPlan -> {
                    // mealPlanBeanList
                    // .add(new MealPlanBean(mealPlan.getId(),
                    // mealPlan.getMealplanplan(), mealPlan.getMealplancode()));
                    MealPlanBean meanPlanBean = new MealPlanBean();
                    meanPlanBean.fillBeanFromEntity(mealPlan);
                    mealPlanBeanList.add(meanPlanBean);
                });*/
    
                // Iterate all roomlist and checked if given hotel has any room then set
                // it as checked
                System.out.println("this.roomRepository.findByHotelId(hotelId) hotelId"+hotelId);
                /* List<Room> roomList = (List<Room>)this.roomRepository.findAll();
                System.out.println("####334 roomRepository.findByHotelId ==========" + roomList.size()); 2024-01-05*/
                roomList.stream().forEach(room -> {
                    RoomtypeBean roomTypeBean = new RoomtypeBean(room.getRoomtype().getId(), room.getRoomtype().getRoomtype());
                    int index = roomTypeBeanList.indexOf(roomTypeBean);
                    if (index != -1) {
                        RoomtypeBean getRoomTypeBean = roomTypeBeanList.get(index);
                        getRoomTypeBean.setChecked(true);
                        getRoomTypeBean.setRoomBeanId(room.getId());
                        getRoomTypeBean.setRoomRank(room.getRoomrank());
                        if (room.getChildrenallowed().equals(Constants.STATUS_TRUE)) {
                            getRoomTypeBean.setChildrenAllowed(true);
                        }
                        if (room.getMultiplechildrate().equals(Constants.STATUS_TRUE)) {
                            getRoomTypeBean.setMultipleChildRate(true);
                        }
                    }
    
                    List<Bed> bedList = bedRepository.findByRoomIdOrderByBedtypeBedtypeAsc(room.getId());
                    System.out.println("####3 bedRepository.findByRoomIdOrderByBedtypeBedtypeAsc ==========" + bedList.size());
                    bedList.stream().forEach(bed -> {
                        BedtypeBean bedTypeBean = new BedtypeBean(bed.getBedtype().getId(), bed.getBedtype().getBedtype());
                        int indexoFBed = bedTypeBeanList.indexOf(bedTypeBean);
                        if (indexoFBed != -1) {
                            BedtypeBean getBedTypeBean = bedTypeBeanList.get(indexoFBed);
                            getBedTypeBean.setChecked(true);
                            getBedTypeBean.setRoomId(room.getId());
                            getBedTypeBean.setBedId(bed.getId());
                            getBedTypeBean.setAdntAdult(bed.getAdntadult());
                            getBedTypeBean.setMaxChildren(bed.getMaxchildren());
                            getBedTypeBean.setMaxOccupancy(bed.getMaxoccupancy());
                            getBedTypeBean.setStdAdults(bed.getStdadults());
                        }
    
                        List<Rateplan> ratePlanList = ratePlanRepository.findByBedId(bed.getId());
                        System.out.println("####33 ratePlanRepository.findByBedId ==========" + ratePlanList.size());
                        ratePlanList.stream().forEach(ratePlan -> {
                            MealPlanBean mealPlanBean = new MealPlanBean(ratePlan.getMealplan().getId(),
                                    ratePlan.getMealplan().getMealplanplan(), ratePlan.getMealplan().getMealplancode());
                            int indexOFMealPlan = mealPlanBeanList.indexOf(mealPlanBean);
                            if (indexOFMealPlan != -1) {
                                MealPlanBean getMeanPlanBean = mealPlanBeanList.get(indexOFMealPlan);
                                getMeanPlanBean.setChecked(true);
                                getMeanPlanBean.setBedId(bed.getId());
                            }
                            bedTypeBean.getMealplanBeanList().add(mealPlanBean);
                        });
                        mealPlanBeanList.stream().forEach(meanPlan -> {
                            if (!bedTypeBean.getMealplanBeanList().contains(meanPlan)) {
                                bedTypeBean.getMealplanBeanList().add(meanPlan);
                            }
                        });
                        roomTypeBean.getBedtypeBeanList().add(bedTypeBean);
                    });
    
                });
                contractTabBean.setSelectedroomTypeBeanList(this.loadRoomcombinationWithItsRealatedBedInfoAndRatePlan(hotelId));
                System.out.println("ContractTabBean loadRoomCombinationInfo process end :::::::::: ");
                return contractTabBean;
            }catch (Exception e){
                System.out.println("ContractTabBean loadRoomCombinationInfoException :::::::::: " + e);
                e.printStackTrace();
            }
            return contractTabBean;
        }

我在此方法的类上方使用了

@Transactional
@Service
注释。

java spring spring-boot exception jdbc
1个回答
0
投票

我强烈建议遵守单一责任原则。重构该方法,仅专注于加载房间组合信息。避免在此方法中更新或创建数据,因为它应该严格处理指定的功能。目前,即使能够调试代码,识别所描述的问题也具有挑战性。

© www.soinside.com 2019 - 2024. All rights reserved.