1

Here is the problem

There is a script that after X amount of time (unknown amount between 5 and 40 minutes) throws the following error: MySQL server has gone away which Kohana turns into a Database_Exception 2006 As a result some of the information is not saved to the DB.

Here is what I think might work

class Model_Bar extends ORM {

protected $_belongs_to = array(
    'foo' => array()
);

public function save(){ //Extends the save method
    try {
        $result = parent::save(); //Try parent save
    }  catch (Database_Exception $e) { //Catch exception
        if ($e->getCode() == 2006) { //If exception code == 2006 then DB has gone away
            mysqli_ping(); //Try to refresh DB link
            $result = parent::save(); //Try parent save again
        } else { //Exception code != 2006
            throw new Exception($e); //Throw new DB exception
        }
    }
    return $result; // Return the result from parent::save()
    }
}

The Question: How can I refresh the link to the DB in Kohana's ORM?

More Info:

Thanks!

Community
  • 1
  • 1
Lord Otori
  • 349
  • 1
  • 4
  • 17
  • 1
    Can you not just reconnect after your long process, and before your save? – Petah Jul 04 '12 at 18:05
  • Not an ORM issue -that is an applicatio ndesign question. The ORM is not really relevant. – TomTom Jul 04 '12 at 18:06
  • @Petah, That's exactly what I want to do. That's why I've extended the save method. The thing is I cannot find a way to do it. – Lord Otori Jul 04 '12 at 18:36
  • @TomTom, I disagree. This is something that happens inside of Kohana's ORM so I believe it's relevant. but thanks for taking the time :) – Lord Otori Jul 04 '12 at 18:36
  • Well, you can disagree all you want - the same discussion I had with people on a BS 2000 (mainframe) and programmers in .NET - handling db level errors is not a tool decision ,it is an application decision. Do you retry? Do you propagate the error? What do you do? Independant on the technology used. FOr example I have an app here where the ONLY valid answer is "STOP THE APPLICATION NOW BEFORE DAMAGE HAPPENS" and "TRIGGER WATCHDOG TO CLOSE OPEN OPERATIONS NOW". – TomTom Jul 04 '12 at 18:43
  • @TomTom Thanks again for your comments but the question remains the same. What I want to do is clear "refresh the link to the DB" and I need a Method that can work inside of Kohana's ORM for that. – Lord Otori Jul 04 '12 at 19:19

1 Answers1

0

That's a problem of either

  • Reached the MYSQL configured timeout
  • Exceeded packet size
  • Loss of packets

If you are executing a long insert, like a bulk insert, if your DB isn't configured for that, no change in your code will have any effect. You may try to reconfigure your MYSQL instance, then rule out the MYSQL blame, then after you try to amend your code (which I doubt is the source of the problem). Retrying saving won't help much, but to make the DB even busier.

Another thing, if you are using a proxy (like HAProxy), check the timeouts on that as well.

pocesar
  • 6,860
  • 6
  • 56
  • 88