On 26/06/02, Tim Bunce ([EMAIL PROTECTED]) wrote:
> On Wed, Jun 26, 2002 at 02:19:56PM -0700, Michael A Chase wrote:
> > On Wed, 26 Jun 2002 21:42:55 +0100 Rory Campbell-Lange <[EMAIL PROTECTED]>
>wrote:
> >
> > > I have a script which I imagine will loop every 15 to 60 seconds.
> > >
> > > Should I make the database connection at the beginning of the script,
> > > and just keep reusing it, or would it be more prudent to remake the
> > > connection in the body of the loop?
> > >
> > > I'm connecting to postgresql on localhost (Linux).
> > Much more efficient to keep the connection. You will want to be sure to
> > check for failures caused by losing the connection and either quit or
> > recover somehow.
>
> Yeap. Something like:
> while (...) {
> eval {
> $dbh = DBI->connect_cached(..., { RaiseError => 1 });
> ... do the work here ...
> }
> }
> Note the connect_cached.
Thanks for your reply, Tim.
I'm following the instructions in the DBI pod by asking how
connect_cached usefully differs from the normal connect method. There
doesn't seem to be a reference to the cached method in your book.
i.e. how
$dbh = DBI->connect_cached($data_source, $username, $password)
or die $DBI::errstr;
differs from my existing little subroutine
sub db_connect {
$dbh = DBI->connect( $db_name, $db_user, $db_pass) or
error_output('Database connection error', "The error was\n$DBI::errstr");
}
Reading the pod carefully, I think I gather the following.
$dbh = DBI->connect_cached() retuns a hash with the connection params.
(the hash is stored in $dbh)
if $dbh is called again with the same params, the cached $dbh is
used instead.
Your eval block evals the hash??? to see if an error is raised against
the connection?
Doesn't that mean that the connect_cached connection needs to be made
before the eval block?
A final question is that my db_connect script is in fact in a database
module apart from the looping script, so I make a new database
connection when I invoke an object from the database.pm file. How can I
keep checking that the connection is still up in this scenario?
Apologies for the beginner's questions.
Cheers
Rory
--
Rory Campbell-Lange
<[EMAIL PROTECTED]>
<www.campbell-lange.net>