Hello,

I'm writing an application using the DBI module and want to achieve the following: (while typing this part, I thought that it would be better to just post code first :))

use DBI; use POE; my $dbh = DBI->connect('dbi:mysql:****', '****', '****', {RaiseError = +> 1}); POE::Session->create(inline_states => {_start => \&tester}, heap => + {dbh => $dbh->clone()}); POE::Session->create(inline_states => {_start => \&tester}, heap => + {dbh => $dbh->clone()}); POE::Kernel->run(); sub tester { my $heap = $_[HEAP]; for my $key (@{$heap->{dbh}->selectcol_arrayref("SELECT id, sleep( +0.1) FROM mytable WHERE 1")}) { print $key; } }
The intended result for 'mytable' having 5 ids is 1122334455, instead I see 1234512345, that is, when trying to access MySQL, even with different handlers, one session waits until another is over. Trying to use the original dbh (without clone-ing) yields the same result.

So I tried to use threads instead

my @threads; for my $i (1..2) { push @threads, threads->create(\&testsub, $dbh->clone()); } foreach my $thread (@threads) { $thread->join(); } sub testsub { for my $key (@{$_[0]->selectcol_arrayref("SELECT id, sleep(0.1) FROM + keywords WHERE 1")}) { print $key; } }
and got the following error Thread 1 terminated abnormally: DBD::mysql::db selectcol_arrayref failed: handle 2 is owned by thread 225054 not current thread 1e60c2c (handles can't be shared between threads and your driver may need a CLONE method added)

Of course, the straightforward solution is to read everything from the DB, and then send it to the subs as a parameter, but I'm highly reluctant to use this method, because 1) I don't know, which fields do the subs need 2) The subs were made to insert some data to database while working 3) the DB can be large, very large

Thanks for help with making some threads/sessions use one db handle simultaneosly, or, if not possible, any suggestions on the design of the application!


In reply to Simultaneous access to database by roddik

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.