in reply to Re^2: Threads and multiple DBI connections
in thread Threads and multiple DBI connections

The problem is that $dbh->dbix_threaded_start_prepare won't return the statement object handle, so $sth ends up being empty.

I agree that the documentation is very confusing.

The $dbh->dbix_threaded_start_* calls don't return handles, they return immediately with IDs.

You then have call $rc = $h->dbix_threaded_wait( $id ); to wait for the thing you started to complete.

So, to asynchonously prepare a statement, you'd do (something like; untested):

my $prepID = $dbh->dbix_threaded_start_prepare( $sql, ... ); ... ## you can do other stuff here ... my $sth = $dbh->dbix_threaded_wait($id);

But you don't need to use the asynchronous calls for everything. You could just use the normal dbi calls for some things.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^4: Threads and multiple DBI connections
by clone4 (Sexton) on Nov 01, 2010 at 20:22 UTC
    Oh didn't realize that DBIx is direct subclass, so I can use all the methods of DBI. Anyway I changed the script accordingly (using just one $dbh DBIx Threaded object), and instead of the async methods I use regular ones, but now the problem is that script isn't able make use of more then one connection, so after one thread is created and detached, the second throws fatal error, as it is unable 'prepare' the statement and return $sth object...
    my $dbh = DBIx::Threaded->connect("DBI:mysql:database=$database;host=$ +host", $user, $password); DBIx::Threaded->dbix_threaded_create_pool(10); my $sth = $dbh->prepare("SELECT id,domain,DATE(created) FROM domain_li +st"); $sth->execute(); my $counter = 0; while (my @row = $sth->fetchrow_array()) { my $id = $row[0]; my $domain = $row[1]; my $created = $row[2]; next unless $row[0..3]; my $thr = threads->create(mx_lookup,$id,$domain,$created,$dbh) unl +ess $counter >= $thread_no; $thr->detach && $counter++ if $thr; } $sth->finish(); sub mx_lookup { my $id = shift; my $domain = shift; my $date_c = shift; my @date_c = split /-/, $date_c; my $delta = Delta_Days(@date_c,@date); my %emails_in; my %emails_out; if ($delta <= 30) { my @mx = mx($domain); #add multiple mxs support here my $mx_count = @mx; my $mx = $mx[0]->exchange; #find the last change for the given domain my $sth = $dbh->prepare("select max(id),domain_id,changed_from +,changed_to,date from mx_history where domain_id=?"); #this is where +the script fails $sth->execute($id); my @row = $sth->fetchrow_array(); $sth->finish(); } }
    And $dbh->errstr is -->
    Thread 43 terminated abnormally: Error:Unexpected db_prepare request: +must be connected to do that.
    seems like that the $dbh handle isn't conected at all...

      Sorry, but I don't understand what is going on either.

      I can only assume from the error message that you are using the module wrong, but reading the pod, I'd probably be trying to us it the same way.

      I think you'll have to contact the modules author.

        Yeah will have to do, thanks for the effort anyway.