I'm running into problems using Class::DBI with threads. My setup is very simple: first use Class::DBI to retrieve a set of objects from database, then for each such object, I'll start a new thread to process it. The following is a stripped-down version (just retrieve one object, and start one thread):
use strict; package MyClass; use base 'Class::DBI'; __PACKAGE__->set_db('Main','dbi:mysql:test', 'test','test'); __PACKAGE__->table('MYCLASS'); __PACKAGE__->columns(Primary=>qw/ID/); __PACKAGE__->columns(Others => qw/NAME/); package main; use threads; my $s = MyClass->retrieve(1); threads->new(\&test, $s)->join(); print "done\n"; exit 0; sub test{ my $o = shift; print $o->name,"\n"; }
I got the following errors:
thread failed to start: DBD::mysql::db FETCH failed: handle 2 is owned + by thread 22432c not current thread 1b1e014(handles can't be shared +between threads and your driver may need a CLONE method added) at C:/ +Perl/site/lib/Ima/DBI.pm line 316. Attempt to free non-existent shared string 'test~~test~~test~~AutoComm +it~~ChopBlanks~~FetchHashKeyName~~PrintError~~RaiseError~~RootClass~~ +ShowErrorStatement~~Taint~~Username~~dbi_connect_method~~1~~1~~NAME_l +c~~0~~1~~DBIx::ContextualFetch~~1~~1~~test~~connect_cached' during gl +obal destruction. Free to wrong pool 1b24430 not 222770 during global destruction.
The first error is instructive, but I'm not sure how to fix it, can one pass the objects retrieved via Class::DBI to other threads? by the way, line 316 of DBI.pm is the "unless" line in the following:
sub _mk_db_closure { my ($class, @connection) = @_; my $dbh; return sub { unless ($dbh && $dbh->FETCH('Active') && $dbh->ping) { $dbh = DBI->connect_cached(@connection); } return $dbh; }; }
I'm more at a loss to the second one, it seems to be some db connection string, but who's doing the destruction and garbage collection here? Thanks.

In reply to threading problem with Class::DBI by johnnywang

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.