尝试在多个表中保存数据时发生数据库死锁

时间:2018-10-25 12:11:49

标签: php codeigniter transactions database-deadlocks

我开发了一个应用程序来处理多个人试图预订房间的预订。 到现在为止一切都还不错,但是随着流量增加了100-200,应用程序立即陷入数据库僵局。

错误:

Deadlock found when trying to get lock; try restarting transaction
INSERT INTO `booking_passenger_map` (`booking_id`, `passenger_id`) VALUES (7665, 18111)

代码:

$this->db->trans_start();
        if(isset($user["booking_id"]))
        {
            $bd=$this->common->getBookingById($user["booking_id"]);
            if($bd[0]->booking_status_id!=CONFIRM && $bd[0]->booking_status_id!=PAID_BUT_WAITING)
            {
                $booking_id=$user["booking_id"];
                $this->common->deleteBooking($booking_id);
            }
        }
        $booking_id=$this->common->saveBooking($data);
        //End Create Booking

        //Saving Passengers
        for($i=1; $i<=count($passengers); $i++)
        {
            $passenger_id=$this->common->savePassenger($passengers["pass_".$i]);
            //Booking Passenger Map
            $bpm["booking_id"]=$booking_id;
            $bpm["passenger_id"]=$passenger_id;
            $this->common->saveBookingPassMap($bpm);
        }
        //End Saving Passengers

        //Creating Booking Room Map
        foreach($cart AS $detail) 
        {
            if(in_array($detail["cat"]->id, $this->config->item("dmtry")))
            {
                $no_of_beds = $detail[$detail["deck"]->id]["num_of_beds"];
                $needed_beds=$this->common->getNeededDmtryCatDataById($detail["cat"]->id, $sf["schedule_id"], $no_of_beds);
                if(count($needed_beds)<$no_of_beds)
                {
                    $this->session->set_flashdata('msg', 
                    "<div class='alert alert-success'>Sorry, Somebody else was more quick in booking the beds from dormetry you 
                    are trying. No worries we have more options for you</div>");
                    redirect(base_url()."booking/show_rooms");
                }
                else
                {
                    foreach($needed_beds AS $needed_bed){
                        $brm["booking_id"]=$booking_id;
                        $brm["category_id"]=$detail["cat"]->id;
                        $brm["room_id"]=$needed_bed->r_id;
                        $brm["bunk_id"]=$needed_bed->b_id;
                        $brm["no_of_ppl"]=1;
                        $brm["room_capacity"]=$detail["cat"]->capacity;
                        $brm["b_adult_rate"]=$detail["rate"]->adult_rate;
                        $brm["b_child_rate"]=$detail["rate"]->child_rate;
                        $brm["b_infant_rate"]=$detail["rate"]->infant_rate;
                        $this->common->saveBookingRoomMap($brm);
                    }                   
                }
            }
            else
            {
                $no_of_rooms = $detail[$detail["deck"]->id]["num_of_rooms"];
                $needed_rooms=$this->common->getNeededRoomCatDataById($detail["cat"]->id, $sf["schedule_id"], $no_of_rooms);
                if(count($needed_rooms)<$no_of_rooms)
                {
                    $this->session->set_flashdata('msg', 
                    "<div class='alert alert-success'>Sorry, Somebody else was more quick in booking the rooms you 
                    are trying. No worries we have more options for you</div>");
                    redirect(base_url()."booking/show_rooms");
                }
                else
                {
                    $temp_ppl=$detail["ppl"];
                    foreach($needed_rooms AS $needed_room){
                        if($temp_ppl>=$detail["cat"]->capacity){
                            $no_of_ppl=$detail["cat"]->capacity;
                        }
                        else if($temp_ppl<$detail["cat"]->capacity){
                            $no_of_ppl=$temp_ppl;
                        }
                        if($no_of_ppl<$detail["cat"]->capacity){
                            $empty_beds=$detail["cat"]->capacity-$no_of_ppl;
                        }
                        if($empty_beds)
                        {
                            $brm["extra_percent"]=EXTRA;
                        }
                        $brm["booking_id"]=$booking_id;
                        $brm["category_id"]=$detail["cat"]->id;
                        $brm["room_id"]=$needed_room->r_id;
                        $brm["bunk_id"]=0;
                        $brm["no_of_ppl"]=$no_of_ppl;
                        $brm["room_capacity"]=$detail["cat"]->capacity;
                        $brm["b_adult_rate"]=$detail["rate"]->adult_rate;
                        $brm["b_child_rate"]=$detail["rate"]->child_rate;
                        $brm["b_infant_rate"]=$detail["rate"]->infant_rate;
                        $this->common->saveBookingRoomMap($brm);

                        $temp_ppl-=$no_of_ppl;
                    }                   
                }
            }       
        }

        $this->db->trans_complete();

        //End Creating Booking Room Map
        if($this->isTransSuccess())
        {
            $user["booking_id"]=$booking_id;    
            $this->session->set_userdata('key_user', $user);
            $cca_data=$this->getCCAData($data["amount_paid"]);
            $back_user=$this->session->userdata("back_user");
            if($back_user!=null && $back_user->user_role_id==ADMIN)
                $this->confirmWithoutPayment();
            else
                $this->ccavenue->pay($cca_data);
            exit;
        }
        else
            redirect(base_url()."booking/cart");

我已经做了很多工作,还尝试了更改插入顺序,但是没有任何效果,请仔细阅读代码和帮助,这对我来说意义非凡。

0 个答案:

没有答案