Oddity when creating DB handles

[email protected] ("Nick Djurovich")
Newsgroups perl.ithreads
Message-ID <[email protected]>
Hi,

I have a test perl script that opens a connection to a db. I was just
playing with threads to see if I could get 10 workers each with
their own db connection.

The problem is that if just the threads instantiate a DB instance
its okay, if the main script instantiates a db handle beforehand,
it throws an error ;

"thread failed to start: DB::mysql::dr failed to connect: handle 1
owned by thread b08382"

The sleeps are there just to make sure each thread/main
doesn't exit.

----------------------------------------------------

use strict;

use Debug;
use threads;
use threads::shared;
use Database;

my $db;

# if i comment out the line below the script works ! :(
ConnectToDB( S_DB => \$db );

sub ConnectToDB
{
        my (%args) = @_;

        my ($s_db) = $args{S_DB};

        my ($dbg) = new Debug(
                              DEBUG_IS_OFF => 1
                             );


        $$s_db = new Database(
                              DRIVER => 'mysql',
                              DEBUG => $dbg
                             );

        if (!$$s_db->ConnectToDB(
                                 SERVER      => '*',
                                 PORT        => '*',
                                 USER        => '*',
                                 PASSWORD    => '*',
                                 DATABASE    => '*',
                                 TRACE_LEVEL => '0',
                                )
           )
        {
                print "Cannot connect to database\n";

        }

}

sub ClientWorker
{
        my $number = shift;

        my ($db);

        ConnectToDB( S_DB => \$db );

        print "Thread $number just connected to db ($db) handle
($db->{DBH}))\n";

        sleep 20;

}

foreach my $number (1..5)
{
        my $thr = threads->new( \&ClientWorker, $number );
}

while (1)
{
        sleep 5;
}

----------------------------------------------------

Regards

Nick
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.