vendredi 26 décembre 2014

Pdo 3, multiple runnings: error: 17 database schema has changed

I have this test code:



session_start();
session_write_close();
set_time_limit(0);

for ($i = 1; $i < 1000; $i++)
{
if (rand(1,5) == 1)
{
$file_db = new PDO('sqlite:x.db');
$file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$file_db->exec('DROP TABLE IF EXISTS messages');
$file_db = null;
}
$file_db = new PDO('sqlite:x.db');
$file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$file_db->exec('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,message TEXT,time INTEGER)');
$file_db->query('INSERT INTO messages VALUES (null, "'.rand(1,9999).'", "'.rand(1,99).'", date("now"))');
$file_db = null;
}

$file_db = new PDO('sqlite:x.db');
$file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$i = 0;
foreach ($file_db->query('SELECT * FROM messages') as $m)
{
$i++;
echo $i.' -> '.$m['id'].'<br>';
}


with this code Im testing if the PDO 3 works with multiple concurrencies. If this code runs in one thread, all is ok. But as soon as I start two instance of this, I get this message:



Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 17 database schema has changed' in code.php:* Stack trace: #0 code.php(*): PDO->query('INSERT INTO mes...') #1 {main} thrown in code.php on line *


the error line is where the INSERT INTO is. Why does this happen and how to dodge it? (Im creating a cache-system where there is do a possibility that tables gets created and dropped. I know there is TRUNCATE, I just want to know why this happens)


Aucun commentaire:

Enregistrer un commentaire