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;
}