I have a parent process that creates a DBI::mysql connection (held in $dbh) to database D, executes some SQL requests, forks some children, (each of which want to execute SQL requests to the same database), and then continues to execute more SQL. The children of course inherit $dbh, but I'm not having much luck from then on.

Someone suggested executing $dbh->{InactiveDestroy} = 1; before forking, to prevent the children's exits from clobbering the shared connection. But I get from Sharing a database handle over multiple processes (dated 2001) that you shouldn't expect a database handle to survive the fork.

So what approach should I take? It sounds like I should disconnect $dbh before I fork (so that no children inherit my connection); have the children each connect afresh; and, back in the parent, *reconnect* to D and continue running.

Or perhaps I should leave the connection open, issue $dbh->{InactiveDestroy} = 1; before any forking, and have the children simply create their own connections in $dbh_child. When they exit, the $dbh_child connection will be cleaned up, and the inherited $dbh connection will be untouched.

Thanks for any help!


In reply to Sharing a database connection across fork() by ibm1620

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.