See the POD for DBD::SQLite which states:

You shouldn't (re)use a database handle you created...before you fork(). Otherwise, you might see a database corruption in the worst case. If you need to fork(), (re)open a database after you fork().

Your sample code seems to be doing exactly what the POD suggests shouldn't be done; sharing a database handle across forked children.

SQLite's locking relies on the operating system's flock. Sharing a database handle probably shares the underlying filehandles, and at least on my Linux systems calling for an exclusive flock on the same filehandle twice is perfectly OK.

The manpage for GNU/Linux flock indicates that calling flock more than one time on a file descriptor is ok, and an already locked file will convert an existing lock to the new lock mode. In the case of your sample script this could be even worse, because your forked children are still alive while you enter the 2nd while(1) loop. Inside your 2nd while(1) you are doing SELECT's, which may be converting the database handle's LOCK_EX to a LOCK_SH. ...probably doesn't matter though; each child reusing the same handle is able to quietly obtain a lock, so the previous lock state is irrelevant -- they all stomp on each other without warning.

Instantiate a new database handle in each child, and the problem should go away.


Dave


In reply to Re^2: DBD::SQLite "file is encrypted or is not a database" in running application (repro code) by davido
in thread [SOLVED]: DBD::SQLite "file is encrypted or is not a database" in running application by stevieb

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.