From 4e732a0d7386cd1c0a624807b08d9534e034b4b9 Mon Sep 17 00:00:00 2001 From: Karina Zhou Date: Mon, 10 May 2021 17:40:54 -0700 Subject: [PATCH 1/3] Port change and fix test failure --- .../jdbc/SQLServerPreparedStatement.java | 1 + .../BatchExecutionWithNullTest.java | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java index c9d72d3367..ab46fc0b92 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java @@ -373,6 +373,7 @@ public final void clearParameters() throws SQLServerException { loggerExternal.entering(getClassNameLogging(), "clearParameters"); checkClosed(); encryptionMetadataIsRetrieved = false; + cryptoMetaBatch.clear(); int i; if (inOutParam == null) return; diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java index b66434b8b0..079c9ba635 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java @@ -110,6 +110,68 @@ public void testAddbatch2AEOnConnection() throws SQLException { public void testAddbatch2() throws SQLException { testAddBatch2(getConnection()); } + + /** + * TestClearBatch with AE enabled on the connection + * + * @throws SQLException + */ + @Test + @Tag(Constants.xSQLv12) + public void testClearBatchAEOnConnection() throws SQLException { + try (Connection connection = PrepUtil.getConnection(connectionString + ";columnEncryptionSetting=Enabled;")) { + testClearBatch(connection); + } + } + + /** + * Test the same as testClearBatchAEOnConnection, with AE disabled + * + * @throws SQLException + */ + @Test + public void testClearBatch() throws SQLException { + testClearBatch(getConnection()); + } + + public void testClearBatch(Connection conn) throws SQLException { + String batchTable = TestUtils + .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("batchTable"))); + String CREATE_TABLE_SQL = "create table " + batchTable + " (KEY1 numeric(19,0) not null, KEY2 numeric(19,0) not null, primary key (KEY1, KEY2))"; + String INSERT_ROW_SQL = "INSERT INTO " + batchTable + "(KEY1, KEY2) VALUES(?, ?)"; + + try (Statement s = conn.createStatement()) { + try ( PreparedStatement pstmt = conn.prepareStatement(INSERT_ROW_SQL)) { + s.execute(CREATE_TABLE_SQL); + // Set auto-commit to false + conn.setAutoCommit(false); + executeBatch(pstmt, 10, "foo".hashCode() + 1); + pstmt.clearParameters(); + executeBatch(pstmt, 10, "bar".hashCode() + 2); + conn.commit(); + } catch (Exception e) { + conn.rollback(); + throw e; + } finally { + TestUtils.dropTableIfExists(batchTable, s); + } + } + } + + private void executeBatch(PreparedStatement pstmt, int count, int run) throws SQLException { + long base = System.currentTimeMillis(); + + for (int idx = 1; idx <= count; idx++) { + pstmt.setLong(1, base + idx); + pstmt.setLong(2, run); + pstmt.addBatch(); + } + + int[] rowCounts = pstmt.executeBatch(); + for (int idx = 0; idx < rowCounts.length; idx++) { + System.out.printf("Row %2d %s successfully inserted.\n", idx + 1, rowCounts[idx] == 1 ? "was" : "was not"); + } + } @BeforeEach @Tag(Constants.xSQLv12) From ee4669bad0847f76ad8533bf7e38302c8a8253c8 Mon Sep 17 00:00:00 2001 From: Karina Zhou Date: Fri, 14 May 2021 17:03:46 -0700 Subject: [PATCH 2/3] address comments --- .../jdbc/preparedStatement/BatchExecutionWithNullTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java index 079c9ba635..85abe014f8 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java @@ -134,7 +134,7 @@ public void testClearBatch() throws SQLException { testClearBatch(getConnection()); } - public void testClearBatch(Connection conn) throws SQLException { + private void testClearBatch(Connection conn) throws SQLException { String batchTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("batchTable"))); String CREATE_TABLE_SQL = "create table " + batchTable + " (KEY1 numeric(19,0) not null, KEY2 numeric(19,0) not null, primary key (KEY1, KEY2))"; @@ -169,7 +169,7 @@ private void executeBatch(PreparedStatement pstmt, int count, int run) throws SQ int[] rowCounts = pstmt.executeBatch(); for (int idx = 0; idx < rowCounts.length; idx++) { - System.out.printf("Row %2d %s successfully inserted.\n", idx + 1, rowCounts[idx] == 1 ? "was" : "was not"); + assertTrue(rowCounts[idx] == 1, "Row " + idx + " was not successfully inserted."); } } From 1d852e88e033f009917ee5c6e3a91f7581de7243 Mon Sep 17 00:00:00 2001 From: Karina Zhou Date: Mon, 17 May 2021 15:27:42 -0700 Subject: [PATCH 3/3] Add comments to testClearBatch --- .../jdbc/preparedStatement/BatchExecutionWithNullTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java index 85abe014f8..1da2e4744c 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithNullTest.java @@ -135,6 +135,7 @@ public void testClearBatch() throws SQLException { } private void testClearBatch(Connection conn) throws SQLException { + // Use specific table for this testing String batchTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("batchTable"))); String CREATE_TABLE_SQL = "create table " + batchTable + " (KEY1 numeric(19,0) not null, KEY2 numeric(19,0) not null, primary key (KEY1, KEY2))";