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

[prev] [thread] [next] [Date index for 2005/09/26]

From: Matt S Trout
Subject: Re: [CDBI] Class:DBI::Loader, Net::Daemon, threads and... ewww... :)
Date: 22:52 on 26 Sep 2005
On Mon, Sep 26, 2005 at 07:21:07PM +0100, Mike Whitaker wrote:
> Here's one for you :)
> 
> Given a Net::Daemon daemon that basically talks to a DB, I'd  
> obviously enough like Class::DBI::Loader to do the donkey work of  
> reading in ****loads of tables just the once, not once per thread.
> 
> 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?

Simple. Immediately after the loader's done it's job, call

CDBIBase->db_Main->disconnect; # Where your tables are CDBIBase::Table

That'll disconnect the stored dbh. Provided nothing touches the db between
then and when you spawn the threads that do the work, the first db operation
each thread initiates will connect its own dbh and you should be fine.

This certainly works ok in a fork/exec environment, but there may be caveats
under threading that I'm not aware of. Give it a go and tell us how you get
on :)

        -- 
             Matt S Trout       Specialists in perl consulting, web development, and
  Technical Director    UNIX/Linux systems architecture and automation. Mail
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

Re: [CDBI] Class:DBI::Loader, Net::Daemon, threads and... ewww... :)
Matt S Trout 22:52 on 26 Sep 2005

Generated at 17:55 on 28 Sep 2005 by mariachi v0.52