Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wzo9d-00071b-Uq for pgsql-odbc@arkaria.postgresql.org; Wed, 25 Jun 2014 14:26:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wzo9d-0004jO-Do for pgsql-odbc@arkaria.postgresql.org; Wed, 25 Jun 2014 14:26:29 +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 1Wzo9c-0004h1-2u for pgsql-odbc@postgresql.org; Wed, 25 Jun 2014 14:26:28 +0000 Received: from mout.kundenserver.de ([212.227.17.24]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wzo9Y-0005TB-5L for pgsql-odbc@postgresql.org; Wed, 25 Jun 2014 14:26:26 +0000 Received: from ayaki.localdomain (106-69-71-247.dyn.iinet.net.au [106.69.71.247]) by mrelayeu.kundenserver.de (node=mreue102) with ESMTP (Nemesis) id 0MBCPl-1WsPlp08Gb-00ADfv; Wed, 25 Jun 2014 16:26:21 +0200 Message-ID: <53AADC07.3090206@2ndquadrant.com> Date: Wed, 25 Jun 2014 22:26:15 +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> In-Reply-To: <53A8EB74.5040806@2ndquadrant.com> X-Enigmail-Version: 1.6 Content-Type: multipart/mixed; boundary="------------060200080707060808030109" X-Provags-ID: V02:K0:18/88pabLN+GPrgY5VVHVG2HZtsV67TeWhYJm1IUQyL Z3ZWfB+5GWE42QY+Ld4mRIif93ekYuAw7Hc1HCywBM2+2U0pYh EMAVMrGQYY5W/ykQj4qoGeQ04BebO+U8+1PvKcNB4XjEdgJ49r GVy3QTLLsWSUsocfUgHoAo5NwllPdqRieTyQx2LvpMrDpgm8Os gKDn44w2uI/sq9r3wt0+LO4B/ntDwj3QDT+HQ71EzGtbGh1S3w n6hQEC5FuHyWh8f6Au03fnSE7HisJDAgCFQJBizve9phOu2tFV +4r6tZr2CzXDVm9pGslLd+SVaP410L4OhmzYNFe1g8odkIQdt6 BkN28z0Fv7bceCyE19HNFz5B7D4fUUs3WIhecwoMI 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. --------------060200080707060808030109 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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. It may well be better to just rewrite pgxalib.dll to use libpq, but I wanted to stay non-intrusive and simple if possible. For now at least. I'm not sure the unicode vs ansi dance is necessary in the patch is necessary - it's probably ok to just always use the unicode driver. Again, though, deviating from what the driver already does as little as possible seemed reasonable. -- Craig Ringer http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------060200080707060808030109 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 efd31cc9788710c03d73bda4fcb3992145ccc7e8 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. --- connection.c | 42 ++++++++++++++++++++++++++++++++++++++++-- psqlodbc.h | 8 ++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/connection.c b/connection.c index 794ddde..512482c 100644 --- a/connection.c +++ b/connection.c @@ -4362,10 +4362,48 @@ 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. + */ +#ifdef UNICODE_SUPPORT + drivername = DBMS_NAME_UNICODE; +#else + drivername = DBMS_NAME_ANSI; +#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 --------------060200080707060808030109 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 --------------060200080707060808030109--