4

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

1
  • The link is broken (404). Commented Feb 17, 2023 at 12:39

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is if you call the handler it will never be null since it's a persistent connection and I believe the connection will start if you try to call the handler. That may not be true though, would have to test it. See: stackoverflow.com/questions/999310/…
@David Checking if the handler object is null won't open a connection. I mean, you can create a persistent connection with PDO if you want: 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.
Couldn't you run into an issue where someone had a connection open and then the maintainence script ran closing the connection while someone else was using it? When you recreate it, they'll be a new ID for the connection and other scripts using the connection in the meantime would break? Normally wouldn't be too bad, but I'm thinking some maintainence scripts might take a while to run.
@David Seems to me that is just one of the negative aspects of keeping a connection open. That problem can probably be fixed by delaying the maintenance script from running until all users have disconnected from the database. I'm liking this discussion, thanks for the input Dave.
Thats the main issue: to check without creating one. Alternatively, I'm considering a momentary stop (2 sec?) window for the script to run. When there is no active connection. But that may be an issue on a popular sites.
|
1

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.

1 Comment

Would be better to grep for the name of the actual SQLite3 database, but yeah - lsof is really the only way to tell if (any process) has the file open without attempting to open it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.