The problem space: I'm creating a web-logging system that records hits to one Apache server to an Oracle database.
There are a number of virtual hosts (47 at last count) and I want to log hits to specific locations on each virtual host.
So it seems I have this kind of hierarchy: Vhost -> location.
Now I've got one script that's going to yank the hits out of the logfile and insert them into the DB. The Vhost and Location objects are going to need access to the DB, but I don't want to create a new db handle for each instance of a location (there are going to be hundreds in all).
Each hit is logged as being to a specific location (file or directory), so each unique location needs a unique identifier. however, locations of the same name may occur on different virtual host, so each location needs to be tied to a particular virtual host. For reporting purposes, all this stuff needs labels (virtual hosts have names, the locations have labels).
so here's one way of writing the script that uses these objects:
What I'm left with are two arrays: one of location objects, and one of virtual host objects. But clearly, both the Vhost module and the Location module need to access the db. I want to use the same handle all the way through.use strict; use DBI; my $dbh = DBI->connect(blah blah blah) or die "blah"; use Vhost; use Vhost::Location; my @vhosts; # the constructor takes the ID (the primary key) # of the virtual host, queries the DB # and returns all the # properties of the host (name, etc.) foreach (#list of identifiers from DB) { push (@vhosts, new Vhost($_); } my @locations; foreach my $vhost(@vhosts) { my @location_ids = $vhost->get_location_ids(); foreach @location_ids) { # here, as above, the constructor takes the primary key # of the object in the DB, and returns the other properties push @locations, new Vhost::Location($_); } }
Thanks for any input!
Philosophy can be made out of anything -- or less
In reply to Sharing a database handle among objects by arturo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |