Disabling ATTR_EMULATE_PREPARES in combination with persistent connections results in dropped connection

StackOverflow https://stackoverflow.com/questions/23437470

  •  14-07-2023
  •  | 
  •  

سؤال

I'm currently re-factoring my PHP framework's database wrapper class to use PDO.

I have few main tasks which I must accomplish with the database wrapper class:

  • always exact same result for basic DML operations (INSERT, SELECT, UPDATE, DELETE) against MySQL, PostgreSQL and Oracle databases
  • persistent connections only (new connection per request is not an option for Oracle as the cost of establishing a connection is very high in terms of latency)
  • bind parameters at all times (provide out of the box SQL injection proof methods)

Unfortunately I experience "ERR_CONNECTION_RESET" every single time I use the combination of the following two PDO attributes ATTR_EMULATE_PREPARES => FALSE and ATTR_PERSISTENT => TRUE while executing query against a MySQL database.

Here is the example code (not the actual wrapper class, but it duplicates the same error):

$connection = new PDO(
    'mysql:host=localhost;dbname=test'
    ,'root'
    ,''
    ,array(
        PDO::ATTR_AUTOCOMMIT => FALSE
        ,PDO::ATTR_CASE => PDO::CASE_LOWER
        ,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ,PDO::ATTR_EMULATE_PREPARES => FALSE
        ,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ,PDO::ATTR_PERSISTENT => TRUE
        ,PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
    )
);

if (!$connection->inTransaction()) $connection->beginTransaction();

$statement = $connection->prepare('INSERT INTO dms_devices (model_id, serial_no, name, status) VALUES (:model_id, :serial_no, :name, :status)');

foreach (array('model_id' => 1, 'serial_no' => 12219321, 'name' => 'Demo', 'status' => 'DSS_MANUFACTURED') as $name => $value) {
    $statement->bindValue(':'.$name, $value);
}

$statement->execute();

$connection->commit();

Once I comment out any of the attributes ATTR_EMULATE_PREPARES or ATTR_PERSISTENT it works without problem.

I use WampServer 2.4 64-bit (Apache 2.4.4, PHP 5.4.12, MySQL 5.6.12) on my development machine.

Any suggestions what would be the best solution (keeping in mind the goals I must achieve)?

هل كانت مفيدة؟

المحلول

Found out that this is already registered PHP segmentation fault bug #61411.

Unfortunately it is still not fixed in version 5.4.12.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top