Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How can I avoid creating two instances of the same object

by monkey_boy (Curate)
on Mar 02, 2006 at 09:45 UTC ( #533835=note: print w/ replies, xml ) Need Help??


in reply to How can I avoid creating two instances of the same object

There is a specific solution for your problem within the DBI module itself, you need to replace your connect with connect_cached, then multiple connects to the same db will just return the same db-handle

$dbh = DBI->connect_cached($data_source, $username, $password);



This is not a Signature...


Comment on Re: How can I avoid creating two instances of the same object
Download Code
Re^2: How can I avoid creating two instances of the same object
by beachguy82 (Novice) on Mar 02, 2006 at 12:09 UTC
    I have tried that as well. That works as far as limiting one $dbh but I still have to call new for every table I create. Some of the scripts may access over 30 tables. That would mean that I would have to call new over 30 times and that is what I'm trying to avoid. What is the best method for only having to create one object then use that object over without having to call new each time I access a new table.
      What is the best method for only having to create one object then use that object over without having to call new each time I access a new table.

      Pass the $dbh to each new()?

      You might also want to look at dependency injection techniques, see IOC for one way to go about them.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-09-20 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (157 votes), past polls