in reply to Problem managing persistent database connections in modules
pacakge MyProject::DBH; use DBI; my $DBH; # lexical scope outside the constructor # makes it available to all calls to new(). sub get_dbh { if ref($DBH) eq 'DBI::db') { # dbh already created, so return it return $DBH; } else { # connect to the DB and return the handle # (assume $user, $pass and $dsn come from somewhere) $DBH = DBI->connect($dsn, $user, $pass); return $DBH; } }
Then, it's just a simple matter of every object calling MyProject::DBH->get_dbh; to get the same database handle.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Problem managing persistent database connections in modules
by mp (Deacon) on Jul 26, 2002 at 00:24 UTC | |
by mfriedman (Monk) on Jul 29, 2002 at 23:24 UTC |