Perrin, I am starting to feel guilty about bugging you so much, but you are the only person to have responded, and I watch the list enough to value your advice quite a bit.
>>sub new { >> my $invocant = shift; >> my $class = ref($invocant) || $invocant; >> > >That looks like voodoo code copied from a man page. If you call this as >Holds->new(), you don't need that junk about ref. (And most people >recommend against the "new Holds" syntax.) > >> my $self = { @_ }; >> bless ($self, $class); >> $dbh = db_connect(); >> > >You don't seem to need this. You aren't using the database handle for >anything in this sub and you aren't gaining anything by calling it here. I wanted the DBH to be global since just about every sub in Holds does a query of some sort. I guess it doesn't matter either way if I do the connect in the new() vs up top outside of a sub. What is the problem with the my $holdcheck = new Holds() type of syntax? I never read anything about that either way. > >>sub GetProcessed { >> >>my $self = shift; >> >> #### This has a bug, somtimes the cached query doesn't stick around. >> > >If you lose your database connection, Apache::DBI will reconnect. Any >prepared queries will be lost. You *must* prepare every time, but see >below... > >>sub db_connect { >> >>require DBI; >> > >You don't need that. You should have already loaded it in startup.pl. > >>my $dbname = 'CS'; >>my ($dbuser, $dbpasswd) = ('myuser', 'mypass'); >> > >Probably should be in a config file, rather than buried in here. > >>my $dbh = DBI->connect("DBI:mysql:$dbname", $dbuser, $dbpasswd) >> or die "can't connect: $DBI::errstr\n"; >> >> # we need these waiting for queries, so we are going to prepare them ahead of >> time, and yes >> # horror of horror they will be global. Sorry Mom I tried :( >> $processed_hnd = $dbh->prepare_cached("select ord_tpak_processed from orders >>where ord_num=?") or confess("can't get tpak processed"); >> $status_hnd = $dbh->prepare_cached("select is_hold_set,holdstate from >>holds where ord_num=?") or confess("can't get hold status"); >> #DBI->trace(2,"/usr/local/apache/htdocs/out.log"); >> return $dbh; >> > >Don't put those in globals. The prepare_cached call already stores them >for the life of your database connection. Apache::DBI will keep that >connection alive (in a global hash) as long as it can and reconnect if >the connection is lost. If the connection does get lost, the statement >handles in these globals will stop working. You do recreate them every >time since you call this sub every time, but you could lose the >connection between the time this sub is called and the time you use >these handles. > I did this, I was a little scared about calling $dbh->finish() but I did what you said, and yes life is good I don't notice a speed difference. >>4. I know the way I have done these db connects is sloppy. But I can't seem >>to find a better way. Could I make one db_connect sub,and inherite it all >>though my modules? >> > >Make one sub that returns a database handle and use it from everywhere. > Doesn't need to be inherited, you can just stick it in a module that >all the other modules call. I have no idea why I put off doing that for so long. But that is done now as well. > >Hope some of that was helpful, >Perrin > (250) 655 - 9513 (PST Time Zone) "Inquiry is fatal to certainty." -- Will Durant