On Tue, 31 Jul 2007, Russ wrote:

> Over time the need to connect to the same database in different
> scripts became apparent.  To do this I began by creating a package
> that contained all the $dbh handles (not advisable) as if one fails
> then your whole script is no good.   So I then split them into
> individual packages for each connect handler.  Again not good if you
> need to connect to multiple databases in your script, but aren't
> dependent on all of them having to work.
> 
> So is there a good way to manage database connections only.  So that
> multiple scripts can use the same connection  but it is defined in the
> one place.  Should I explore DBI::Ingres.  or is there another easier
> way, other than defining the connection in each script I create.


If I understand right, you want to create you database handle ($dbh) and 
then have multiple scripts use that same $dbh, right?  If so, read on; if 
not, the talks linked below may still be useful.

Tim talked about caching database handles (and other things) and their 
gotchas in his Advanced DBI talk, which he posted to the list earlier.

Two things come to mind off the top of my head, both really based on Tim's 
talks, which I recommend that you peruse:

<http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI_AdvancedTalk_200708.pdf>
<http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/Gofer-200707.pdf>

One approach would be to have a parent script that creates the database 
handle and then manages all of the other scripts.  This assumes several 
things about what you are doing, and may not be practical to you.  I 
suppose you could launch a separate script that maintains the $dbh and 
allows the other scripts to communicate to it.

Another approach is that the scripts are called from Apache, then it seems
that it is possible to share the $dbh between processes.

I'm still looking at DBI::Gofer, and can't say with confidence that it 
would work for this situation.

John

Reply via email to