[CDBI] Re: Class:DBI::Loader, Net::Daemon, threads and... ewww... :)

Edward J. Sabol sabol at alderaan.gsfc.nasa.gov
Mon Sep 26 20:35:20 BST 2005


> If I do that, and then hang the loader object off the Net::Daemon
> object such that each thread can fetch it, I get errors of the form:
>
> DBD::mysql::db FETCH failed: handle 2 is owned by thread 814c008 not
> current thread 9698b50 (handles can't be shared between threads and
> your driver may need a CLONE method added) at /usr/share/perl5/Ima/
> DBI.pm line 316, <GEN12> line 1.
>
> OK. Fair enough. I'm probably being naughty. But is there a way of
> preserving the Class::DBI::Loader constructed relationships and hooking
> a new DBI handle on the back end?

I don't use CDBI::Loader. I think it would be far less problematic to just
define the CDBI classes and relationships yourself.

Anyway, the problem you're experiencing is also shared by mod_perl
developers, so there's some overlap there with the problem and it has been
dealt with on the old CDBI mailing list a lot over the years. Basically, you
could try to somehow close the DBI handle after the classes and relationships
are declared by CDBI::Loader and before the threads are spawned. I don't know
how Net::Daemon works, so I don't know if that's possible or not. Since
Ima::DBI caches the DBI handle in a closure, your best bet is probably to
override db_Main() to create your DBI handle when you need it. That's covered
in the CDBI pod at

http://search.cpan.org/~tmtm/Class-DBI-v3.0.8/lib/Class/DBI.pm#Dynamic_Database_Connections_/_db_Main

Also, check out the following Wiki articles:

http://wiki.class-dbi.com/index.cgi?UsingWithModPerl
http://wiki.class-dbi.com/index.cgi?ReUseLegacyDBIHandle

Neither of those articles apply explicitly to your problem, but the solution
to your problem will be similar in concept....

Hope this helps,
Ed




More information about the ClassDBI mailing list