summaryrefslogtreecommitdiff
path: root/msdtc_enlist.cpp
blob: 41812b0a4d7556a82f4344318aaa38e1aa8f1fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
/* Module:			msdtc_enlist.cpp
 *
 * Description:
 *		This module contains routines related to
 *			the enlistment in MSDTC.
 *
 *-------
 */

#ifdef	_HANDLE_ENLIST_IN_DTC_

#undef	_MEMORY_DEBUG_
#ifndef	_WIN32_WINNT
#define	_WIN32_WINNT	0x0400
#endif	/* _WIN32_WINNT */

#define	WIN32_LEAN_AND_MEAN
#include <oleTx2xa.h>
#include <XOLEHLP.h>
/*#include <Txdtc.h>*/
#define	_PGDTC_FUNCS_IMPORT_
#include "connexp.h"

/*#define	_SLEEP_FOR_TEST_*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <process.h>
#include <map>
#ifndef	WIN32
#include <errno.h>
#endif /* WIN32 */

#include <sql.h>
#define	_MYLOG_FUNCS_IMPORT_
#include "mylog.h"
#define	_PGENLIST_FUNCS_IMPLEMENT_
#include "pgenlist.h"
#include "xalibname.h"

#ifdef WIN32
#ifndef snprintf
#define snprintf _snprintf
#endif /* snprintf */
#endif /* WIN32 */

/* Define a type for defining a constant string expression */
#ifndef CSTR
#define CSTR static const char * const
#endif /* CSTR */

EXTERN_C {
HINSTANCE s_hModule;               /* Saved module handle. */
}
/*      This is where the Driver Manager attaches to this Driver */
BOOL    WINAPI
DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
        switch (ul_reason_for_call)
        {
                case DLL_PROCESS_ATTACH:
                        s_hModule = (HINSTANCE) hInst;  /* Save for dialog boxes */
			break;
                case DLL_PROCESS_DETACH:
                        mylog("DETACHING pgenlist\n");
			break;
	}
	return TRUE;
}

/*
 *	A comment About locks used in this module
 *
 *	  the locks should be acquired with stronger to weaker order.
 *
 *	1:ELOCK -- the strongest per IAsyncPG object lock
 *	  When the *isolated* or *dtcconn* member of an IAsyncPG object
 *	  is changed, this lock should be held.
 *	  While an IAsyncPG object accesses a psqlodbc connection,
 *	  this lock should be held.
 *
 *	2:[CONN_CS] -- per psqlodbc connection lock
 *	  This lock would be held for a pretty long time while accessing
 *	  the psqlodbc connection assigned to an IAsyncPG object. You
 *	  can use the connection safely by holding a ELOCK for the
 *	  IAsyncPG object because the assignment is ensured to be
 *	  fixed while the ELOCK is held.
 *
 *	3:LIFELOCK -- a global lock to ensure the lives of IAsyncPG objects
 *	  While this lock is held, IAsyncPG objects would never die.
 *
 *	4:SLOCK -- the short term per IAsyncPG object lock
 *	  When any member of an IAsyncPG object is changed, this lock
 *	  should be held.
 */

// #define	_LOCK_DEBUG_
static class INIT_CRIT
{
public:
	CRITICAL_SECTION	life_cs; /* for asdum member of ConnectionClass */
	INIT_CRIT() {
		InitializeCriticalSection(&life_cs);
		}
	~INIT_CRIT() {
			DeleteCriticalSection(&life_cs);
			}
} init_crit;
#define	LIFELOCK_ACQUIRE EnterCriticalSection(&init_crit.life_cs)
#define	LIFELOCK_RELEASE LeaveCriticalSection(&init_crit.life_cs)

/*
 *	Some helper macros about connection handling.
 */
#define	CONN_CS_ACQUIRE(conn)	PgDtc_lock_cntrl((conn), TRUE, FALSE)
#define	TRY_CONN_CS_ACQUIRE(conn) PgDtc_lock_cntrl((conn), TRUE, TRUE)
#define	CONN_CS_RELEASE(conn)	PgDtc_lock_cntrl((conn), FALSE, FALSE)

#define	CONN_IS_IN_TRANS(conn)	PgDtc_get_property((conn), inTrans)


static const char *XidToText(const XID &xid, char *rtext)
{
	int	glen = xid.gtrid_length, blen = xid.bqual_length;
	int	i, j;

	for (i = 0, j = 0; i < glen; i++, j += 2)
		sprintf(rtext + j, "%02x", (unsigned char) xid.data[i]);
	strcat(rtext, "-"); j++;
	for (; i < glen + blen; i++, j += 2)
		sprintf(rtext + j, "%02x", (unsigned char) xid.data[i]);
	return rtext;
}

static LONG	g_cComponents = 0;
static LONG	g_cServerLocks = 0;

//
//	以下のITransactionResourceAsyncオブジェクトは任意のスレッドから
//	自由にアクセス可能なように実装する。各Requestの結果を返すために
//	使用するITransactionEnlistmentAsyncインターフェイスもそのように
//	実装されている(と思われる、下記参照)ので呼び出しにCOMのアパー
//	トメントを意識する(CoMarshalInterThreadInterfaceInStream/CoGetIn
//	terfaceAndReleaseStreamを使用する)必要はない。
//	このDLL内で使用するITransactionResourceAsyncとITransactionEnlist
//	mentAsyncのインターフェイスポインターは任意のスレッドから直接使用
//	することができる。
//

// OLE Transactions Standard
//
// OLE Transactions is the Microsoft interface standard for transaction
// management. Applications use OLE Transactions-compliant interfaces to
// initiate, commit, abort, and inquire about transactions. Resource
// managers use OLE Transactions-compliant interfaces to enlist in
// transactions, to propagate transactions to other resource managers,
// to propagate transactions from process to process or from system to
// system, and to participate in the two-phase commit protocol.
//
// The Microsoft DTC system implements most OLE Transactions-compliant
// objects, interfaces, and methods. Resource managers that wish to use
// OLE Transactions must implement some OLE Transactions-compliant objects,
// interfaces, and methods.
//
// The OLE Transactions specification is based on COM but it differs in the
// following respects:
//
// OLE Transactions objects cannot be created using the COM CoCreate APIs.
// References to OLE Transactions objects are always direct. Therefore,
// no proxies or stubs are created for inter-apartment, inter-process,
// or inter-node calls and OLE Transactions references cannot be marshaled
// using standard COM marshaling.
// All references to OLE Transactions objects and their sinks are completely
// free threaded and cannot rely upon COM concurrency control models.
// For example, you cannot pass a reference to an IResourceManagerSink
// interface on a single-threaded apartment and expect the callback to occur
// only on the same single-threaded apartment.

class	IAsyncPG : public ITransactionResourceAsync
{
private:
	IDtcToXaHelperSinglePipe	*helper;
	DWORD				RMCookie;
	void				*dtcconn;
	LONG				refcnt;
	CRITICAL_SECTION		as_spin; // to make this object Both
	CRITICAL_SECTION		as_exec; // to make this object Both
	XID				xid;
	bool				isolated;
	bool				prepared;
	bool				done;
	bool				abort;
	HANDLE				eThread[3];
	bool				eFin[3];
	bool				requestAccepted;
	HRESULT				prepare_result;
	HRESULT				commit_result;
#ifdef	_LOCK_DEBUG_
	int				spin_cnt;
	int				cs_cnt;
#endif /* _LOCK_DEBUG_ */

public:
	enum {
		PrepareExec = 0
		,CommitExec
		,AbortExec
		};

	ITransactionEnlistmentAsync	*enlist;

	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
	ULONG	STDMETHODCALLTYPE AddRef(void);
	ULONG	STDMETHODCALLTYPE Release(void);

	HRESULT STDMETHODCALLTYPE PrepareRequest(BOOL fRetaining,
				DWORD grfRM,
				BOOL fWantMoniker,
				BOOL fSinglePhase);
	HRESULT STDMETHODCALLTYPE CommitRequest(DWORD grfRM, XACTUOW * pNewUOW);
	HRESULT STDMETHODCALLTYPE AbortRequest(BOID * pboidReason,
				BOOL fRetaining,
				XACTUOW * pNewUOW);
	HRESULT STDMETHODCALLTYPE TMDown(void);

	IAsyncPG();
	void SetHelper(IDtcToXaHelperSinglePipe *pHelper, DWORD dwRMCookie) {helper = pHelper; RMCookie = dwRMCookie;}

	HRESULT RequestExec(DWORD type, HRESULT res);
	HRESULT ReleaseConnection(void);
	void SetConnection(void *sconn) {SLOCK_ACQUIRE(); dtcconn = sconn; SLOCK_RELEASE();}
	void SetXid(const XID *ixid) {SLOCK_ACQUIRE(); xid = *ixid; SLOCK_RELEASE();}
	void	*separateXAConn(bool spinAcquired, bool continueConnection);
	bool CloseThread(DWORD type);
private:
	~IAsyncPG();
	void SLOCK_ACQUIRE() {EnterCriticalSection(&as_spin);}
	void SLOCK_RELEASE() {LeaveCriticalSection(&as_spin);}
	void ELOCK_ACQUIRE() {EnterCriticalSection(&as_exec);}
	void ELOCK_RELEASE() {LeaveCriticalSection(&as_exec);}
	void	*getLockedXAConn(void);
	void	*generateXAConn(bool spinAcquired);
	void	*isolateXAConn(bool spinAcquired, bool continueConnection);
	void SetPrepareResult(HRESULT res) {SLOCK_ACQUIRE(); prepared = true; prepare_result = res; SLOCK_RELEASE();}
	void SetDone(HRESULT);
	void Wait_pThread(bool slock_hold);
	void Wait_cThread(bool slock_hold, bool once);
};


IAsyncPG::IAsyncPG(void) : helper(NULL), RMCookie(0), enlist(NULL), dtcconn(NULL), refcnt(1), isolated(false), done(false), abort(false), prepared(false), requestAccepted(false)
{
	InterlockedIncrement(&g_cComponents);
	InitializeCriticalSection(&as_spin);
	InitializeCriticalSection(&as_exec);
	eThread[0] = eThread[1] = eThread[2] = NULL;
	eFin[0] = eFin[1] = eFin[2] = false;
	memset(&xid, 0, sizeof(xid));
#ifdef	_LOCK_DEBUG_
	spin_cnt = 0;
	cs_cnt = 0;
#endif /* _LOCK_DEBUG_ */
}

//
//	invoked from *delete*.
//	When entered ELOCK -> LIFELOCK -> SLOCK are held
//	and they are released.
//
IAsyncPG::~IAsyncPG(void)
{
	void *fconn = NULL;

	if (dtcconn)
	{
		if (isolated)
			fconn = dtcconn;
		PgDtc_set_async(dtcconn, NULL);
		dtcconn = NULL;
	}
	SLOCK_RELEASE();
	LIFELOCK_RELEASE;
	if (fconn)
	{
		mylog("IAsyncPG Destructor is freeing the connection\n");
		PgDtc_free_connect(fconn);
	}
	DeleteCriticalSection(&as_spin);
	ELOCK_RELEASE();
	DeleteCriticalSection(&as_exec);
	InterlockedDecrement(&g_cComponents);
}
HRESULT STDMETHODCALLTYPE IAsyncPG::QueryInterface(REFIID riid, void ** ppvObject)
{
	mylog("%p QueryInterface called\n", this);
	if (riid == IID_IUnknown || riid == IID_ITransactionResourceAsync)
	{
		*ppvObject = this;
		AddRef();
		return S_OK;
	}
	*ppvObject = NULL;
	return E_NOINTERFACE;
}
//
//	acquire/releases SLOCK.
//
ULONG	STDMETHODCALLTYPE IAsyncPG::AddRef(void)
{
	mylog("%p->AddRef called\n", this);
	SLOCK_ACQUIRE();
	refcnt++;
	SLOCK_RELEASE();
	return refcnt;
}
//
//	acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
ULONG	STDMETHODCALLTYPE IAsyncPG::Release(void)
{
	mylog("%p->Release called refcnt=%d\n", this, refcnt);
	SLOCK_ACQUIRE();
	refcnt--;
	if (refcnt <= 0)
	{
		SLOCK_RELEASE();
		ELOCK_ACQUIRE();
		LIFELOCK_ACQUIRE;
		SLOCK_ACQUIRE();
		if (refcnt <=0)
		{
			const int refcnt_copy = refcnt;
			mylog("delete %p\n", this);
			delete this;
			return refcnt_copy;
		}
		else
		{
			SLOCK_RELEASE();
			LIFELOCK_RELEASE;
			ELOCK_RELEASE();
		}
	}
	else
		SLOCK_RELEASE();
	return refcnt;
}

//
//	Acquire/release SLOCK.
//
void IAsyncPG::Wait_pThread(bool slock_hold)
{
	mylog("Wait_pThread %d in\n", slock_hold);
	HANDLE	wThread;
	int	wait_idx = PrepareExec;
	DWORD	ret;

	if (!slock_hold)
		SLOCK_ACQUIRE();
	while (NULL != (wThread = eThread[wait_idx]) && !eFin[wait_idx])
	{
		SLOCK_RELEASE();
		ret = WaitForSingleObject(wThread, 2000);
		SLOCK_ACQUIRE();
		if (WAIT_TIMEOUT != ret)
			eFin[wait_idx] = true;
	}
	if (!slock_hold)
		SLOCK_RELEASE();
	mylog("Wait_pThread out\n");
}

//
//	Acquire/releases SLOCK.
//
void IAsyncPG::Wait_cThread(bool slock_hold, bool once)
{
	HANDLE	wThread;
	int	wait_idx;
	DWORD	ret;

	mylog("Wait_cThread %d,%d in\n", slock_hold, once);
	if (!slock_hold)
		SLOCK_ACQUIRE();
	if (NULL != eThread[CommitExec])
		wait_idx = CommitExec;
	else
		wait_idx = AbortExec;
	while (NULL != (wThread = eThread[wait_idx]) && !eFin[wait_idx])
	{
		SLOCK_RELEASE();
		ret = WaitForSingleObject(wThread, 2000);
		SLOCK_ACQUIRE();
		if (WAIT_TIMEOUT != ret)
			eFin[wait_idx] = true;
		else if (once)
			break;
	}
	if (!slock_hold)
		SLOCK_RELEASE();
	mylog("Wait_cThread out\n");
}

/* Processing Prepare/Commit Request */
typedef
struct RequestPara {
	DWORD	type;
	LPVOID	lpr;
	HRESULT	res;
} RequestPara;

//
//	Acquire/releases LIFELOCK -> SLOCK.
//	may acquire/release ELOCK.
//
void	IAsyncPG::SetDone(HRESULT res)
{
	LIFELOCK_ACQUIRE;
	SLOCK_ACQUIRE();
	done = true;
	if (E_FAIL == res ||
	    E_UNEXPECTED == res)
		abort = true;
	requestAccepted = true;
	commit_result = res;
	if (dtcconn)
	{
		PgDtc_set_async(dtcconn, NULL);
		if (isolated)
		{
			SLOCK_RELEASE();
			LIFELOCK_RELEASE;
			ELOCK_ACQUIRE();
			if (dtcconn)
			{
				mylog("Freeing isolated connection=%p\n", dtcconn);
				PgDtc_free_connect(dtcconn);
				SetConnection(NULL);
			}
			ELOCK_RELEASE();
		}
		else
		{
			dtcconn = NULL;
			SLOCK_RELEASE();
			LIFELOCK_RELEASE;
		}
	}
	else
	{
		SLOCK_RELEASE();
		LIFELOCK_RELEASE;
	}
}

//
//	Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void	*IAsyncPG::generateXAConn(bool spinAcquired)
{
mylog("generateXAConn isolated=%d dtcconn=%p\n", isolated, dtcconn);
	if (!spinAcquired)
		SLOCK_ACQUIRE();
	if (isolated || done)
	{
		SLOCK_RELEASE();
		return dtcconn;
	}
	SLOCK_RELEASE();
	ELOCK_ACQUIRE();
	LIFELOCK_ACQUIRE;
	SLOCK_ACQUIRE();
	if (dtcconn && !isolated && !done && prepared)
	{
		void	*sconn = dtcconn;

		dtcconn = PgDtc_isolate(sconn, useAnotherRoom);
		isolated = true;
		SLOCK_RELEASE();
		LIFELOCK_RELEASE;
		// PgDtc_connect(dtcconn); may be called in getLockedXAConn
	}
	else
	{
		SLOCK_RELEASE();
		LIFELOCK_RELEASE;
	}
	ELOCK_RELEASE();
	return dtcconn;
}

//
//	Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void	*IAsyncPG::isolateXAConn(bool spinAcquired, bool continueConnection)
{
	void *sconn;

mylog("isolateXAConn isolated=%d dtcconn=%p\n", isolated, dtcconn);
	if (!spinAcquired)
		SLOCK_ACQUIRE();
	if (isolated || done || NULL == dtcconn)
	{
		SLOCK_RELEASE();
		return dtcconn;
	}
	SLOCK_RELEASE();
	ELOCK_ACQUIRE();
	LIFELOCK_ACQUIRE;
	SLOCK_ACQUIRE();
	if (isolated || done || NULL == dtcconn)
	{
		SLOCK_RELEASE();
		LIFELOCK_RELEASE;
		ELOCK_RELEASE();
		return dtcconn;
	}
	sconn = dtcconn;

	dtcconn = PgDtc_isolate(sconn, continueConnection ? 0 : disposingConnection);

	isolated = true;
	SLOCK_RELEASE();
	LIFELOCK_RELEASE;
	if (continueConnection)
	{
		PgDtc_connect(sconn);
	}
	ELOCK_RELEASE();
	return dtcconn;
}

//
//	Acquire/releases [ELOCK -> LIFELOCK -> ] SLOCK.
//
void	*IAsyncPG::separateXAConn(bool spinAcquired, bool continueConnection)
{
	mylog("%s isolated=%d dtcconn=%p\n", __FUNCTION__, isolated, dtcconn);
	if (!spinAcquired)
		SLOCK_ACQUIRE();
	if (prepared)
		return generateXAConn(true);
	else
		return isolateXAConn(true, continueConnection);
}

//
//	[when entered]
//	ELOCK is held.
//
//	Acquire/releases SLOCK.
//	Try to acquire CONN_CS also.
//
//	[on exit]
//	ELOCK is kept held.
//	If the return connection != NULL
//		the CONN_CS lock for the connection is held.
//
void	*IAsyncPG::getLockedXAConn()
{
	SLOCK_ACQUIRE();
	while (!done && !isolated && NULL != dtcconn)
	{
		/*
		 * Note that COMMIT/ROLLBACK PREPARED command should be
		 * issued outside the transaction.
		 */
		if (!prepared || !CONN_IS_IN_TRANS(dtcconn))
		{
			if (TRY_CONN_CS_ACQUIRE(dtcconn))
			{
				if (prepared && CONN_IS_IN_TRANS(dtcconn))
				{
					CONN_CS_RELEASE(dtcconn);
				}
				else
					break;
			}
		}
		separateXAConn(true, true);
		SLOCK_ACQUIRE(); // SLOCK was released by separateXAConn()
	}
	SLOCK_RELEASE();
	if (isolated && NULL != dtcconn)
	{
		CONN_CS_ACQUIRE(dtcconn);
		if (!PgDtc_get_property(dtcconn, connected))
			PgDtc_connect(dtcconn);
	}
	return dtcconn;
}

//
//	Acquire/release ELOCK -> SLOCK.
//
HRESULT IAsyncPG::RequestExec(DWORD type, HRESULT res)
{
	HRESULT		ret;
	bool		bReleaseEnlist = false;
	void	*econn;
	char		pgxid[258];

	mylog("%p->RequestExec type=%d conn=%p\n", this, type, dtcconn);
	XidToText(xid, pgxid);
#ifdef	_SLEEP_FOR_TEST_
	/*Sleep(2000);*/
#endif	/* _SLEEP_FOR_TEST_ */
	ELOCK_ACQUIRE();
	switch (type)
	{
		case PrepareExec:
			if (done || NULL == dtcconn)
			{
				res = E_UNEXPECTED;
				break;
			}
			if (econn = getLockedXAConn(), NULL != econn)
			{
				PgDtc_set_property(econn, inprogress, (void *) 1);
				if (E_FAIL == res)
					PgDtc_one_phase_operation(econn, ABORT_GLOBAL_TRANSACTION);
				else if (XACT_S_SINGLEPHASE == res)
				{
					if (!PgDtc_one_phase_operation(econn, ONE_PHASE_COMMIT))
						res = E_FAIL;
				}
				else
				{
					if (!PgDtc_two_phase_operation(econn, PREPARE_TRANSACTION, pgxid))
						res = E_FAIL;
				}
				PgDtc_set_property(econn, inprogress, (void *) 0);
				CONN_CS_RELEASE(econn);
			}
			if (S_OK != res)
			{
				SetDone(res);
				bReleaseEnlist = true;
			}
			ret = enlist->PrepareRequestDone(res, NULL, NULL);
			SetPrepareResult(res);
			break;
		case CommitExec:
			Wait_pThread(false);
			if (E_FAIL != res)
			{
				econn = getLockedXAConn();
				if (econn)
				{
					PgDtc_set_property(econn, inprogress, (void *) 1);
					if (!PgDtc_two_phase_operation(econn, COMMIT_PREPARED, pgxid))
						res = E_FAIL;
					PgDtc_set_property(econn, inprogress, (void *) 0);
					CONN_CS_RELEASE(econn);
				}
			}
			SetDone(res);
			ret = enlist->CommitRequestDone(res);
			bReleaseEnlist = true;
			break;
		case AbortExec:
			Wait_pThread(false);
			if (prepared && !done)
			{
				econn = getLockedXAConn();
				if (econn)
				{
					PgDtc_set_property(econn, inprogress, (void *) 1);
					if (!PgDtc_two_phase_operation(econn, ROLLBACK_PREPARED, pgxid))
						res = E_FAIL;
					PgDtc_set_property(econn, inprogress, (void *) 0);
					CONN_CS_RELEASE(econn);
				}
			}
			SetDone(res);
			ret = enlist->AbortRequestDone(res);
			bReleaseEnlist = true;
			break;
		default:
			ret = -1;
	}
	if (bReleaseEnlist)
	{
		helper->ReleaseRMCookie(RMCookie, TRUE);
		enlist->Release();
	}
	ELOCK_RELEASE();
	mylog("%p->Done ret=%d\n", this, ret);
	return ret;
}

//
//	Acquire/releses SLOCK
//	 	or 	[ELOCK -> LIFELOCK -> ] SLOCK.
//
HRESULT IAsyncPG::ReleaseConnection(void)
{
	mylog("%p->ReleaseConnection\n", this);

	SLOCK_ACQUIRE();
	if (isolated || NULL == dtcconn)
	{
		SLOCK_RELEASE();
		return SQL_SUCCESS;
	}
	Wait_pThread(true);
	if (NULL != eThread[CommitExec] || NULL != eThread[AbortExec] || requestAccepted)
	{
		if (!done)
			Wait_cThread(true, true);
	}
	if (!isolated && !done && dtcconn && PgDtc_get_property(dtcconn, connected))
	{
		isolateXAConn(true, false);
	}
	else
		SLOCK_RELEASE();
	mylog("%p->ReleaseConnection exit\n", this);
	return SQL_SUCCESS;
}

EXTERN_C static unsigned WINAPI DtcRequestExec(LPVOID para);
EXTERN_C static void __cdecl ClosePrepareThread(LPVOID para);
EXTERN_C static void __cdecl CloseCommitThread(LPVOID para);
EXTERN_C static void __cdecl CloseAbortThread(LPVOID para);

//
//	Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::PrepareRequest(BOOL fRetaining, DWORD grfRM,
				BOOL fWantMoniker, BOOL fSinglePhase)
{
	HRESULT	ret, res;
	RequestPara	*reqp;
	const DWORD	reqtype = PrepareExec;

	mylog("%p PrepareRequest called grhRM=%d enl=%p\n", this, grfRM, enlist);
	SLOCK_ACQUIRE();
	if (dtcconn && 0 != PgDtc_get_property(dtcconn, errorNumber))
		res = ret = E_FAIL;
	else
	{
		ret = S_OK;
		if (fSinglePhase)
		{
			res = XACT_S_SINGLEPHASE;
			mylog("XACT is singlePhase\n");
		}
		else
			res = S_OK;
	}
	SLOCK_RELEASE();
	ELOCK_ACQUIRE();
#ifdef	_SLEEP_FOR_TEST_
	Sleep(2000);
#endif	/* _SLEEP_FOR_TEST_ */
	reqp = new RequestPara;
	reqp->type = reqtype;
	reqp->lpr = (LPVOID) this;
	reqp->res = res;
#define	DONT_CALL_RETURN_FROM_HERE ???
	AddRef();
	HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
	if (NULL == hThread)
	{
		delete(reqp);
		ret = E_FAIL;
	}
	else
	{
		SLOCK_ACQUIRE();
		eThread[reqtype] = hThread;
		SLOCK_RELEASE();
		/*
		 * We call here _beginthread not _beginthreadex
		 * so as not to call CloseHandle() to clean up
		 * the thread.
		 */
		_beginthread(ClosePrepareThread, 0, (void *) this);
	}
	ELOCK_RELEASE();
	Release();
#undef	return
	return ret;
}
//
//	Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::CommitRequest(DWORD grfRM, XACTUOW * pNewUOW)
{
	HRESULT		res = S_OK, ret = S_OK;
	RequestPara	*reqp;
	const DWORD	reqtype = CommitExec;

	mylog("%p CommitRequest called grfRM=%d enl=%p\n", this, grfRM, enlist);

	SLOCK_ACQUIRE();
	if (!prepared || done)
		ret = E_UNEXPECTED;
	else if (S_OK != prepare_result)
		ret = E_UNEXPECTED;
	SLOCK_RELEASE();
	if (S_OK != ret)
		return ret;
#define	DONT_CALL_RETURN_FROM_HERE ???
	AddRef();
	ELOCK_ACQUIRE();
#ifdef	_SLEEP_FOR_TEST_
	Sleep(1000);
#endif	/* _SLEEP_FOR_TEST_ */
	reqp = new RequestPara;
	reqp->type = reqtype;
	reqp->lpr = (LPVOID) this;
	reqp->res = res;
	enlist->AddRef();
	HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
	if (NULL == hThread)
	{
		delete(reqp);
		enlist->Release();
		ret = E_FAIL;
	}
	else
	{
		SLOCK_ACQUIRE();
		eThread[reqtype] = hThread;
		SLOCK_RELEASE();
		/*
		 * We call here _beginthread not _beginthreadex
		 * so as not to call CloseHandle() to clean up
		 * the thread.
		 */
		_beginthread(CloseCommitThread, 0, (void *) this);
	}
	mylog("CommitRequest ret=%d\n", ret);
	requestAccepted = true;
	ELOCK_RELEASE();
	Release();
#undef	return
	return ret;
}
//
//	Acquire/release [ELOCK -> ] SLOCK.
//
HRESULT STDMETHODCALLTYPE IAsyncPG::AbortRequest(BOID * pboidReason, BOOL fRetaining,
				XACTUOW * pNewUOW)
{
	HRESULT		res = S_OK, ret = S_OK;
	RequestPara	*reqp;
	const DWORD	reqtype = AbortExec;

	mylog("%p AbortRequest called\n", this);
	SLOCK_ACQUIRE();
	if (done)
		ret = E_UNEXPECTED;
	else if (prepared && S_OK != prepare_result)
		ret = E_UNEXPECTED;
	SLOCK_RELEASE();
	if (S_OK != ret)
		return ret;
#define	return	DONT_CALL_RETURN_FROM_HERE ???
	AddRef();
	ELOCK_ACQUIRE();
	if (!prepared && dtcconn)
	{
		PgDtc_set_property(dtcconn, inprogress, (void *) 1);
		PgDtc_one_phase_operation(dtcconn, ONE_PHASE_ROLLBACK);
		PgDtc_set_property(dtcconn, inprogress, (void *) 0);
	}
	reqp = new RequestPara;
	reqp->type = reqtype;
	reqp->lpr = (LPVOID) this;
	reqp->res = res;
	enlist->AddRef();
	HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, DtcRequestExec, reqp, 0, NULL);
	if (NULL == hThread)
	{
		delete(reqp);
		enlist->Release();
		ret = E_FAIL;
	}
	else
	{
		SLOCK_ACQUIRE();
		eThread[reqtype] = hThread;
		SLOCK_RELEASE();
		/*
		 * We call here _beginthread not _beginthreadex
		 * so as not to call CloseHandle() to clean up
		 * the thread.
		 */
		_beginthread(CloseAbortThread, 0, (void *) this);
	}
	mylog("AbortRequest ret=%d\n", ret);
	requestAccepted = true;
	ELOCK_RELEASE();
	Release();
#undef	return
	return	ret;
}
HRESULT STDMETHODCALLTYPE IAsyncPG::TMDown(void)
{
	mylog("%p TMDown called\n", this);
	return	S_OK;
}

bool IAsyncPG::CloseThread(DWORD type)
{
	CSTR		func = "CloseThread";
	HANDLE		th;
	DWORD		ret, excode = S_OK;
	bool		rls_async = false;

	mylog("%s for %p thread=%d\n", func, this, eThread[type]);
	if (th = eThread[type], NULL == th || eFin[type])
		return false;
	ret = WaitForSingleObject(th, INFINITE);
	if (WAIT_OBJECT_0 == ret)
	{
		switch (type)
		{
			case IAsyncPG::AbortExec:
			case IAsyncPG::CommitExec:
				rls_async = true;
				break;
			default:
				GetExitCodeThread(th, &excode);
				if (S_OK != excode)
					rls_async = true;
		}
		SLOCK_ACQUIRE();
		eThread[type] = NULL;
		eFin[type] = true;
		SLOCK_RELEASE();
		CloseHandle(th);
	}
	mylog("%s ret=%d\n", func, ret);
	return rls_async;
}

EXTERN_C static void __cdecl ClosePrepareThread(LPVOID para)
{
	CSTR		func = "ClosePrepareThread";
	IAsyncPG	*async = (IAsyncPG *) para;
	bool		release;

	mylog("%s for %p", func, async);
	if (release = async->CloseThread(IAsyncPG::PrepareExec), release)
		async->Release();
	mylog("%s release=%d\n", func, release);
}

EXTERN_C static void __cdecl CloseCommitThread(LPVOID para)
{
	CSTR		func = "CloseCommitThread";
	IAsyncPG	*async = (IAsyncPG *) para;
	bool		release;

	mylog("%s for %p", func, async);
	if (release = async->CloseThread(IAsyncPG::CommitExec), release)
		async->Release();
	mylog("%s release=%d\n", func, release);
}

EXTERN_C static void __cdecl CloseAbortThread(LPVOID para)
{
	CSTR		func = "CloseAbortThread";
	IAsyncPG	*async = (IAsyncPG *) para;
	bool		release;

	mylog("%s for %p", func, async);
	if (release = async->CloseThread(IAsyncPG::AbortExec), release)
		async->Release();
	mylog("%s release=%d\n", func, release);
}

EXTERN_C static unsigned WINAPI DtcRequestExec(LPVOID para)
{
	RequestPara	*reqp = (RequestPara *) para;
	DWORD		type = reqp->type;
	IAsyncPG *async = (IAsyncPG *) reqp->lpr;
	HRESULT	res = reqp->res, ret;

	mylog("DtcRequestExec type=%d", reqp->type);
	delete(reqp);
	ret = async->RequestExec(type, res);
	mylog(" Done ret=%d\n", ret);
	return ret;
}

CSTR	regKey = "SOFTWARE\\Microsoft\\MSDTC\\XADLL";

static int regkeyCheck(const char *xalibname, const char *xalibpath)
{
	int	retcode = 0;
	LONG	ret;
	HKEY	sKey;
	DWORD	rSize;

	ret = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, regKey, 0, KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_64KEY, &sKey);
	switch (ret)
	{
		case ERROR_SUCCESS:
			break;
		case ERROR_FILE_NOT_FOUND:
			ret = ::RegCreateKeyEx(HKEY_LOCAL_MACHINE, regKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sKey, NULL);
			mylog("%s:CreateKeyEx ret=%d\n", __FUNCTION__, ret);
			break;
		default:
			mylog("%s:OpenKeyEx ret=%d\n", __FUNCTION__, ret);
	}
	if (ERROR_SUCCESS != ret)
		return -1;
	else
	{
		char keyval[1024];

		rSize = sizeof(keyval);
		switch (ret = ::RegQueryValueEx(sKey, xalibname, NULL, NULL, (LPBYTE) keyval, &rSize))
		{
			case ERROR_SUCCESS:
				if (rSize > 0)
				{
					if (0 == _stricmp(keyval, xalibpath))
						break;
					mylog("%s:XADLL value %s is different from %s\n", __FUNCTION__, keyval, xalibpath);
					if (IsWow64())
					{
						mylog("%s:avoid RegSetValue operation from wow64 process\n", __FUNCTION__);
						break;
					}
				}
			case ERROR_FILE_NOT_FOUND:
				mylog("%s:Setting value %s\n", __FUNCTION__, xalibpath);
				ret = ::RegSetValueEx(sKey, xalibname, 0, REG_SZ, (CONST BYTE *) xalibpath, (DWORD) strlen(xalibpath) + 1);
				if (ERROR_SUCCESS == ret)
					retcode = 1;
				else
				{
					retcode = -1;
					mylog("%s:SetValueEx ret=%d\n", __FUNCTION__, ret);
				}
				break;
			default:
				retcode = -1;
				mylog("%s:QueryValueEx ret=%d\n", __FUNCTION__, ret);
				break;
		}
		::RegCloseKey(sKey);
	}
	return retcode;
}

RETCODE static EnlistInDtc_1pipe(void *conn, ITransaction *pTra, ITransactionDispenser *pDtc, int method)
{
	CSTR	func = "EnlistInDtc_1pipe";
	static	IDtcToXaHelperSinglePipe	*pHelper = NULL;
	ITransactionResourceAsync		*pRes = NULL;
	IAsyncPG				*asdum;
	HRESULT	res;
	DWORD	dwRMCookie;
	XID	xid;
	const char *xalibname = GetXaLibName();
	const char *xalibpath = GetXaLibPath();

	int	recovLvl;
	char	errmsg[256];
	char	reason[128];

	if (!pHelper)
	{
		res = pDtc->QueryInterface(IID_IDtcToXaHelperSinglePipe, (void **) &pHelper);
		if (res != S_OK || !pHelper)
		{
			mylog("DtcToXaHelperSinglePipe get error %d\n", res);
			pHelper = NULL;
			return SQL_ERROR;
		}
	}
	res = (NULL != (asdum = new IAsyncPG)) ? S_OK : E_FAIL;
	if (S_OK != res)
	{
		mylog("CoCreateInstance error %d\n", res);
		return SQL_ERROR;
	}

	recovLvl = PgDtc_is_recovery_available(conn, reason, sizeof(reason));
	switch (method)
	{
		case DTC_CHECK_BEFORE_LINK:
			if (0 == recovLvl)
			{
				snprintf(errmsg, sizeof(errmsg), "%s is unavailable in distributed transactions", reason);
				PgDtc_set_error(conn, errmsg, func);
				return SQL_ERROR;
			}
	}
/*mylog("dllname=%s dsn=%s\n", xalibname, conn->connInfo.dsn); res = 0;*/
	char	dtcname[1024];
	PgDtc_create_connect_string(conn, dtcname, sizeof(dtcname));

	bool	confirmedRegkey = false, confirmingLink = false, xarmerr = false;
	char	error_header[64];
	while (true)
	{
		res = pHelper->XARMCreate(dtcname, (char *) xalibname, &dwRMCookie);

		mylog("XARMcreate error code=%x (%d %d)\n", res, confirmedRegkey, confirmingLink);
		xarmerr = true;
		if (!confirmingLink)
			snprintf(error_header, sizeof(error_header), "XARMcreate error code=%x", res);
		switch (res)
		{
			case S_OK:
				if (confirmingLink)
				{
					switch (recovLvl)
					{
						case 0:
							snprintf(errmsg, sizeof(errmsg), "%s:%s is currently unavailable in distributed transactions", error_header, reason);
							break;
						case -1:
							snprintf(errmsg, sizeof(errmsg), "%s:Possibly you connect to the database whose authentication method is %s or ident", error_header, reason);
							break;
						case 1:
							snprintf(errmsg, sizeof(errmsg), "%s:Are you trying to connect to the database whose authentication method is ident?", error_header);
							break;
					}
				}
				else
					xarmerr = false;
				break;
			case XACT_E_XA_TX_DISABLED:
				snprintf(errmsg, sizeof(errmsg), "%s:Please enable XA transaction in MSDTC security configuration", error_header);
				break;
			case XACT_E_TMNOTAVAILABLE:
				snprintf(errmsg, sizeof(errmsg), "%s:Please start Distributed Transaction Coordinator service", error_header);
				break;
			case E_FAIL:
				if (!confirmedRegkey)
				{
					int retcode = regkeyCheck(xalibname, xalibpath);
					confirmedRegkey = true;
					if (retcode > 0)
						continue;
				}
				switch (method)
				{
					case DTC_CHECK_RM_CONNECTION:
						if (!confirmingLink)
						{
							confirmingLink = true;
							strcat(dtcname, ";" KEYWORD_DTC_CHECK "=0");
							continue;
						}
					default:
						snprintf(errmsg, sizeof(errmsg), "%s:Failed to link with DTC service. Please look at the log of Event Viewer etc.", error_header);
				}
				break;
			case XACT_E_CONNECTION_DOWN:
				snprintf(errmsg, sizeof(errmsg), "%s:Lost connection with DTC transaction manager\nMSDTC has some trouble?", error_header);
				break;
			default:
				snprintf(errmsg, sizeof(errmsg), "%s\n", error_header);
				break;
		}
		break;
	}
	if (xarmerr)
	{
		PgDtc_set_error(conn, errmsg, func);
		return SQL_ERROR;
	}

	res = pHelper->ConvertTridToXID((DWORD *) pTra, dwRMCookie, &xid);
	if (res != S_OK)
	{
		mylog("ConvertTridToXid error %d\n", res);
		return SQL_ERROR;
	}
{
char	pgxid[258];
XidToText(xid, pgxid);
mylog("ConvertTridToXID -> %s\n", pgxid);
}
	asdum->SetXid(&xid);
	/* Create an IAsyncPG instance by myself */
	/* DLLGetClassObject(GUID_IAsyncPG, IID_ITransactionResourceAsync, (void **) &asdum); */

	asdum->SetHelper(pHelper, dwRMCookie);
	res = pHelper->EnlistWithRM(dwRMCookie, pTra, asdum, &asdum->enlist);
	if (res != S_OK)
	{
		mylog("EnlistWithRM error %d\n", res);
		pHelper->ReleaseRMCookie(dwRMCookie, TRUE);
		return SQL_ERROR;
	}

	mylog("asdum=%p start transaction\n", asdum);
	asdum->SetConnection(conn);
	LIFELOCK_ACQUIRE;
	PgDtc_set_async(conn, asdum);
	LIFELOCK_RELEASE;

	return 	SQL_SUCCESS;
}


EXTERN_C RETCODE
IsolateDtcConn(void *conn, BOOL continueConnection)
{
	IAsyncPG *async;

	LIFELOCK_ACQUIRE;
	if (async = (IAsyncPG *) PgDtc_get_async(conn), NULL != async)
	{
		if (PgDtc_get_property(conn, idleInGlobalTransaction))
		{
			async->AddRef();
			LIFELOCK_RELEASE;
			async->separateXAConn(false, continueConnection ? true : false);
			async->Release();
		}
		else
			LIFELOCK_RELEASE;
	}
	else
		LIFELOCK_RELEASE;
	return SQL_SUCCESS;
}


static ITransactionDispenser *getITransactionDispenser(DWORD grfOptions, HRESULT *hres)
{
	static	ITransactionDispenser	*pDtc = NULL;
	HRESULT	res = S_OK;

	if (!pDtc)
	{
		res = DtcGetTransactionManagerEx(NULL, NULL, IID_ITransactionDispenser,
			
			grfOptions, NULL, (void **) &pDtc);
		if (FAILED(res))
		{
			mylog("DtcGetTransactionManager error %x\n", res);
			pDtc = NULL;
		}
	}
	if (hres) 
		*hres = res;

	return pDtc;
}

EXTERN_C void	*GetTransactionObject(HRESULT *hres)
{
	
	ITransaction	*pTra = NULL;
	ITransactionDispenser	*pDtc = NULL;

	if (pDtc = getITransactionDispenser(OLE_TM_FLAG_NONE, hres), NULL == pDtc)
		return pTra;
	HRESULT res = pDtc->BeginTransaction(NULL, ISOLATIONLEVEL_READCOMMITTED,
		0, NULL, &pTra);
	switch (res)
	{
		case S_OK:
			break;
		default:
			pTra = NULL;
	}
	if (hres)
		*hres = res;
	return pTra; 
}

EXTERN_C void	ReleaseTransactionObject(void *pObj)
{
	ITransaction	*pTra = (ITransaction *) pObj;

	if (!pTra)	return;
	pTra->Release();
}

EXTERN_C RETCODE EnlistInDtc(void *conn, void *pTra, int method)
{
	ITransactionDispenser	*pDtc = NULL;
	RETCODE	ret;

	if (!pTra)
	{
		IAsyncPG *asdum = (IAsyncPG *) PgDtc_get_async(conn);
		PgDtc_set_property(conn, enlisted, (void *) 0);
		return SQL_SUCCESS;
	}
	if (CONN_IS_IN_TRANS(conn))
	{
		PgDtc_one_phase_operation(conn, SHUTDOWN_LOCAL_TRANSACTION);
	}
	HRESULT	hres;
	pDtc = getITransactionDispenser(OLE_TM_FLAG_NODEMANDSTART, &hres);
	if (!pDtc)
	{
		char	errmsg[128];
		snprintf(errmsg, sizeof(errmsg), "enlistment error:DtcGetTransactionManager error code=%x", hres);
		PgDtc_set_error(conn, errmsg, __FUNCTION__);
		return SQL_ERROR;
	}
	ret = EnlistInDtc_1pipe(conn, (ITransaction *) pTra, pDtc, method);
	if (SQL_SUCCEEDED(ret))
		PgDtc_set_property(conn, enlisted, (void *) 1);
	return ret;
}

EXTERN_C RETCODE DtcOnDisconnect(void *conn)
{
	mylog("DtcOnDisconnect\n");
	LIFELOCK_ACQUIRE;
	IAsyncPG *asdum = (IAsyncPG *) PgDtc_get_async(conn);
	if (asdum)
	{
		asdum->AddRef();
		LIFELOCK_RELEASE;
		asdum->ReleaseConnection();
		asdum->Release();
	}
	else
		LIFELOCK_RELEASE;
	return SQL_SUCCESS;
}

#endif /* _HANDLE_ENLIST_IN_DTC_ */