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 test issue holding lock on database #1593

Merged
merged 18 commits into from
Jun 3, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ public void testAddbatch2AEOnConnection() throws SQLException {
*/
@Test
public void testAddbatch2() throws SQLException {
testAddBatch2(getConnection());
try (Connection connection = getConnection()) {
testAddBatch2(connection);
}
}

/**
* TestClearBatch with AE enabled on the connection
*
Expand All @@ -123,38 +125,41 @@ public void testClearBatchAEOnConnection() throws SQLException {
testClearBatch(connection);
}
}

/**
* Test the same as testClearBatchAEOnConnection, with AE disabled
*
* @throws SQLException
*/
@Test
public void testClearBatch() throws SQLException {
testClearBatch(getConnection());
try (Connection connection = getConnection()) {
testClearBatch(connection);
}
}

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 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)) {
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();
lilgreenbird marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
conn.rollback();
throw e;
} finally {
TestUtils.dropTableIfExists(batchTable, s);
conn.commit();
}
}
}
Expand Down