Assuming you're actually using threads, be advised that threading with the DBI is a bit hit or miss. It requires (a) a thread-safe database driver and (b) a threads-aware DBI driver.

I believe DBD::MySQL is one of the few (only ?) DBD's which have implemented the internal brain surgery needed to swap things between threads, and I think your app has to do some extra work to enable it.

Attempting to recover a database connection across a fork() (regardless the platform or API) can be a dangerous operation (as you've discovered), due to the complex interactions most database drivers have with their connection to the database. So, as ikegami pointed out, you'll need to create your connection within the child process. And in most instances, this also applies to Perl threads. Oh, BTW, on Windows, fork() is threads, but probably less stable than regular threads.

I'll toss in DBIx::Threaded as another option. It uses apartment threading to isolate the connection in its own thread, and distributes proxies to other threads for access. Essentially, its a poor man' Actor model. However, as it currently relies on threads::shared, performance in contentious systems is less than ideal.

As to other comments wrt threads here, I'll suggest that the more recent releases of threads and threads::shared permit reducing thread footprint, add add'l functionality, and improve stability (many thnx to jdhedden's efforts). However, ithreads still suffers from lengthy startup times and large memory footprints. I'll offer the following advice:

I'll offer Thread::Apartment as a means to accomplish the latter.

Perl Contrarian & SQL fanboy

In reply to Re: Multi-thread database script by renodino
in thread Multi-thread database script by nandn

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.