PHP supports the use of persistent SQLite connections. However, a problem arises in trying to run maintenance script开发者_Python百科s (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
If you have access to shell_exec() or exec, you can run a shell command to check to see if a SQLite process is running using something like top
or maybe a command like lsof -i -n -P | grep sqlite
assuming sqlite
is the name of the process.
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
}
To quote the manual at https://www.php.net/manual/en/features.persistent-connections.php:
'When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates the link. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable).'
That seems to imply that there no need to check prior to trying to connect.
Note that the article goes into a lot of considerations about how persistent connections work out with web servers, particularly about how subsequent processes attempting to use the connection may be blocked by what a fault in an earlier process created, implying that persistent connections may not be reliable in a web server environment given how web sessions can terminate at any time.
In particular, it recommends that persistent connections are not used for scripts that use table locks or transactions.
精彩评论