5

I'm using PDO to access two SQLite 3 databases in PHP. I want to switch the database files during a query by renaming them but I can't do that while the files are open as it gives an error that the file is being used by another process. I've tried turning off persistent connections and setting the handles to null but neither work.

Is there really no way to close a PDO handle and release the lock on the database file?

Tamlyn
  • 22,122
  • 12
  • 111
  • 127

2 Answers2

12

I believe unset($var) does that, I use it on my pdo sqlite project and it works like I want it to :)

gnur
  • 4,671
  • 2
  • 20
  • 33
5

Set all references to the handle to null (or to anything except the PDO object, really) and the runtime will destruct the object, which will close the connection.

$db = new PDO('...');
// Do some stuff

$db = null;
// Assuming this was the last reference to that PDO
// object, the runtime will destroy the object and
// its connection.
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 3
    Sensible suggestion but experience tells me that your better to `unset()` them instead, as the garbage collector in PHP<5.3 was a bit fickle with regards to `$var = NULL` operations and wouldn't destroy the resources till it felt like it (try doing it with an `fsockopen()` resource on 5.2.x with Wireshark open and you will see it takes 5-10 seconds before Zend wakes up and sends an RST packet, unless the script ends). Unset, for some reason, doesn't have this problem. – DaveRandom Aug 15 '11 at 17:00