--- DBI.xs	Fri Jun 14 15:11:32 2002
+++ /mnt/mond/Perl/msrc/DBI-1.28/DBI.xs	Thu Jul  4 08:45:34 2002
@@ -125,11 +125,15 @@
 #define MAX_LongReadLen 2147483647L
 #endif
 
+#ifdef USE_ITHREADS
+static char *dbi_build_opt = "-ithread";
+#else
 #ifdef DBI_USE_THREADS
 static char *dbi_build_opt = "-thread";
 #else
 static char *dbi_build_opt = "-nothread";
 #endif
+#endif
 
 /* 32 bit magic FNV-0 and FNV-1 prime */
 #define FNV_32_PRIME ((UV)0x01000193)
@@ -226,6 +230,9 @@
     DBIS->logfp	 = PerlIO_stderr();
     DBIS->neatsvpvlen = get_sv("DBI::neat_maxlen", GV_ADDMULTI);
     sv_setiv(DBIS->neatsvpvlen, 400);
+#ifdef USE_ITHREADS
+    DBIS->thr_user = PERL_GET_THX ;
+#endif
 
     /* publish address of dbistate so dynaloaded DBD's can find it	*/
     sv_setiv(get_sv(DBISTATE_PERLNAME,1), (IV)DBIS);
@@ -765,10 +772,12 @@
 		   |DBIcf_ACTIVE	/* drivers are 'Active' by default	*/
 		   |DBIcf_AutoCommit	/* advisory, driver must manage this	*/
 	);
+#ifndef USE_ITHREADS
 #ifdef DBI_USE_THREADS
 	Newz(1, DBIc_THR_COND(imp), 1, dbi_cond);
 	COND_INIT(DBIc_THR_COND(imp));
 #endif
+#endif
     } else {		
 	imp_xxh_t *parent_com = DBIh_COM(parent_h);
 	DBIc_PARENT_H(imp)    = (SV*)SvREFCNT_inc(parent_h); /* ensure it lives	*/
@@ -778,10 +787,17 @@
 	DBIc_THR_COND(imp)    = DBIc_THR_COND(parent_com);
 	++DBIc_KIDS(parent_com);
     }
+#ifdef USE_ITHREADS
+    DBIc_THR_USER(imp) = PERL_GET_THX ;
+    if (DBIS->debug >= 3)
+	PerlIO_printf(DBILOGFP,"    dbih_make_com set thread to %x (type=%d)\n",
+		DBIc_THR_USER(imp), DBIc_TYPE(imp));
+#else
     /* handles come into life with DBIc_THR_USER set to DBIc_THR_USER_NONE	*/
     /* because DBIc_THR_USER indicates which thread has 'entered the DBI'	*/
     /* on this handle and, at this point, none has.				*/
     DBIc_THR_USER(imp) = DBIc_THR_USER_NONE;
+#endif
 
     if (DBIc_TYPE(imp) == DBIt_ST) {
 	imp_sth_t *imp_sth = (imp_sth_t*)imp;
@@ -975,6 +991,7 @@
 {
     dPERINTERP;
     dTHR;
+    dTHX;
     int dump = FALSE;
     int auto_dump = (DBIS->debug >= 6);
 
@@ -988,6 +1005,19 @@
 	return;
     }
 
+
+#ifdef USE_ITHREADS
+	if (DBIc_THR_USER(imp_xxh) != my_perl) {
+		/* don't clear handle that belongs to another thread */
+		if (DBIS->debug >= 3) {
+		    PerlIO_printf(DBILOGFP,"ignore dbi_hclearcom because DBI handle (type=%d, %s) is not owned by current thread (is %x, should %x)\n", 
+		          DBIc_TYPE(imp_xxh), HvNAME(DBIc_IMP_STASH(imp_xxh)), DBIc_THR_USER(imp_xxh), my_perl) ;
+		    PerlIO_flush(DBILOGFP);
+		}    
+	    return ;
+	}
+#endif
+
     if (auto_dump)
 	dbih_dumpcom(imp_xxh,"dbih_clearcom", 0);
 
@@ -2217,6 +2247,34 @@
 	    }
 	}
 
+#ifdef USE_ITHREADS
+{
+	dTHX ;
+	PerlInterpreter * is_perl = DBIc_THR_USER(imp_xxh) ;
+
+	if (debug >= 6) {
+		PerlIO_printf(DBILOGFP,"****DISPATCH: DBI handle (type=%d) in call to %s::%s (is %x, should %x)\n", 
+		          DBIc_TYPE(imp_xxh), HvNAME(DBIc_IMP_STASH(imp_xxh)), meth_name, is_perl, my_perl) ;
+	}		          
+	if (is_perl != my_perl) {
+		if (is_DESTROY) {
+			if (debug) {
+			    PerlIO_printf(DBILOGFP,"ignore DESTROY because DBI handle (type=%d) is not owned by current thread in call to %s::%s (is %x, should %x)\n", 
+			          DBIc_TYPE(imp_xxh), HvNAME(DBIc_IMP_STASH(imp_xxh)), meth_name, is_perl, my_perl) ;
+			    PerlIO_flush(DBILOGFP);
+			}    
+		    XSRETURN(0); /* don't DESTROY handle, if it is not our's !*/
+		}
+		if (debug) {
+		    PerlIO_printf(DBILOGFP,"FATAL: DBI handle (type=%d) is not owned by current thread in call to %s::%s (is %x, should %x)\n", 
+		          DBIc_TYPE(imp_xxh), HvNAME(DBIc_IMP_STASH(imp_xxh)), meth_name, is_perl, my_perl) ;
+		    PerlIO_flush(DBILOGFP);
+		}    
+		croak ("FATAL: DBI handle (type=%d) is not owned by current thread in call to %s::%s (is %x, should %x)", 
+		          DBIc_TYPE(imp_xxh), HvNAME(DBIc_IMP_STASH(imp_xxh)), meth_name, is_perl, my_perl) ;
+	}
+}
+#else
 #ifdef DBI_USE_THREADS		/* only pay the cost with threaded perl	*/
 	DBI_LOCK;
 	while(DBIc_THR_USER(imp_xxh) != DBIc_THR_USER_NONE && DBIc_THR_USER(imp_xxh) != thr->tid) {
@@ -2236,6 +2294,7 @@
 	SAVEDESTRUCTOR(dbi_cond_signal, imp_xxh);  /* arrange later cond signal	*/
 	DBI_UNLOCK;
 #endif
+#endif
 
 	if (!imp_msv) {
 	    imp_msv = (SV*)gv_fetchmethod(DBIc_IMP_STASH(imp_xxh), meth_name);
@@ -2982,6 +3041,12 @@
 
 
 void
+_clone_dbis()
+    CODE:
+    dbi_bootinit();
+
+
+void
 _setup_handle(sv, imp_class, parent, imp_datasv)
     SV *	sv
     char *	imp_class
--- DBIXS.h	Thu Jun  6 00:39:54 2002
+++ /mnt/mond/Perl/msrc/DBI-1.28/DBIXS.h	Thu Jul  4 08:45:34 2002
@@ -99,7 +99,11 @@
 
     I32  kids;		/* count of db's for dr's, st's for db's etc	*/
     I32  active_kids;	/* kids which are currently DBIc_ACTIVE		*/
+#ifdef USE_ITHREADS
+    PerlInterpreter * thr_user;	/* thread id currently using the handle		*/
+#else
     U32  thr_user;	/* thread id currently using the handle		*/
+#endif
     dbistate_t *dbistate;
 } dbih_com_std_t;
 
@@ -387,7 +391,14 @@
 
     int         (*logmsg)	_((imp_xxh_t *imp_xxh, char *fmt, ...));
     int         (*set_err)	_((imp_xxh_t *imp_xxh, char *fmt, ...));
-    void *pad[7];
+
+#ifdef USE_ITHREADS
+    PerlInterpreter * thr_user;	/* thread id currently using the handle		*/
+#else
+    U32  thr_user;	/* thread id currently using the handle		*/
+#endif
+
+    void *pad[3];
 };
 
 /* macros for backwards compatibility */
--- DBI.pm	Fri Jun 14 15:11:36 2002
+++ /mnt/mond/Perl/msrc/DBI-1.28/DBI.pm	Thu Jul  4 08:45:34 2002
@@ -135,7 +135,7 @@
 {
 package DBI;
 
-my $Revision = substr(q$Revision: 11.16 $, 10);
+my $Revision = substr(q1.1, 10);
 
 use Carp;
 use DynaLoader ();
@@ -415,6 +415,14 @@
 }
 
 
+sub CLONE {
+	my $olddbis = $DBI::_dbistate ;
+	_clone_dbis() ; # clone the DBIS structure
+	%DBI::installed_drh = () ;	# clear loaded drivers so they have a chance to reinitialize
+    DBI->trace(@DBI::dbi_debug);
+	DBI->trace_msg("CONE DBI for new thread (" . sprintf ("dbis %x -> %x",$olddbis, $DBI::_dbistate) . ")\n");
+}
+	
 
 # --- The DBI->connect Front Door methods
 
