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

Re: Database Connections

by Fastolfe (Vicar)
on Jan 15, 2001 at 19:43 UTC ( [id://51933]=note: print w/replies, xml ) Need Help??


in reply to Database Connections

You might decide to do something like this:
package DBHandle; use DBI; my $dbh; sub new { $dbh ||= DBI->connect(...) or die "..."; return $dbh; } # and everywhere you need it: my $dbh = new DBHandle;
It might even be clearer to future maintainers of your code if you used 'get' instead of 'new'.

Replies are listed 'Best First'.
Re: Re: Database Connections
by ichimunki (Priest) on Jan 15, 2001 at 19:51 UTC
    I like this suggestion. Following it along, I'd suggest making sure to extend this notion to separating as much of your database connection logic into one package and your program logic into another. This approach is more flexible long-term (changing databases doesn't require dealing with code entangled in the program logic) and makes it easier to develop your code in the present (by not having to try and debug DB logic and program logic at the same time because they are intermingled).
Re: Re: Database Connections
by Anonymous Monk on Jan 15, 2001 at 20:04 UTC
    My question about the above approach is that I would still be creating a db connection everytime I need to use the db, right? In other words, if for one script instantiation I call two sub routines, test1 and test2 and they both use the db then they both will be creating two separate db connections, right?
    test1(); test2(); sub test1 { $dbh=new MyDBLayer; $dbh->do(etc..); } sub test2 { $dbh=new MyDBLayer; $dbh->do(etc..); }
    It is best to minimize the number of times you have to make a db connection right? If I do it this way though, I only make one db connection but use it in two subroutines.
    $dbh=new db connection; test1($dbh); test2($dbh); sub test1 { $dbh->do(etc..); } sub test2 { $dbh->do(etc..); }
    Is it wrong to do it this way?
    Thanks.
      No, they'll be returning the same $dbh. The use of the ||= operator is equivalent to $dbh = $dbh || DBI->connect(...);. Thus, $dbh will be connected the first time (since it hasn't been defined yet), but will simply be re-used each additional time the function is called.
      Actually, Fastolfe's suggestion allows you to only create a single DB connection for the life of the script.

      The first time new MyDBLayer is called, the variable $dbh will be undef, so a database connection will be created. The second time new MyDBLayer is called, $dbh will already hold a database object; that object will be returned, instead of a new connection being made.

      The key is the use of the ||= operator; a new database connection will be made only if $dbh doesn't already hold a database object.

      Personally, I've used the approach of passing a database connection into a subroutine, but I really like Fastolfe's approach.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-19 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found