Hi Monks! I need some help with threads, DBI and overall effectiveness of using mysql with perl. I'm just finding out that perl DBI might not be thread safe, I often get errors logged that "$dbh not owned by X(pid), DESTROY Ignored" or something to that extent. My program is slowly but surely growing in size and makes heavy use of threads to process things in a timely manner. Here's sample code used to process requests as they occur.
use DBI; use threads qw(stringify); use threads::shared; use Time::HiRes qw (sleep); do("./sql.pl"); &sql_setup; $server_id = "A"; share($server_id); $sys_ok = 1; share($sys_ok); $|++; $thr1 = threads->new(\&starter); $thr1->detach; $thr2 = threads->new(\&stopper); $thr2->detach; $thr3 = threads->new(\&monitor); $thr3->detach; $thr4 = threads->new(\&processor); $thr4->detach;
As these treads are run, their subroutines pickup tasks to be processed then simply exit. My guess is while one $dbh is executing, another is called and when the first is finished, its no longer assigned to the same handle and thus crashes. Now here is the second part to my question... Is there a better way to use perl DBI when making multiple queries rather than constantly creating more than one $dbh? For example, when executing one query and data needs to be pulled from a second table, rather than creating $dbh2...? Here's some of the code contained in sql.pl used in the processing of queries.
sub row_sql(){ my $select = $_[0]; my @row2,$dbh2,$sth2; if ($select eq ""){print "Empty Select."; exit;} $dbh2=DBI->connect($connectionInfo,$user,$passwd) || print "DBI Conn +ection Failed!($DBI::errstr)"; $sth2=$dbh2->prepare($select); if (!($sth2->execute())) { } @row2=$sth2->fetchrow_array(); $sth2->finish; $dbh2->disconnect(); return (@row2); }
So to sum up two questions: 1. Is there a thread safe DBI or way to use DBI within multiple threads and processes? 2. Is there a better way to run a query inside a query without having to pre-define $dbh1 $dbh2 $dbh3? Thank you for your help and hope to hear from you soon.

In reply to Threads, MySQL & Ease of use by expresspotato

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.