j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Reconnect to DB #327

Closed LoyCog closed 6 years ago

LoyCog commented 6 years ago

I have a situation where php script is running for a long time and updating DB. Sometimes there is problems with DB server, for example a restart and connection is lost. Is there a way for idiorm to force to reestablish connection? Bcs now I have to restart my script in order for idiorm to make new connection.

alessandroraffa commented 6 years ago

In situations like this, I usually page the result of the query using limit and offset and a temporary column to filter done/undone records.

The script can call itself or, better, you could user an external client (i.e. jQuery) to poll the script every x seconds/minutes.

If you think this could be suitable for you, I can post some code using idiorm.

LoyCog commented 6 years ago

In my case php script is running as a service, reading from queue and executing queries. It would be waste of resources if I had to spawn new php processes, so your proposed solution would not work for me.

joseph-montanez commented 6 years ago

@LoyCog I know mysqli has a ping command to keep a connection open, there is no such thing in PDO which is what this library uses. You can initialize PDO outside of this library and set it up as a persistent connection i.e:

$opt = array(
    PDO::ATTR_PERSISTENT => true
);
$conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass, $opt);
ORM::set_db($conn);

Or you can pull PDO out and check if it is connected i.e:

$status = $conn->getAttribute(PDO::ATTR_CONNECTION_STATUS);

And if not then reset the libraries's PDO.

If all else fails you can just execute SELECT 1 to try to keep the connection open every few seconds ot something.

treffynnon commented 6 years ago

I am guessing that the discussion here has answered your question, so closing.