Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix | Clear batch params when clearing params in pstmt #1509

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -364,6 +364,7 @@ public final void clearParameters() throws SQLServerException {
loggerExternal.entering(getClassNameLogging(), "clearParameters");
checkClosed();
encryptionMetadataIsRetrieved = false;
cryptoMetaBatch.clear();
int i;
if (inOutParam == null)
return;
Expand Down
Expand Up @@ -110,6 +110,45 @@ public void testAddbatch2AEOnConnection() throws SQLException {
public void testAddbatch2() throws SQLException {
testAddBatch2(getConnection());
}

@Test
public void testClearBatch() throws SQLException {
String batchTable = 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 (Connection conn = getConnection(); 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)
Expand Down