Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using DBI module and connecting with my postgres/sql database, How can I check that the database connection is already exists or not ?

Replies are listed 'Best First'.
Re: Database connection checking.
by mje (Curate) on Feb 06, 2009 at 10:39 UTC

    Slightly depends on what you mean by exists. I would test the handle returned by DBI connect is defined then use the "ping" method documented in the DBI docs.

Re: Database connection checking.
by alanhaggai (Novice) on Feb 06, 2009 at 10:49 UTC

    From DBI's pod:

    Active (boolean, read-only)

    The Active attribute is true if the handle object is "active". This is rarely used in applications. The exact meaning of active is somewhat vague at the moment. For a database handle it typically means that the handle is connected to a database ($dbh->disconnect sets Active off). For a statement handle it typically means that the handle is a SELECT that may have more data to fetch. (Fetching all the data or calling $sth->finish sets Active off.)

    Example: $handle->{Active}

    Hope it helps.

Re: Database connection checking.
by linuxer (Curate) on Feb 06, 2009 at 10:49 UTC