in reply to Problem creating a singleton wrapper around DBI

As an alternative to caching the DBI connection yourself - get DBI to do it with connect_cached. This has the additional advantage of reconnecting if the old database handle has been disconnected or a ping fails.

sub new { # get necessary config info my $dbuser = conf::SiteConfig::get_conf ( 'dbuser' ); my $dbpass = conf::SiteConfig::get_conf ( 'dbpass' ); my $dbname = conf::SiteConfig::get_conf ( 'dbname' ); my $dbserv = conf::SiteConfig::get_conf ( 'dbserv' ); # Build up DBI connection string. my $datasource = 'dbi:mysql:'.$dbname; $datasource .= "\;host=$dbserv" if ( $dbserv ); # Connect return DBI->connect_cached( $datasource, $dbuser, $dbpass ); }

Replies are listed 'Best First'.
Re^2: Problem creating a singleton wrapper around DBI
by eric256 (Parson) on Aug 23, 2005 at 20:37 UTC

    I just wanted to second this. If your script does anything long term then this is a must. I've been bitten by randomly closing database handles on my singleton in the past and it was well worth the additional _cached to fix it. I beleive this also adds value in mod_perl environments as it will cache across requests which is a big ++.


    ___________
    Eric Hodges