Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for existing, persistent SQLite connection in PHP

PHP supports the use of persistent SQLite connections. However, a problem arises in trying to run maintenance scripts (like a file backup), with such a connection opened. These scripts are likely to be run without a server restart every now and then, in periods of low traffic.

How can I check if there is currently an SQLite persistent connection, without opening / calling one (hence creating the connection)?

See: php SQLite persistent connection

like image 365
PicoCreator Avatar asked Oct 20 '25 14:10

PicoCreator


1 Answers

If you use PDO you can just check if the handler is null or not before you run the maintenance scripts. That way you wont be creating a connection like with sqlite_popen()

Create a persistent connection with PDO if you want: $handler = new PDO('sqlite:test.db', NULL, NULL, array(PDO::ATTR_PERSISTENT => TRUE));

...Then you can just close the connection before the maintenance script is called, assuming it is on some sort of schedule:

if(!is_null($handler)){
$handler = null;
//run maintenance script, recreate connection once finished
}
like image 54
Trevor Arjeski Avatar answered Oct 23 '25 04:10

Trevor Arjeski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!