I don't recommend sharing the database handle like that. Think about it, if two sets of instructions are going through the same handle, which one gets to go first? Would they even be valid instructions?

One thing to note is, even if you don't share the same database connection between the parent and the child, you need to be careful of the self-clean up that the database drivers perform :

my $parent_dbh = DBI->connect( ... ); if( my $pid = fork() ) { # do stuff... # >>> $parent_dbh may be cleaned up here, # >>> because it *is* in the child's scope exit 0; }

You could do this:

# fork first if( my $pid = fork() ) { # do stuff, including opening new DBI connection exit 0; } my $parent_dbh = DBI->connect(...); # do parent stuff

or, if you *really* need to open the connection in the parent beforehand,

my $parent_dbh = DBI->connect(...); $parent_dbh->{ InactiveDestroy } = 0; if(my $pid = fork()) { # do stuff }

Incidentally, setting InactiveDestroy may actually fix your problem, but I still would STRONGLY advise you not to share the same handle between the parent and the child


In reply to Re: Access the same database in two processes by lestrrat
in thread Access the same database in two processes by sachaer

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.