CustomConfig keeps dropping connection?

Brian Taber btaber at DIVERSECG.COM
Mon May 23 21:35:16 IST 2005


    [ The following text is in the "iso-8859-1" character set. ]
    [ Your display is set for the "US-ASCII" character set.  ]
    [ Some characters may be displayed incorrectly. ]

I have a few custom actions that utilize mysql to retrive user prefs, andI
had them working on an older server no problem, but I have moved the same
scripts to a newer server and they won't maintain the mysql connection.
This is what I had before:

------------------------------------------------------------------------
sub InitSACustom_Score { &InitDB; }
sub EndSACustom_Score { &CloseDB; }
sub SACustom_Score {
  my($message) = @_;

  return unless $message;

  if(!$dbh->ping) {
   undef $dbh;
   MailScanner::Log::InfoLog("Database ping failure attempting to
re-connect");
   InitSACustom_Score();
  }

  if (!defined $dbh){
        MailScanner::Log::InfoLog("MailScanner Custom Database Unavailable");
        return "8";
  }

  return unless defined $dbh;

  foreach (@{$message->{to}}) {
          ($to, $domain)=split("\@", $_);
          if ($domain ne ''){
                $domains{"domain='$domain'"}=1;
          }
  }

  $where=join(" OR ", keys(%domains));

  my $sth = $dbh->prepare("SELECT AVG(value) AS score FROM custom_score
WHERE opt='score' AND ($where)");
  $sth->execute();

  my $ref = $sth->fetchrow_hashref();

  if ($ref->{'score'} eq "") {
          $score=8;
  }else{
          $score=$ref->{'score'};
  }
  return $score;
}


sub InitDB {
 return if defined $dbh;

 MailScanner::Log::InfoLog("Initializing database connection");

 $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
                     $db_user, $db_pass,
                     {PrintError => 0});

 if (!$dbh) {
  MailScanner::Log::WarnLog("Unable to initialize database connection:
%s", $DBI::errstr);
  return;
 }

 MailScanner::Log::InfoLog("Finished Initializing database connection");
}

sub CloseDB {
 return if defined $dbh;
 $dbh->disconnect;
 MailScanner::Log::InfoLog("Disconnected from the database");
}
------------------------------------------------------------------------


Now I had to do this to get MailScanner running temporarily:


------------------------------------------------------------------------


sub InitSACustom_Score {  }
sub EndSACustom_Score {  }
sub SACustom_Score {
my($dbh);
my($sth);

  my($message) = @_;

  # Don't bother trying to do an insert if  no message is passed-in
  return unless $message;

   if (!defined $dbh){
        $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
                                                 $db_user, $db_pass,
                                                 {PrintError => 0});
   }

  # Check to make sure DB connection is still valid
  if(!$dbh->ping) {
   undef $dbh;
   MailScanner::Log::InfoLog("Database ping failure");
  }

  # Stop processing here if the connection to the database is down

  if (!defined $dbh){
        MailScanner::Log::InfoLog("MailScanner Custom Database Unavailable");
        return "8";
  }

  return unless defined $dbh;

  foreach (@{$message->{to}}) {
          ($to, $domain)=split("\@", $_);
          if ($domain ne ''){
                $domains{"domain='$domain'"}=1;
          }
  }

  $where=join(" OR ", keys(%domains));


  my $sth = $dbh->prepare("SELECT AVG(value) AS score FROM custom_score
WHERE opt='score' AND ($where)");
  $sth->execute();

  my $ref = $sth->fetchrow_hashref();

  if ($ref->{'score'} eq "") {
          $score=8;
  }else{
          $score=$ref->{'score'};
  }

  return $score;

}
------------------------------------------------------------------------


The above works, but I have at least 10 similar functions, and I dont want
to have all of them connected to the database...  Any suggestions?





-------------------------
Brian Taber
Manager/IT Specialist
Diverse Computer Group
Office: 508-758-4402
Cell: 508-496-9221
btaber at diversecg.com

------------------------ MailScanner list ------------------------
To unsubscribe, email jiscmail at jiscmail.ac.uk with the words:
'leave mailscanner' in the body of the email.
Before posting, read the Wiki (http://wiki.mailscanner.info/) and
the archives (http://www.jiscmail.ac.uk/lists/mailscanner.html).

Support MailScanner development - buy the book off the website!




More information about the MailScanner mailing list