2ge has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm programming one web application, and there are some Perl issues. I am on FreeBSD, using mod_perl 2.0 and my scripts looks like:
main_script: ============ use strict; use warnings; ... ... use lib '/home/hosting/foo.bar/public_html/libs/perl'; use OpenSubtitles::Help; ... ( $i->{FileName} ) = $dbh->selectrow_array(qq{ select SubFileName from subs_subtitle_file a left join subs_subtitle b on a.SubtitleID = b.IDSubtitle where IDSubtitleFile = ? }, {}, $i->{IDSubtitleFile}); module_script: ============== package OpenSubtitles::Help; use strict; use warnings; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; use DBI; $VERSION = 1.00; @ISA = ('Exporter'); @EXPORT = qw( $dbh get_user ); #db connect our $dbh = DBI->connect("DBI:mysql:database=sub;host=localhost;port=33 +06", "foo", "bar", { AutoCommit => 1, RaiseError => 1, }) or die "Can't connect: ", $DBI::errstr; ... 1;
Symptomps: the worst thing on this is, sometimes script works OK, as I expect. But sometimes I get:
1.error: [Fri Mar 3 19:46:19 2006] -e: DBD::mysql::db selectrow_array failed: MySQL server has gone away at ... 2.error: [Fri Mar 03 19:43:11 2006] [error] Global symbol "$dbh" requires expli +cit package name at ...
I really dont know whats going here. I checked mysql uptime, it is OK, it is not restarting.

Please help me guys if you can

Replies are listed 'Best First'.
Re: mysql server has gone away & strange behaviour
by rnahi (Curate) on Mar 04, 2006 at 10:47 UTC
Re: mysql server has gone away & strange behaviour
by perrin (Chancellor) on Mar 04, 2006 at 15:38 UTC
    Are you using Apache::DBI?
      thanks for answers. Not, I am not using Apache::DBI. Also my mod_perl scripts has strange behaviors, for example:

      .. die "a"; .. die "b"; ...
      sometimes script dies on die "a" (always should!), and sometimes on die "b". I really dont know whats going on here

      UPDATE: It seems to be caching issue. I have to turn this off while developing, where set this plz ?
        It's not exactly a caching issue. Your global variable stays around, since you are running in a persistent environment. After some inactivity, the database disconnects the idle connection. Then your next attempt to use that connection fails. Apache::DBI will fix this for you. You just need to call DBI->connect every time instead of only the first time when your "use lib" is executed. Make that library into a proper Perl5 module with a package declaration, and add a sub to it that makes the DBI->connect call and returns the handle. Apache::DBI will handle caching that connection so that it doesn't have to reconnect if the handle is still active.