cron锁表每5分钟一次

时间:2015-08-26 10:05:40

标签: php mysql transactions deadlock

每运行5,10,60,3600分钟的crons, 当我检查errorlog时,我看到非常多的死锁异常,几乎每5分钟cron就会发送死锁,超时和序列化错误来记录。

如果我理解使用同一个表的两个或多个查询时发生了很好的死锁,那么我的代码中很有可能。

我在每个cron中使用表MAP 2次,因为我需要映射每个团队,

例如:

  切尔西在映射后回归切尔西足球俱乐部   映射后的埃弗顿回归埃弗顿足球俱乐部

我从大型xml文件中获取团队,当我杀死所有进程时cron5在2分钟内运行即可,但是当其他进程启动时,它需要这样的时间......

当我通过SHOW OPEN TABLES WHERE in_use>0查看时,我明白了 map表in_use主要是2,有时是1。

function getMap($country,$data){
    //echo $data . '<br>';
    $fdata = $data;
    $query = $this->PDO->prepare("CALL GetMap(:data)");
    $query->bindParam(':data', $fdata);
    $query->execute();
    //echo 'DATA: ' . $data. ' RES: '.$res . '<br>';
    $res = $query->fetchColumn();
    if($res!=''){
        $query->closeCursor();
        $sql = "UPDATE map SET Country = :country WHERE Base = :base";
        $q2 = $this->PDO->prepare($sql);
        $q2->bindParam(':country', $country);
        $q2->bindParam(':base', $res);
        $q2->execute();

        //echo $res;
        return $res;
    }
    else
    {
        $query->closeCursor();
        $this->fillMap($country,$fdata);
        return $fdata;
    }
}

这是GetMap的程序

DELIMITER //
CREATE PROCEDURE GetMap(p_team VARCHAR(100))
BEGIN
SELECT Base FROM map WHERE 
map.Base = p_team OR
Ver1 = p_team OR
Ver2 = p_team OR
Ver3 = p_team OR
Ver4 = p_team OR
Ver5 = p_team OR
Ver6 = p_team OR
Ver7 = p_team OR
Ver8 = p_team OR
Ver9 = p_team OR
Ver10 = p_team OR
Ver11 = p_team OR
Ver12 = p_team;
end //
DELIMITER ;

现在我的问题是当使用php锁定该表时如何解锁表映射? 另外我需要说的是我在xml读取之前启动事务,并在一切都完成时提交,因此事务等待大约2分钟时可能没有锁定映射。这可能是我得到超时错误的原因,我可以将其移动到循环内或更改mysql超时。

编辑详细代码: 检查$ this-&gt; getMap();什么是最常用的表。

function sendOddsToDb(){    
try{
    $this->conn->connect();
    $this->PDO->beginTransaction();

    $countries = $this->OddCountries; 
    $stmt = $this->PDO->prepare($this->insOdds);
    //$dbempty = $this->countRows('odds');
    //$this->clrLeagues();                  
    foreach($countries as $country){
     $path = "{$this->base}/feeds/odds/{$country}odds.xml";
     $odds_feed = $this->parser->parseFile($path);

     $fupddate = date('Y-m-d H:i:s',strtotime($odds_feed->attributes()->updated));
     $newcountry = '';
     if(isset($odds_feed->league)){

     $newcountry = $odds_feed->league->attributes()->country;
     $fcountries = $this->filterCountries($newcountry);
      echo $fcountries . '<br>';
      $sql = "select UpdatedDate from odds where Country = '{$fcountries}' order by UpdatedDate desc limit 1";
      $res = $this->PDO->query($sql);
      $dbupd = date('Y-m-d H:i:s',strtotime($res->fetchColumn()));

      if(!$this->sameUpdated($fupddate,$dbupd)){
        foreach($odds_feed->league as $league){ //za svaku ligu u fidu
            $subid = $league->attributes()->sub_id;

            $sportname = '';
            $sportid = 0;
            if($odds_feed->attributes()->sport == 'soccer' || $odds_feed->attributes()->sport == 'football'){
                $sportname = 'football';
                $sportid = 1;
            }
            else{
                $sportname = NULL;
            }
            $lg2 = '';
            $lg = $league->attributes()->name;
            $ctr = $league->attributes()->country.':';
            if(!contains(strtolower($lg),strtolower($ctr))){
                $lg2 = ucfirst($ctr) . ' ' . ucfirst($lg);  
            }
            else{
                $lg2 = $league->attributes()->name;
            }

            $flg = $this->getMap2($lg2);
            $this->sendLeaguesToDb($league->attributes()->id, $flg ,$fcountries,$sportid,$subid,'tipgin');
            foreach($league->match as $match){ 

                $dbdate = date('Y-m-d',strtotime($match->attributes()->date));
                $updateddate = date('Y-m-d H:i:s',strtotime($odds_feed->attributes()->updated));
                $dbtime = date('H:i:s', strtotime($match->attributes()->time));


                $datetime = date('Y-m-d H:i:s',strtotime($dbdate . $dbtime));   

                $hteam = $this->getMap($fcountries,toShort($match->home->attributes()->name));  
                $ateam = $this->getMap($fcountries,toShort($match->away->attributes()->name));  

                //echo $hteam . ' ' . $ateam;
                $fixid = 0;
                if($match->attributes()->id == 0 || $match->attributes()->id == ''){
                    if($match->attributes()->alternate_id == 0 || $match->attributes()->id == ''){
                        $fixid = $match->attributes()->alternate_id_2;
                    }
                    else{
                        $fixid = $match->attributes()->alternate_id;
                    }
                }
                else{
                    $fixid = $match->attributes()->id;
                }       
                //$m = $hteam . ' ' . $ateam;
                //var_dump('MATCH: ' . $m . ' HTEAM: ' . mb_detect_encoding($hteam). ' ATEAM: ' . mb_detect_encoding($ateam));
                if($match->odds != NULL or $match->odds != ''){         
                foreach ($match->odds as $odds) { 
                    //var_dump($odd);
                    if($odds->type != NULL or $odds->type != ''){
                    foreach ($odds->type as $type) { 
                        if($type->bookmaker != NULL or $type->bookmaker != ''){
                        foreach ($type->bookmaker as $bookmaker) { 
                            //$oldbook = ;
                            if($bookmaker->attributes()->name != 'BetClick'){
                            if($bookmaker->attributes()->name != 'Bodog'){
                            if($bookmaker->attributes()->name != 'YouWin'){
                            $homeodds = NULL;
                            $drawodds = NULL;
                            $awayodds = NULL;
                            $numodds = NULL;
                            $dbhandicap = NULL;
                            $id2 = '0';
                            $typename = $this->getMap2($type->attributes()->name);

                            $fbooks = $this->filterBooks($bookmaker->attributes()->name);   

                            if($typename == 'Over/Under' || $typename == 'Over/Under 1st Half' || $typename == 'Over/Under 2nd Half'){

                                foreach ($bookmaker->total as $total) { //daj kvote za svaki mec

                                $numodds = $total->attributes()->name;
                                foreach($total->odd as $odd){
                                switch ($odd->attributes()->name) {
                                    case 'Over':
                                        $homeodds = $odd->attributes()->value;
                                        break;
                                    case 'Under':
                                        $awayodds = $odd->attributes()->value;
                                        break;
                                }

                                $id2 = $numodds;
                                }

                            //echo "HOME TEAM: {$hteam} <BR>";
                            //echo "AWAY TEAM: {$ateam} <BR>";
                            /*
                            echo 'IF ID: ' . $match->attributes()->alternate_id_2 . $league->attributes()->country . " ODDS: {$homeodds} {$drawodds} {$awayodds} TYPE: {$typename}  BOOK: {$fbooks}  ID2: {$id2}" . "<br>";
                */
                            $this->execOdds($stmt, $fixid, $updateddate,
                            $typename, $fcountries,$league->attributes()->name,
                            $league->attributes()->id, $subid, $league->attributes()->cup,$sportname,
                            $hteam,$match->home->attributes()->id,
                            $ateam,$match->away->attributes()->id, $datetime, $dbdate,
                            $dbtime, $match->attributes()->status, $numodds,$homeodds,$drawodds,
                            $awayodds, $fbooks, $bookmaker->attributes()->id, $dbhandicap, $id2);
                            }
                            }

                            else if($typename == 'Handicap' || $typename == '3Way Handicap'){
                            $homeodds = array();
                            $drawodds = array();
                            $awayodds = array();
                            $arrhandicap = array();
                            $fhandicap = '';

                            foreach ($bookmaker->handicap as $handicap) { //daj kvote za svaki mec 
                            if (strpos($handicap->attributes()->name,'+') !== false) {
                            $fhandicap = str_replace('+','',(string)$handicap->attributes()->name);                             
                            }else{
                            $fhandicap = (string)$handicap->attributes()->name;
                            }
                            $newhandi = '';
                            foreach($handicap->odd as $odd){
                                    //$odd = $handicap->odd;
                                    switch ($odd->attributes()->name) {
                                    case '1':
                                        $newhandi = (string)$fhandicap;
                                        $homeodds[(string)$newhandi] = 
                                        (string)$odd->attributes()->value;
                                        break;
                                    case 'X':
                                        $newhandi = $fhandicap;
                                        $drawodds[(string)$newhandi] = 
                                        (string)$odd->attributes()->value;
                                        break;  
                                    case '2':
                                        if (strpos($fhandicap,'-') !== false) { //if negative 
                                            $newhandi = str_replace('-','',(string)$fhandicap); //to positive
                                            //echo 'NEGATIVE - OLD: ' . $fhandicap . ' NEW: '.$newhandi . '<br>';
                                        }
                                        elseif($fhandicap=='0'){
                                            $newhandi = '0';
                                        }
                                        else{ //if positive
                                            $newhandi = '-' . (string)$fhandicap; //to negative
                                            //echo 'POSITIVE - OLD: ' . $fhandicap . ' NEW: '.$newhandi . '<br>';
                                        }
                                        $awayodds[(string)$newhandi] = (string)$odd->attributes()->value; 
                                        break;
                                    }
                                    $arrhandicap[] = (string)$newhandi;
                                }
                            }
                            //print_r($arrhandicap);
                            //$arrhandicap[] = sort($dbhandicap);
                            $inhand = '';
                            $varhome = '';
                            $vardraw = '';
                            $varaway = '';

                            foreach($arrhandicap as $hand){
                            $inhand = (string)$hand;
                            if(isset($hand)){
                            //echo $typename. ': ' . $hand . '<br>';

                            if((array_key_exists($inhand,$homeodds)) && (isset($homeodds[$inhand]))){
                                $varhome = $homeodds[$inhand];
                            }

                            if((array_key_exists($inhand,$awayodds)) && (isset($awayodds[$inhand]))){
                                $varaway = $awayodds[$inhand];
                            }
                            if((array_key_exists($inhand,$drawodds)) && (isset($drawodds[$inhand]))){
                                $vardraw = $drawodds[$inhand];
                            }

                            $this->execOdds($stmt, $fixid, $updateddate,
                            $typename, $fcountries,$league->attributes()->name,
                            $league->attributes()->id, $subid, $league->attributes()->cup,$sportname,
                            $hteam,$match->home->attributes()->id,
                            $ateam,$match->away->attributes()->id, $datetime, $dbdate,
                            $dbtime, $match->attributes()->status, $numodds,$varhome,
                            $vardraw,$varaway, $fbooks, 
                            $bookmaker->attributes()->id, $hand, $hand);
                            /*echo 'ELSEIF ID: ' . $match->attributes()->id . $league->attributes()->country . " HOMEODDS: {$varhome}  DRAW: {$vardraw}  AWAYODDS: {$varaway} TYPE: {$typename}  BOOK: {$fbooks}  ID2: {$inhand}" . "<br>";                                                 
                            */}
                            }
                            }
                            else{
                            foreach ($bookmaker->odd as $odd) { //daj kvote za svaki mec
                                if($typename == 'Home/Away'){
                                    $typename = 'Draw No Bet';
                                }
                                switch ($odd->attributes()->name) {
                                    case '1':
                                    case '1X':
                                    case 'Yes':
                                        $homeodds = $odd->attributes()->value;
                                        break;
                                    case 'X':
                                    case 'X2':
                                        $drawodds = $odd->attributes()->value;
                                        break;
                                    case '2':
                                    case '12':
                                    case 'No':
                                        $awayodds = $odd->attributes()->value;
                                        break;
                                }

                                $id2 = 0;
                                //echo 'ID: ' . $match->attributes()->alternate_id_2 . $league->attributes()->country . " ODDS: {$homeodds} {$drawodds} {$awayodds} TYPE: {$typename}  BOOK: {$fbooks}  ID2: {$id2}" . "<br>";
                              }
                            /*
                            echo "ELSE HOME TEAM: {$hteam} <BR>";
                            echo "ELSE AWAY TEAM: {$ateam} <BR>";   
                            */
                            $this->execOdds($stmt, $fixid, $updateddate,
                            $typename, $fcountries,$league->attributes()->name,
                            $league->attributes()->id, $subid, $league->attributes()->cup,$sportname,
                            $hteam,$match->home->attributes()->id,
                            $ateam,$match->away->attributes()->id, $datetime, $dbdate,
                            $dbtime, $match->attributes()->status, $numodds,$homeodds,$drawodds,
                            $awayodds, $fbooks, $bookmaker->attributes()->id, $dbhandicap, $id2);

                            }
                            }
                        }
                        }
                        }
                        }
                        }
                    }
                    }
                    }
                }
            }
        }
        //echo $country.'<br>';
    }
    }
    $this->PDO->commit(); //SAVE
    $this->conn->close();
    }
    catch(Exception $e){
//echo $e;
    }
}

private function execOdds($stmt, $matchid, $upddate, $type, $country, $league, $leagueid, $subid, $cup, $sport, $hteam, $hteamid, $ateam, $ateamid, $datetime, $date, $time, $status, $numodds, $homeodds, $drawodds, $awayodds, $book, $bookid, $handicap, $id2){

    $event = $hteam . ' - ' . $ateam;

    $fcountry = $country;
    $fleague = $league;

    $stmt->bindParam(':fixturematchid', $matchid);$stmt->bindParam(':updateddate',$upddate);$stmt->bindParam(':type',$type);
    $stmt->bindParam(':country',$fcountry);$stmt->bindParam(':league',$fleague);$stmt->bindParam(':leagueid',$leagueid);
    $stmt->bindParam(':subid',$subid);
    $stmt->bindParam(':cup',$cup);$stmt->bindParam(':eventname',$event);$stmt->bindParam(':sportname',$sport);
    $stmt->bindParam(':hometeam', $hteam);$stmt->bindParam(':hometeamid', $hteamid);$stmt->bindParam(':awayteam', $ateam);
    $stmt->bindParam(':awayteamid', $ateamid);$stmt->bindParam(':datetime',$datetime);$stmt->bindParam(':date',$date);
    $stmt->bindParam(':time',$time);$stmt->bindParam(':status',$status);$stmt->bindParam(':numodds', $numodds);
    $stmt->bindParam(':homeodds', $homeodds);$stmt->bindParam(':drawodds',$drawodds);$stmt->bindParam(':awayodds',$awayodds);
    $stmt->bindParam(':bookmaker',$book);$stmt->bindParam(':bookmakerid',$bookid);$stmt->bindParam(':handicap', $handicap);
    $stmt->bindParam(':id2', $id2);
    $stmt->execute(); //EXEC*/
}

0 个答案:

没有答案