PEAR::DB disconnect closes all database connections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sven Jacobs

    PEAR::DB disconnect closes all database connections

    Dear newsgroup,

    I've upgraded to PEAR::DB 1.6.5 recently. Unfortunately now the database
    connection doesn't work as expected anymore :( The problems seems to be the
    method disconnect(), which now closes the database connections of _all_
    database objects. I ask myself if this is intentional or a bug. Below a
    code example:

    01 <?php
    02   error_reporting (E_ALL);
    03
    04   require_once('D B.php');
    05
    06   define('DB_TYPE ',     'mysql');
    07   define('DB_HOST ',     'localhost');
    08   define('DB_PORT ',     3306);
    09   define('DB_USER NAME', 'xxx');
    10   define('DB_PASS WORD', 'yyy');
    11   define('DB_DATA BASE', 'zzz');
    12
    13   function db_open()
    14   {
    15     $dsn = DB_TYPE . '://' . DB_USERNAME . ':' . DB_PASSWORD . '@' .
    16     DB_HOST . ':' . DB_PORT . '/' . DB_DATABASE;
    17     $db  = DB::connect($ds n);
    18     return $db;
    19   }
    20
    21   function a()
    22   {
    23     $db = db_open();
    24
    25     b();
    26
    27     echo 'query in a()<br />';
    28     $test = $db->getAll('sele ct * from abc');
    29     $db->disconnect() ;
    30
    31     if (DB::isError($t est))
    32       echo $test->getMessage() . '<br /><br />' . $test->getDebugInfo() ;
    33   }
    34
    35   function b()
    36   {
    37     $db = db_open();
    38
    39     echo 'query in b()<br />';
    40     $test = $db->getAll('sele ct * from abc');
    41     $db->disconnect() ;
    42
    43     if (DB::isError($t est))
    44       echo $test->getMessage() . '<br /><br />' . $test->getDebugInfo() ;
    45   }
    46
    47   a();
    48 ?>

    In function a() function b() is called which creates a local database
    object, does some stuff on the database and then closes its connection.
    That worked up to now. Since upgrading PEAR::DB it seems like the
    disconnect() of the database object from b() also disconnects the object
    from a() because I get the error "DB Error: no database selected" in row 28
    of the script. If I comment out the call of b() at row 25 everything works
    as expected. I was able to reproduce this behavior with the PHP version
    4.3.8 and 5.0.1. Why does a method of a local object interfere with another
    local object?!

    --
    Sincerely
    Sven Jacobs
  • Nikolai Chuvakhin

    #2
    Re: PEAR::DB disconnect closes all database connections

    Sven Jacobs <sven.jacobs@we b.de> wrote in message
    news:<cgnhh9$e1 f$00$1@news.t-online.com>...[color=blue]
    >
    > I've upgraded to PEAR::DB 1.6.5 recently. Unfortunately now the database
    > connection doesn't work as expected anymore :( The problems seems to be the
    > method disconnect(), which now closes the database connections of _all_
    > database objects.[/color]
    ....[color=blue]
    > Why does a method of a local object interfere with another local object?![/color]

    It has NOTHING to do with objects and everything to do with underlying
    database interface:

    If a second call is made to mysql_connect() with the same arguments,
    no new link will be established, but instead, the link identifier
    of the already opened link will be returned. The new_link parameter
    modifies this behavior and makes mysql_connect() always open a new
    link, even if mysql_connect() was called before with the same
    parameters.

    Note: The new_link parameter became available in PHP 4.2.0



    Now let's look at your code (compressed to highlight the point):

    21 function a()
    22 {
    23 $db = db_open();
    25 b();
    29 $db->disconnect() ;
    33 }
    35 function b()
    36 {
    37 $db = db_open();
    41 $db->disconnect() ;
    45 }
    47 a();

    When function a() calls function b(), b() DOES NOT ESTABLISH A NEW
    LINK TO THE DATABASE, but, rather, uses the one established by a().
    So when b() closes the link, it closes the link established by a(),
    and a() is no longer able to use it.

    Cheers,
    NC

    Comment

    • Sven Jacobs

      #3
      Re: PEAR::DB disconnect closes all database connections

      Nikolai Chuvakhin wrote:
      [color=blue]
      > When function a() calls function b(), b() DOES NOT ESTABLISH A NEW
      > LINK TO THE DATABASE, but, rather, uses the one established by a().
      > So when b() closes the link, it closes the link established by a(),
      > and a() is no longer able to use it.[/color]

      So why did the code work before upgrading PEAR::DB? :-/ Did they pass the
      new_link parameter and now removed it? Possible...

      Can I safely remove all $db->disconnect() 's from my code and PHP will
      disconnect from the db automaticly if the script execution ends?

      --
      Sincerely
      Sven Jacobs

      Comment

      • Nikolai Chuvakhin

        #4
        Re: PEAR::DB disconnect closes all database connections

        Sven Jacobs <sven.jacobs@we b.de> wrote in message
        news:<cgnrbs$s2 9$01$1@news.t-online.com>...[color=blue]
        >
        > Can I safely remove all $db->disconnect() 's from my code and PHP will
        > disconnect from the db automaticly if the script execution ends?[/color]

        Yes, absolutely.

        Cheers,
        NC

        Comment

        Working...