Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2011
    Posts
    2

    Unanswered: How to implement reconnect method

    Using perl 10 + lastest dbi + mysql, how do I implement a 'reconnect' method?
    (This is with mysql_auto_reconnect => 0, obviously.)
    I want to do
    ..
    # some code that might timeout the connection
    ..
    $dbh->ensure_connected;
    # some code that relies on being connected

    or
    ..
    # some code that might fiddle with connection settings (eg fk checking)
    ..
    $dbh->reconnect;
    # some code that relies on being connected

    (If you have a working 'reconnect' then 'ensure_connected' is trivial.)

    The problem is I don't know how to replace the _content_ of $dbh with the
    value of a replacement dbh. At the moment I'm trying to get by with
    $dbh = $dbh->reconnect;

    which works to a degree, but is no use when inside a (third-party) module because it would rely on the caller of that module also using that clumsy construct on the outside.

    If dbh was a simple hash- or array-based object, the solution becomes easy, but it isn't.
    The fact that DBD::Mysql is able to implement 'mysql_auto_connect' gives me hope that there's a solution possible.
    -Nic

  2. #2
    Join Date
    Jun 2011
    Posts
    2

    Easy :- pass by ref

    Bah! It's all easy and not specific to DBI. It's so long since I've taken advantage of perl's pass-by-reference, I had forgotten it was an option.
    So to pass the caller a replacement dbh, I just need to update $_[0].

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •