Re: threads,dbi 1.30,odbc 0.43, perl 5.8 (AS)

2002-12-03 Thread Trey Gregory
Here is a short example. Short overview is: main connects, queries, then spawns thread 
(test_thread) which creates a new connection and attempts to query. The error received 
is:

thread failed to start: DBD::ODBC::dr connect failed: handle 1 is owned by thread 
15d6d7c not current thread 32c814c (handles can't be shared between threads and your 
driver may need a CLONE method ad
ded) at D:/perl/site/lib/DBI.pm line 503.


use threads;
use DBI;

my $dbh;
if (!($dbh = DBI->connect("dbi:ODBC:my_dsn",
  "loginpw",
  "loginpw",
  {PrintError => 1}
  ))) {
print "Can't connect:  $DBI::errstr" if $DEBUG;

exit;
}

my $sql = qq[
select * from account_t
];

my $result;

if (!($result = executeCommand($dbh,$sql))) {
print "error $sql\n";
}


my $thr = threads->new(\&test_thread);
$thr->join;

exit;


sub test_thread {

my $dbh;
if (!($dbh = DBI->connect("dbi:ODBC:my_dsn",
  "loginpw",
  "loginpw",
  {PrintError => 1}
  ))) {
print "Can't connect:  $DBI::errstr" if $DEBUG;

exit;
}

my $sql = qq[
select * from account_t
];

my $result;

if (!($result = executeCommand($dbh,$sql))) {
return undef;
}
}


sub executeCommand {
my ($dbh, $query_str) = @_;

my $sth;
if (!($sth = $dbh->prepare($query_str))) {
return undef;
}

my $rc;
if (!($rc = $sth->execute)) {
return undef;
}

my $resultsARef;
if (!($resultsARef = $sth->fetchall_arrayref)) {
return undef;
}

$sth->finish;
return $resultsARef;
}



threads,dbi 1.30,odbc 0.43, perl 5.8 (AS)

2002-12-03 Thread Trey Gregory
I'm sure this has been answered before, but I wasn't able to find anything in the 
archives. (Is there a search interface available?)

I have a multithreaded program. My problem is this: if I create a connection in the 
parent, then create a thread which creates a new dbh, I get an error message about the 
handle not being owned by the (current) child thread. Since I was creating a new 
connection in the child, I thought I would be creating a new handle as well, but this 
does not seem to be the case.

What is the proper way to work with DBI in this multithreaded environment?


Thanks,
Trey