diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java index 939fef4d8..0d1a154f9 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java @@ -402,7 +402,12 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = this.getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + + if (null != databaseError) { + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + } else { + SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + } } return false; @@ -5404,7 +5409,12 @@ boolean onDone(TDSReader tdsReader) throws SQLServerException { SQLServerError databaseError = getDatabaseError(); MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_serverError")); Object[] msgArgs = {status, (databaseError != null) ? databaseError.getErrorMessage() : ""}; - SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + + if (null != databaseError) { + SQLServerException.makeFromDatabaseError(stmt.connection, null, form.format(msgArgs), databaseError, false); + } else { + SQLServerException.makeFromDriverError(stmt.connection, stmt, form.format(msgArgs), null, false); + } } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java index c176fb2cd..4d69693b9 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java @@ -30,7 +30,12 @@ import java.util.TimeZone; import java.util.UUID; +import com.microsoft.sqlserver.jdbc.SQLServerConnection; +import com.microsoft.sqlserver.jdbc.TestResource; +import com.microsoft.sqlserver.testframework.PrepUtil; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; @@ -48,6 +53,10 @@ public class ResultSetTest extends AbstractTest { private static final String tableName = RandomUtil.getIdentifier("StatementParam"); + private static final String expectedSqlState = "S0001"; + + private static final int expectedErrorCode = 8134; + static final String uuid = UUID.randomUUID().toString(); @BeforeAll @@ -55,6 +64,20 @@ public static void setupTests() throws Exception { setConnection(); } + @BeforeEach + public void init() throws Exception { + try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); + } + } + + @AfterEach + public void cleanUp() throws Exception { + try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); + } + } + /** * Tests proper exception for unsupported operation * @@ -595,4 +618,38 @@ public void testMultipleResultSets() throws SQLException { } } } + + @Test + public void testResultSetFetchBufferSqlErrorState() throws Exception { + try (SQLServerConnection connection = PrepUtil.getConnection(connectionString)) { + try (Statement stmt = connection.createStatement()) { + ResultSet rs = stmt.executeQuery("select 1/0"); + rs.next(); + fail(TestResource.getResource("R_expectedFailPassed")); + } catch (SQLException e) { + assertEquals(expectedSqlState, e.getSQLState()); + assertEquals(expectedErrorCode, e.getErrorCode()); + } + } + } + + @Test + public void testResultSetClientCursorInitializerSqlErrorState() throws Exception { + try (Connection con = PrepUtil.getConnection(connectionString); Statement stmt = con.createStatement()) { + stmt.executeUpdate("create table " + AbstractSQLGenerator.escapeIdentifier(tableName) + + " (col1 int)"); + stmt.setQueryTimeout(1); + boolean hasResults = stmt.execute("select * from " + AbstractSQLGenerator.escapeIdentifier(tableName) + + "; select 1/0"); + while(hasResults) { + ResultSet rs = stmt.getResultSet(); + while (rs.next()) {} + hasResults = stmt.getMoreResults(); + } + fail(TestResource.getResource("R_expectedFailPassed")); + } catch (SQLException e) { + assertEquals(expectedSqlState, e.getSQLState()); + assertEquals(expectedErrorCode, e.getErrorCode()); + } + } }