diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index 0ec3d4aab5..262728a94b 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -528,7 +528,7 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE } String dbName = connection.getSCatalog(); - boolean needsPrepare = false; + boolean needsPrepare = true; // Retry execution if existing handle could not be re-used. for (int attempt = 1; attempt <= 2; ++attempt) { try { @@ -548,7 +548,7 @@ final void doExecutePreparedStatement(PrepStmtExecCmd command) throws SQLServerE getNextResult(); } catch (SQLException e) { - if (retryBasedOnFailedReuseOfCachedHandle(e, attempt)) + if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare)) continue; else throw e; @@ -566,13 +566,13 @@ else if (EXECUTE_UPDATE == executeMethod && null != resultSet) { /** Should the execution be retried because the re-used cached handle could not be re-used due to server side state changes? */ private boolean retryBasedOnFailedReuseOfCachedHandle(SQLException e, - int attempt) { + int attempt, boolean needsPrepare) { // Only retry based on these error codes and if statementPooling is enabled: // 586: The prepared statement handle %d is not valid in this context. Please verify that current database, user default schema, and // ANSI_NULLS and QUOTED_IDENTIFIER set options are not changed since the handle is prepared. // 8179: Could not find prepared statement with handle %d. - // 99586: Error used for testing. - return 1 == attempt && (586 == e.getErrorCode() || 8179 == e.getErrorCode() || 99586 == e.getErrorCode()) && connection.isStatementPoolingEnabled(); + if(needsPrepare) return false; + return 1 == attempt && (586 == e.getErrorCode() || 8179 == e.getErrorCode()) && connection.isStatementPoolingEnabled(); } /** @@ -2629,7 +2629,7 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand) th } String dbName = connection.getSCatalog(); - boolean needsPrepare = false; + boolean needsPrepare = true; // Retry execution if existing handle could not be re-used. for (int attempt = 1; attempt <= 2; ++attempt) { try { @@ -2692,7 +2692,7 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand) th throw e; // Retry if invalid handle exception. - if (retryBasedOnFailedReuseOfCachedHandle(e, attempt)) { + if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare)) { // reset number of batches prepare numBatchesPrepared = numBatchesExecuted; retry = true; @@ -2723,7 +2723,7 @@ final void doExecutePreparedStatementBatch(PrepStmtBatchExecCmd batchCommand) th } } catch (SQLException e) { - if (retryBasedOnFailedReuseOfCachedHandle(e, attempt) && connection.isStatementPoolingEnabled()) { + if (retryBasedOnFailedReuseOfCachedHandle(e, attempt, needsPrepare) && connection.isStatementPoolingEnabled()) { // Reset number of batches prepared. numBatchesPrepared = numBatchesExecuted; continue;