-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(bigquery-jdbc): avoid rollback on statement close in manual commit mode #13503
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
Changes from 2 commits
e81a651
bb53c59
2751219
f49a91d
1c7abba
38139f9
91a9e02
f8561be
5b16b74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2324,6 +2324,104 @@ public void testConnectionWithMultipleTransactionCommits() throws SQLException { | |
| connection.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPreparedStatementCloseDoesNotRollbackTransaction() throws SQLException { | ||
| String TRANSACTION_TABLE = "JDBC_PS_CLOSE_TABLE" + randomNumber; | ||
| String createTransactionTable = | ||
| String.format( | ||
| "CREATE OR REPLACE TABLE %s.%s (`id` INTEGER, `name` STRING, `age` INTEGER);", | ||
| DATASET, TRANSACTION_TABLE); | ||
| String insertQuery = | ||
| String.format("INSERT INTO %s.%s (id, name, age) VALUES (?, ?, ?);", DATASET, TRANSACTION_TABLE); | ||
| String selectQuery = | ||
| String.format("SELECT id, name, age FROM %s.%s ORDER BY id;", DATASET, TRANSACTION_TABLE); | ||
|
|
||
| bigQueryStatement.execute(createTransactionTable); | ||
|
|
||
| Connection connection = DriverManager.getConnection(session_enabled_connection_uri); | ||
| connection.setAutoCommit(false); | ||
|
|
||
| PreparedStatement ps1 = connection.prepareStatement(insertQuery); | ||
| PreparedStatement ps2 = connection.prepareStatement(insertQuery); | ||
| try { | ||
| ps1.setInt(1, 1); | ||
| ps1.setString(2, "DwightShrute"); | ||
| ps1.setInt(3, 10); | ||
| assertEquals(1, ps1.executeUpdate()); | ||
|
|
||
| ps2.setInt(1, 2); | ||
| ps2.setString(2, "MichaelScott"); | ||
| ps2.setInt(3, 20); | ||
| assertEquals(1, ps2.executeUpdate()); | ||
|
|
||
| ps1.close(); | ||
| connection.commit(); | ||
|
|
||
| ResultSet resultSet = bigQueryStatement.executeQuery(selectQuery); | ||
| int rowCount = 0; | ||
| while (resultSet.next()) { | ||
| rowCount++; | ||
| assertEquals(rowCount, resultSet.getInt(1)); | ||
| } | ||
| assertEquals(2, rowCount); | ||
| } finally { | ||
| try { | ||
| ps2.close(); | ||
| } finally { | ||
| bigQueryStatement.execute( | ||
| String.format("DROP TABLE IF EXISTS %s.%s", DATASET, TRANSACTION_TABLE)); | ||
| connection.close(); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The connection, prepared statements, and result set are not safely closed if an exception occurs during test execution. If try (Connection connection = DriverManager.getConnection(session_enabled_connection_uri)) {
connection.setAutoCommit(false);
try (PreparedStatement ps1 = connection.prepareStatement(insertQuery);
PreparedStatement ps2 = connection.prepareStatement(insertQuery)) {
ps1.setInt(1, 1);
ps1.setString(2, "DwightShrute");
ps1.setInt(3, 10);
assertEquals(1, ps1.executeUpdate());
ps2.setInt(1, 2);
ps2.setString(2, "MichaelScott");
ps2.setInt(3, 20);
assertEquals(1, ps2.executeUpdate());
ps1.close();
connection.commit();
try (ResultSet resultSet = bigQueryStatement.executeQuery(selectQuery)) {
int rowCount = 0;
while (resultSet.next()) {
rowCount++;
assertEquals(rowCount, resultSet.getInt(1));
}
assertEquals(2, rowCount);
}
}
} finally {
bigQueryStatement.execute(
String.format("DROP TABLE IF EXISTS %s.%s", DATASET, TRANSACTION_TABLE));
}References
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| } | ||
|
|
||
| @Test | ||
| public void testClosingUnusedPreparedStatementDoesNotRollbackPreviousExecute() | ||
| throws SQLException { | ||
| String TRANSACTION_TABLE = "JDBC_PS_UNUSED_CLOSE_TABLE" + randomNumber; | ||
| String createTransactionTable = | ||
| String.format( | ||
| "CREATE OR REPLACE TABLE %s.%s (`id` INTEGER, `name` STRING, `age` INTEGER);", | ||
| DATASET, TRANSACTION_TABLE); | ||
| String insertQuery = | ||
| String.format("INSERT INTO %s.%s (id, name, age) VALUES (?, ?, ?);", DATASET, TRANSACTION_TABLE); | ||
| String selectQuery = | ||
| String.format("SELECT id, name, age FROM %s.%s ORDER BY id;", DATASET, TRANSACTION_TABLE); | ||
|
|
||
| bigQueryStatement.execute(createTransactionTable); | ||
|
|
||
| Connection connection = DriverManager.getConnection(session_enabled_connection_uri); | ||
| connection.setAutoCommit(false); | ||
|
|
||
| PreparedStatement ps1 = connection.prepareStatement(insertQuery); | ||
| PreparedStatement ps2 = connection.prepareStatement(insertQuery); | ||
| try { | ||
|
|
||
| ps2.setInt(1, 1); | ||
| ps2.setString(2, "MichaelScott"); | ||
| ps2.setInt(3, 20); | ||
| assertEquals(1, ps2.executeUpdate()); | ||
|
|
||
| ps1.close(); | ||
| connection.commit(); | ||
|
|
||
| ResultSet resultSet = bigQueryStatement.executeQuery(selectQuery); | ||
| assertTrue(resultSet.next()); | ||
| assertEquals(1, resultSet.getInt(1)); | ||
| assertEquals("MichaelScott", resultSet.getString(2)); | ||
| assertEquals(20, resultSet.getInt(3)); | ||
| assertFalse(resultSet.next()); | ||
| } finally { | ||
| try { | ||
| ps2.close(); | ||
| } finally { | ||
| bigQueryStatement.execute( | ||
| String.format("DROP TABLE IF EXISTS %s.%s", DATASET, TRANSACTION_TABLE)); | ||
| connection.close(); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, the connection, prepared statements, and result set in this test are not safely closed in case of exceptions, leading to potential resource leaks. Refactoring to use try-with-resources ensures robust resource management. try (Connection connection = DriverManager.getConnection(session_enabled_connection_uri)) {
connection.setAutoCommit(false);
try (PreparedStatement ps1 = connection.prepareStatement(insertQuery);
PreparedStatement ps2 = connection.prepareStatement(insertQuery)) {
ps2.setInt(1, 1);
ps2.setString(2, "MichaelScott");
ps2.setInt(3, 20);
assertEquals(1, ps2.executeUpdate());
ps1.close();
connection.commit();
try (ResultSet resultSet = bigQueryStatement.executeQuery(selectQuery)) {
assertTrue(resultSet.next());
assertEquals(1, resultSet.getInt(1));
assertEquals("MichaelScott", resultSet.getString(2));
assertEquals(20, resultSet.getInt(3));
assertFalse(resultSet.next());
}
}
} finally {
bigQueryStatement.execute(
String.format("DROP TABLE IF EXISTS %s.%s", DATASET, TRANSACTION_TABLE));
}References
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix; I think using try-with-resources would address the issue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| } | ||
|
|
||
| // Private Helper functions | ||
| private String getSessionId() throws InterruptedException { | ||
| QueryJobConfiguration stubJobConfig = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add this logic to the connection.closeImpl()?
I think when connection is closed, it needs to cancel any pending transaction otherwise they will be eventually cancelled by some timeout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Not sure about calling rollbackImpl() as-is, since it starts a new transaction after the rollback, which seems unnecessary during close().
On the other hand, connection.close() may fail before the connection is fully closed, so keeping the existing rollback behavior might actually be the safer option.