--- Changes.orig	Tue Aug  9 14:08:41 2005
+++ Changes	Tue Aug  9 14:07:09 2005
@@ -1,5 +1,11 @@
 Revision history for ApacheDBI.
 
+patch from Joe Thomas September 2, 2004
+  - Move $Idx from a file-scoped variable to a connect() scoped
+    variable, which gets passed to other subroutines as needed.
+    This will ensure that the cleanup/rollback feature will work
+    properly when a script uses more than one database handle.
+
 0.99  08/03/2005
   - Turn off Debugging by default.
     Reported by <jonanderson@seren.com>
--- DBI.pm.orig	Tue Aug  9 13:48:00 2005
+++ DBI.pm	Tue Aug  9 14:29:16 2005
@@ -22,7 +22,6 @@
 my %Rollback;     # keeps track of pushed PerlCleanupHandler which can do a rollback after the request has finished
 my %PingTimeOut;  # stores the timeout values per data_source, a negative value de-activates ping, default = 0
 my %LastPingTime; # keeps track of last ping per data_source
-my $Idx;          # key of %Connected and %Rollback.
 
 
 # supposed to be called in a startup script.
@@ -76,7 +75,7 @@
     my $dsn    = "dbi:$drh->{Name}:$args[0]";
     my $prefix = "$$ Apache::DBI            ";
 
-    $Idx = join $;, $args[0], $args[1], $args[2];
+    my $Idx = join $;, $args[0], $args[1], $args[2]; # key of %Connected and %Rollback.
 
     # the hash-reference differs between calls even in the same
     # process, so de-reference the hash-reference 
@@ -114,15 +113,15 @@
     my $needCleanup = ($Idx =~ /AutoCommit[^\d]+0/) ? 1 : 0;
     if(!$Rollback{$Idx} and $needCleanup and Apache->can('push_handlers')) {
         print STDERR "$prefix push PerlCleanupHandler \n" if $Apache::DBI::DEBUG > 1;
-				if ($ENV{MOD_PERL_API_VERSION} == 2) {
-					require Apache2::ServerUtil;
-					my $s = Apache2::ServerUtil->server;
-					$s->push_handlers("PerlCleanupHandler", \&cleanup);
-				}
-				else {
-        	Apache->push_handlers("PerlCleanupHandler", \&cleanup);
-				}
-        # make sure, that the rollback is called only once for every 
+        if ($ENV{MOD_PERL_API_VERSION} == 2) {
+            require Apache2::ServerUtil;
+            my $s = Apache2::ServerUtil->server;
+            $s->push_handlers("PerlCleanupHandler", sub { cleanup($Idx) });
+        }
+        else {
+            Apache->push_handlers("PerlCleanupHandler", sub { cleanup($Idx) });
+        }
+        # make sure, that the rollback is called only once for every
         # request, even if the script calls connect more than once
         $Rollback{$Idx} = 1;
     }
@@ -180,6 +179,7 @@
 # Note: the PerlCleanupHandler runs after the response has been sent to the client
 
 sub cleanup {
+    my $Idx = shift;
     my $prefix = "$$ Apache::DBI            ";
     print STDERR "$prefix PerlCleanupHandler \n" if $Apache::DBI::DEBUG > 1;
     my $dbh = $Connected{$Idx};
