EndCustomFunctions when the batch finish

Steve Freegard steve.freegard at fsl.com
Thu Mar 3 17:56:02 GMT 2011


Hi Alvaro,

On 03/03/11 13:50, Alvaro Marin wrote:
> The problem is that as I see in bin/Mailscanner, the End functions of
> the "plugins" are called (with
> MailScanner::Config::EndCustomFunctions()) only when the child has to be
> restarted by "restartevery" variable:
>
> (time>=$StartTime && time<$RestartTime && !$BayesRebuild)
>
> The default value for "Restart Every" is 7200, so the MySQL connection
> is kept open until that time without any use.
>
> So, Can I ad safely :
>
> # Close down all the user's custom functions
> MailScanner::Config::EndCustomFunctions();
>
> inside that "while" to be executed every time that a Batch finish to
> close the DB connection?
>

If you do that; your database handle will get destroyed and the next 
time DBPlug() is called it will fail to insert anything (and will return 
an error).  It would also mean you'd have to hack MailScanner every time 
you upgrade it.

If you want to connect/disconnect each time your code runs; then do 
everything in DBPlug() and not in the Init/End functions e.g.

sub InitDBPlug {
   # Nothing here...
}

sub DBPlug {
   my $dbh = DBI->connect ...
   ...
   $dbh->commit;
   $dbh->disconnect;
}

sub EndDBPlug {
   # Nothing here either...
}

You only want to do database connect/disconnect in the Init/End 
functions if you want to keep your database connection for the life of 
the child process; this is more efficient if you are doing a lot of 
database work as connect/disconnect is expensive if you are doing it a lot.

Hope that helps.

Kind Regards,
Steve.


More information about the MailScanner mailing list