Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X04yB-0000eU-Nb for pgsql-odbc@arkaria.postgresql.org; Thu, 26 Jun 2014 08:23:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X04yB-00070V-4e for pgsql-odbc@arkaria.postgresql.org; Thu, 26 Jun 2014 08:23:47 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X04yA-00070O-12 for pgsql-odbc@postgresql.org; Thu, 26 Jun 2014 08:23:46 +0000 Received: from mout.kundenserver.de ([212.227.17.10]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X04y2-0000YI-Kj for pgsql-odbc@postgresql.org; Thu, 26 Jun 2014 08:23:44 +0000 Received: from ayaki.localdomain (106-69-71-247.dyn.iinet.net.au [106.69.71.247]) by mrelayeu.kundenserver.de (node=mreue103) with ESMTP (Nemesis) id 0LnDWH-1WTkQY2ird-00hKxq; Thu, 26 Jun 2014 10:23:33 +0200 Message-ID: <53ABD87F.2030204@2ndquadrant.com> Date: Thu, 26 Jun 2014 16:23:27 +0800 From: Craig Ringer Organization: 2nd Quadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: "Inoue, Hiroshi" CC: "pgsql-odbc@postgresql.org" Subject: Re: msdtc with 32-bit app fails to resolve in-doubt or not-notifed transactions References: <53A45B59.70303@2ndquadrant.com> <53A4A8FE.6010901@tpf.co.jp> <53A50F8E.3090109@2ndquadrant.com> <53A53BF9.1020603@2ndquadrant.com> <53A7762E.10705@tpf.co.jp> <53A781AE.6020009@2ndquadrant.com> <53A89AC5.4010004@tpf.co.jp> <53A8EB74.5040806@2ndquadrant.com> <53AADC07.3090206@2ndquadrant.com> In-Reply-To: <53AADC07.3090206@2ndquadrant.com> X-Enigmail-Version: 1.6 Content-Type: multipart/mixed; boundary="------------090100070708010000080602" X-Provags-ID: V02:K0:Ws6M1kxPsO0cGrszXqpFFrK3njNPFoL1I+fWg2aSxMi DcFFFqvFfreGLeEmRtvKQSQgDKO9wAdUmrJh9YBndCmzqo+gXw hYca5hnmH864hsvJjmYKf1ylIgjXsYrg+EySlNNOH66sBaKBvg tc9Oraz+z8s26q6n66qSuDOhVrW7bk2Rdw38KUyjp+ckoT911I Pj+jFU19cXovTQdM7+EBxWfRNLXmMMfPBu8e7zDcFZKatmKFI8 X1+StrW0bEIldzfDTP165ic8yhcTN9+pO2HTjStcWwWi2YXE8V PyDyyTKzsAXoxLgf/MhxcDU1rNiIA69WgezfdoYWWdB++LalSW Ke37ZpluR3ft1tUSjo7ffQ4EZYtYMdU/YjbXk8s9o X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org This is a multi-part message in MIME format. --------------090100070708010000080602 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 06/25/2014 10:26 PM, Craig Ringer wrote: > On 06/24/2014 11:07 AM, Craig Ringer wrote: >> On 06/24/2014 05:23 AM, Inoue, Hiroshi wrote: >>> >>> XARMCreate() triggers xa_open() ( and xa_close()) probably so as to >>> check that the XADLL works. Currently xa_open() doesn't connect to >>> the database and simply saves the connection information for subsequent >>> xa_commit(), xa_rollback() or xa_recover() call. When original process >>> finished COMMIT/ROLLBACK PREPARED properly, No database access occurs >>> in msdtc process. We can change xa_open() so that it connects to the >>> database immediately and causes an error to XARMCreate() when the >>> connection fails. >> >> That's enlightening. Thankyou very much. >> >> I'll happily implement that and send in a patch. It may take a week or >> two as I have some other projects on the boil, but hopefully it won't >> take super long. > > ... and done. > > Please merge branch fix-syswow64-msdtc from my repo at > https://github.com/ringerc/psqlODBC.git . > > see: https://github.com/ringerc/psqlODBC/pull/2 > > Patch attached if you prefer that. See patch header and in-code comments > for details. Here's the revised patch. I've tested this against the customer's original test case and it now runs properly. MSDTC correctly recovers abandoned tx's in which Phase I commit succeeded then the app exited / crashed before Phase II completed on one or both sessions. Updated patch attached: git am -s 0001-Fix-driver-name-mismatch-between-32-bit-ODBC-app-and.patch or pull my branch, above. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------090100070708010000080602 Content-Type: text/x-patch; name="0001-Fix-driver-name-mismatch-between-32-bit-ODBC-app-and.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fix-driver-name-mismatch-between-32-bit-ODBC-app-and.pa"; filename*1="tch" From 85e208db45cd675d69aad44a95f8c79854682f5f Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Wed, 25 Jun 2014 15:17:00 +0800 Subject: [PATCH] Fix driver name mismatch between 32-bit ODBC app and 64-bit MSDTC host If the 32-bit driver runs on a 64-bit host within a 32-bit app running under SysWow64 then XA transactions will fail to recover. That's because recovery is done by pgxalib.dll which runs within MSDTC.exe - a 64-bit application. It connects to a DSN supplied by psqlODBC in EnlistInDtc_1pipe during DTC enlistment by the XA resource manager. This DSN uses the driver name of the client's ODBC driver - which is fine so long as the client and MSDTC architectures match. If they don't then MSDTC will fail to connect to Pg, so it'll never resolve transactions. Traces of MSDTC will show RM_COMMIT_DELIVERY_FAILED_DUE_TO_CONNECTION_DOWN events. Traces of pgxalib.dll will show xa_recover(..) being entered, followed by "SQLDriverConnect return=-1". To fix this, we detect if we're running under SysWow64 by examining the PROCESSOR_ARCHITEW6432 environment variable and supply the 64-bit driver name if so. See mailing list thread: http://www.postgresql.org/message-id/53A45B59.70303@2ndquadrant.com for details. Craig Ringer --- connection.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- psqlodbc.h | 8 ++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/connection.c b/connection.c index 794ddde..3aeb71a 100644 --- a/connection.c +++ b/connection.c @@ -4362,10 +4362,52 @@ const char *CurrCatString(const ConnectionClass *conn) DLL_DECLARE void PgDtc_create_connect_string(void *self, char *connstr, int strsize) { ConnectionClass *conn = (ConnectionClass *) self; - + SYSTEM_INFO si; ConnInfo *ci = &(conn->connInfo); + const char *drivername = ci->drivername; + +#if defined(_WIN32) && !defined(_WIN64) + /* + * If this is an x86 driver running on an x64 host then the driver name + * passed to MSDTC must be the (x64) driver but the client app will be + * using the 32-bit driver name. So MSDTC.exe will fail to find the driver + * and we'll fail to recover XA transactions. + * + * IsWow64Process(...) would be the ideal function for this, but is only + * available on Windows 6+ (Vista/2k8). We'd use GetNativeSystemInfo, which + * is supported on XP and 2k3, instead, but that won't link with older + * SDKs. + * + * It's impler to just test the PROCESSOR_ARCHITEW6432 environment + * variable. + * + * See http://www.postgresql.org/message-id/53A45B59.70303@2ndquadrant.com + * for details on this issue. + */ + const char * const procenv = getenv("PROCESSOR_ARCHITEW6432"); + if (procenv != NULL && strcmp(procenv, "AMD64") == 0) + { + /* + * We're a 32-bit binary running under SysWow64 on a 64-bit host and need + * to pass a different driver name. + * + * To avoid playing memory management games, just return a different + * string constant depending on the unicode-ness of the driver. + * + * It probably doesn't matter whether we use the Unicode or ANSI driver + * for the XA transaction manager, but pick the same as the client driver + * to keep things as similar as possible. + */ +#ifdef UNICODE_SUPPORT + drivername = DBMS_NAME_UNICODE"(x64)"; +#else + drivername = DBMS_NAME_ANSI"(x64)"; +#endif + } +#endif // _WIN32 && !_WIN64 + snprintf(connstr, strsize, "DRIVER={%s};SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s;" ABBR_SSLMODE "=%s", - ci->drivername, ci->server, ci->port, ci->database, ci->username, SAFE_NAME(ci->password), ci->sslmode); + drivername, ci->server, ci->port, ci->database, ci->username, SAFE_NAME(ci->password), ci->sslmode); } DLL_DECLARE void PgDtc_set_async(void *self, void *async) diff --git a/psqlodbc.h b/psqlodbc.h index c0c895f..05dcc9e 100644 --- a/psqlodbc.h +++ b/psqlodbc.h @@ -249,6 +249,10 @@ typedef double SDOUBLE; /* Driver stuff */ #define DRIVERNAME "PostgreSQL ODBC" + +#define DBMS_NAME_UNICODE "PostgreSQL Unicode" +#define DBMS_NAME_ANSI "PostgreSQL ANSI" + #if (ODBCVER >= 0x0300) #if (ODBCVER >= 0x0351) #define DRIVER_ODBC_VER "03.51" @@ -257,9 +261,9 @@ typedef double SDOUBLE; #endif /* ODBCVER 0x0351 */ #ifndef DBMS_NAME #ifdef UNICODE_SUPPORT -#define DBMS_NAME "PostgreSQL Unicode" +#define DBMS_NAME DBMS_NAME_UNICODE #else -#define DBMS_NAME "PostgreSQL ANSI" +#define DBMS_NAME DBMS_NAME_ANSI #endif /* UNICODE_SUPPORT */ #endif /* DBMS_NAME */ #else -- 1.9.0 --------------090100070708010000080602 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090100070708010000080602--