#41579 [Fbk->Opn]: PDO timeout lost connection

From: Date: Mon, 04 Jun 2007 09:38:28 +0000
Subject: #41579 [Fbk->Opn]: PDO timeout lost connection
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-113334@lists.php.net to get a copy of this message
 ID:               41579
 User updated by:  vtsupermok at gmail dot com
 Reported By:      vtsupermok at gmail dot com
-Status:           Feedback
+Status:           Open
 Bug Type:         PDO related
 Operating System: Window
 PHP Version:      5.2.3
 New Comment:

Dear sir

Sorry for my incomplete example, let me give you a full set program.

<?
function dbConnect($db){
    try{
       
$database="mysql:host={$db['host']};port={$db['port']};dbname={$db['name']}";
        $pdo = new PDO($database, $db['userID'], $db['userPwd'],
array(PDO::ATTR_PERSISTENT => true));
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_TIMEOUT,5);
        return $pdo;
    } catch (PDOException $e) {
        throw $e;
    }
}

function dbException($e,$db){
    echo "<p>";
    echo "Message : " . $e->getMessage() . "<br/>";
    echo "File : " . $e->getFile() . "<br/>";
    echo "Line : " . $e->getLine() . "<br/>";
    echo "</p>";
    die();
}       

function dbQuery($pdo,$sql,$db){
    try{
        return $pdo->query($sql);
    } catch (PDOException $e) {
        throw $e;
    }
}

function dbRecord($result,$db){       
    $resultSet=$result->fetchAll(PDO::FETCH_ASSOC);
    $counter=count($resultSet);
    if ($counter>0){
        for ($i=0;$i<$counter;$i++){
            foreach ($resultSet[$i] as $key => $value){
                echo "get data: "."$value"."<Br>";
            }
        }
    }
}

try {
    $db=array(
        'name'=>'test',
        'host'=>'127.0.0.1',
        'port'=>'3306',
        'userID'=>'abc',
        'userPwd'=>'abc',
        'charset'=>'latin1',
        'encoding'=>'big-5'
    );
    echo 'Step 1<Br />';
    $pdo=dbConnect($db);
    echo 'Step 2<Br />';
    $sql="select 'a','b','c'";
    $result=dbQuery($pdo,$sql,$db);
    echo 'Step 3<Br />';
    dbRecord($result,$db); 
    echo 'Step 4<Br />';
} catch (PDOException $e) {
    dbException($e,$db);   
}
     
?>

the testing process should be like this
1.run this script

2.the screen output:
Step 1
Step 2
Step 3
get data: a
get data: b
get data: c
Step 4

3. the connection is PERSISTENT so I kill it in db server by myself

4. run the script again, the result is like this.
Step 1
Step 2

Message : SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL
server during query
File : D:\AppServ\www\report\92\testing.php
Line : 25



The problem is, the connection was killed so it return 2013 Lost
connection. I suppose the exception was throw after step 1 since the
connection was fail,but the real case is this exception was throw after
step 2 rather than step 1.

I have no ways to know the PERSISTENT connection is still alive or not


Previous Comments:
------------------------------------------------------------------------

[2007-06-04 08:30:55] tony2001@php.net

What exactly do I need to do in order to reproduce it?

------------------------------------------------------------------------

[2007-06-04 04:25:10] vtsupermok at gmail dot com

Description:
------------
I create a pdo object and set PDO::ATTR_PERSISTENT => true, after
serveral hours I run my programme again. Suppose the connection will be
timeout and then the program throw a PDOException. 

But the real situation is it said time out lost connection but it
wouldn't return a PDOException.

Reproduce code:
---------------
$db=array(
    'name'=>'abc',
    'host'=>'127.0.0.1',
    'port'=>'3306',
    'userID'=>'123',
    'userPwd'=>'123',
    'charset'=>'latin1',
    'encoding'=>'big-5'
);
 
try{
   
$database="mysql:host={$db['host']};port={$db['port']};dbname={$db['name']}";
    $pdo = new PDO($database, $db['userID'], $db['userPwd'],
array(PDO::ATTR_PERSISTENT => true));
} catch (PDOException $e) {
    throw $e;
}

Expected result:
----------------
throw a exception.

Actual result:
--------------
the mysql error is timeout lost connection but it would't throw a
exception


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=41579&edit=1


Thread (13 messages)

« previous php.bugs (#113334) next »