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

Hi

I have some rather big an complicated daemon, which forks and do some stuff.
Every forked process has it's own db connection, I call connect_cached() after forking.
But from time to time I can see in log file errors like that:

1) DBD driver has not implemented the AutoCommit attribute
2) DBD::mysql::db selectall_arrayref failed: fetch() without execute()

I'm loosing some data, because of that.
Can you explain me what I'm doing wrong?

My db connection looks like that:

my $dsn = "DBI:mysql:host=%s;database=%s;mysql_auto_reconnect=1;mysql_ +enable_utf8=1"; my $attr = { RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => 1, mysql_auto_reconnect => 1 } DBI->connect_cached( $dsn, $user, $pass, $attr)

DBI version 1.607
DBD::mysql version 4.006
perl - 5.8.8 built for i486-linux-gnu-thread-multi

Thanks!

Replies are listed 'Best First'.
Re: DBI and DBD::mysql errors
by kyle (Abbot) on Sep 11, 2008 at 18:14 UTC

    If you fork before making any connections, you should be fine.

    If you make a connection first and then fork, you probably have different children trying to share a single database connection. That's not going to work. What you may want to do is have the children clone the parent's database connection or use connect instead of connect_cached. Either way, the parent's database connection has to be disposed of properly in the child.

    $parent_dbh->{InactiveDestroy} = 1; undef $parent_dbh;

    There's an example in DBI, fork, and clone.

Re: DBI and DBD::mysql errors
by Fletch (Bishop) on Sep 11, 2008 at 17:36 UTC

    Erm, I believe connect_cached reuses an already open connection if one exists with the same connection parameters. Seeing as how database connection handles aren't something that play nice across forks that's probably not what you want to be doing. The child process really should be calling and creating its own handle fresh.

    Update: Unless you mean you're calling connect_cached after you've forked and you're not calling it in the parent, in which case UR DOIN IT RITE ALRDY and just ignore me . . .

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: DBI and DBD::mysql errors
by perrin (Chancellor) on Sep 11, 2008 at 18:08 UTC
    Make certain you aren't opening a connection in the parent. If you are, it will eventually time out and trigger a cleanup on the server side which may close one of your active connections.