Re: Error: handles can't be shared between threads

[prev] [thread] [next] [Date index for 2005/05/20]

From: Perrin Harkins
Subject: Re: Error: handles can't be shared between threads
Date: 20:45 on 20 May 2005
On Friday 20 May 2005 4:27 pm, Octavian Rasnita wrote:
> In all the modules I have put something like:
>
> package Site::Module1;
>
> use strict;
> use Site::MySQL (); #The module that connects to MySQL
> my $dbh = Site::MySQL::dbh();

You are creating a closure when you use that $dbh in your subs.  This is very 
bad, for multiple reasons.  In addition to the problem you already see, this 
will prevent Apache::DBI from reconnecting if your MySQL connection times 
out.  It will also break the safety rollback handler which you would need if 
you ever decide to use transactions.

> What should I do?
>
> Put "use Site::MySQL" and "my $dbh = Site::MySQL::dbh()" inside of
> subroutines?

You only need the "use" once at the top, but you should put the call to get 
the dbh at the beginning of each sub.  Apache::DBI will intercept the connect 
calls and give you back a cached handle.

- Perrin

Error: handles can't be shared between threads
Octavian Rasnita 17:47 on 19 May 2005

Re: Error: handles can't be shared between threads
Perrin Harkins 20:48 on 19 May 2005

Re: Error: handles can't be shared between threads
Octavian Rasnita 01:32 on 20 May 2005

Re: Error: handles can't be shared between threads
Perrin Harkins 03:50 on 20 May 2005

Re: Error: handles can't be shared between threads
Octavian Rasnita 20:27 on 20 May 2005

Re: Error: handles can't be shared between threads
Perrin Harkins 20:45 on 20 May 2005

Generated at 15:53 on 25 May 2005 by mariachi v0.52