Skip to content

Commit

Permalink
Fix | Clear batch params when clearing params in pstmt (#1578)
Browse files Browse the repository at this point in the history
Port changes from #1509 and fix test failure
  • Loading branch information
karinazhou authored May 27, 2021
1 parent b97172c commit 22864c2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,69 @@ 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());
}

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))";
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++) {
assertTrue(rowCounts[idx] == 1, "Row " + idx + " was not successfully inserted.");
}
}

@BeforeEach
@Tag(Constants.xSQLv12)
Expand Down

0 comments on commit 22864c2

Please sign in to comment.