Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Modules and DBI

by Revelation (Deacon)
on Nov 10, 2002 at 20:55 UTC ( [id://211794]=note: print w/replies, xml ) Need Help??


in reply to Modules and DBI

If you're asking 'how can I pass around a database handle, so I don't have to generate it repeatedly,' there are a couple ways:

The simplest of them would be to create a singleton class, which holds the database handle like so:
package MyStateManagement::DBI::Singleton; use base qw(DBI); use DBI; my $Dbh = undef; sub connect { my $class = shift; $Dbh ||= DBI->connect( @_ ); return $Dbh; } 1;
And connect by doing  MyStateManagement::DBI::Singleton->connect(); the same way you would connect with DBI. Assuming you use one database handle for the whole package, subsequent connects will use the same connection, and will not regenerate the connect; however, you could change the singleton class to check to see if the connect string was the same, and create some sort of connect hash, if there were multiple connections (or you could just specify the connection by a number, or something of that sort.)

The alternative would be to just pass around your database handle through the subroutines.

Gyan Kapur
gyan.kapur@rhhllp.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://211794]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found