From f2766233096ce0b1be59e0a1af3d9be5db245555 Mon Sep 17 00:00:00 2001 From: Suraiya Hameed Date: Tue, 18 Apr 2017 13:20:07 -0700 Subject: [PATCH] initialize XA resource --- .../sqlserver/jdbc/SQLServerXAResource.java | 83 +++++++++---------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAResource.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAResource.java index d09def6ab..b2d194808 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAResource.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerXAResource.java @@ -380,56 +380,52 @@ private String typeDisplay(int type) { SQLServerCallableStatement cs = null; try { synchronized (this) { - if (controlConnection == null) { + if (!xaInitDone) { try { synchronized (xaInitLock) { - if (!xaInitDone) { - SQLServerCallableStatement initCS = null; - - initCS = (SQLServerCallableStatement) controlConnection.prepareCall("{call master..xp_sqljdbc_xa_init_ex(?, ?,?)}"); - initCS.registerOutParameter(1, Types.INTEGER); // Return status - initCS.registerOutParameter(2, Types.CHAR); // Return error message - initCS.registerOutParameter(3, Types.CHAR); // Return version number + SQLServerCallableStatement initCS = null; + + initCS = (SQLServerCallableStatement) controlConnection.prepareCall("{call master..xp_sqljdbc_xa_init_ex(?, ?,?)}"); + initCS.registerOutParameter(1, Types.INTEGER); // Return status + initCS.registerOutParameter(2, Types.CHAR); // Return error message + initCS.registerOutParameter(3, Types.CHAR); // Return version number + try { + initCS.execute(); + } + catch (SQLServerException eX) { try { - initCS.execute(); - } - catch (SQLServerException eX) { - try { - initCS.close(); - // Mapping between control connection and xaresource is 1:1 - controlConnection.close(); - } - catch (SQLException e3) { - // we really want to ignore this failue - if (xaLogger.isLoggable(Level.FINER)) - xaLogger.finer(toString() + " Ignoring exception when closing failed execution. exception:" + e3); - } - if (xaLogger.isLoggable(Level.FINER)) - xaLogger.finer(toString() + " exception:" + eX); - throw eX; - } - - // Check for error response from xp_sqljdbc_xa_init. - int initStatus = initCS.getInt(1); - String initErr = initCS.getString(2); - String versionNumberXADLL = initCS.getString(3); - if (xaLogger.isLoggable(Level.FINE)) - xaLogger.fine(toString() + " Server XA DLL version:" + versionNumberXADLL); - initCS.close(); - if (XA_OK != initStatus) { - assert null != initErr && initErr.length() > 1; + initCS.close(); + // Mapping between control connection and xaresource is 1:1 controlConnection.close(); - - MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_failedToInitializeXA")); - Object[] msgArgs = {String.valueOf(initStatus), initErr}; - XAException xex = new XAException(form.format(msgArgs)); - xex.errorCode = initStatus; + } + catch (SQLException e3) { + // we really want to ignore this failue if (xaLogger.isLoggable(Level.FINER)) - xaLogger.finer(toString() + " exception:" + xex); - throw xex; + xaLogger.finer(toString() + " Ignoring exception when closing failed execution. exception:" + e3); } + if (xaLogger.isLoggable(Level.FINER)) + xaLogger.finer(toString() + " exception:" + eX); + throw eX; + } - xaInitDone = true; + // Check for error response from xp_sqljdbc_xa_init. + int initStatus = initCS.getInt(1); + String initErr = initCS.getString(2); + String versionNumberXADLL = initCS.getString(3); + if (xaLogger.isLoggable(Level.FINE)) + xaLogger.fine(toString() + " Server XA DLL version:" + versionNumberXADLL); + initCS.close(); + if (XA_OK != initStatus) { + assert null != initErr && initErr.length() > 1; + controlConnection.close(); + + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_failedToInitializeXA")); + Object[] msgArgs = {String.valueOf(initStatus), initErr}; + XAException xex = new XAException(form.format(msgArgs)); + xex.errorCode = initStatus; + if (xaLogger.isLoggable(Level.FINER)) + xaLogger.finer(toString() + " exception:" + xex); + throw xex; } } } @@ -440,6 +436,7 @@ private String typeDisplay(int type) { xaLogger.finer(toString() + " exception:" + form.format(msgArgs)); SQLServerException.makeFromDriverError(null, null, form.format(msgArgs), null, true); } + xaInitDone = true; } }