Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inst/csv/jarChecksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0c277e1c4c34ed47ef1a095432de82d28f43074143cf782ee29c821e0cb963ef
903a5a97f01cd5b49f55c5da6aa7a017beba2ebf4582916c867590cdb0ba16ce
Binary file modified inst/java/DatabaseConnector.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions java/org/ohdsi/databaseConnector/BatchedInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public boolean executeBatch() throws SQLException, ParseException {
if (!dbms.equals(SPARK)) {
connection.rollback();
}
trySettingAutoCommit(true);
throw e;
} finally {
for (int i = 0; i < columnCount; i++) {
Expand Down Expand Up @@ -203,6 +204,7 @@ private boolean executeBigQueryBatch() throws SQLException, ParseException {
return true;
} catch (SQLException e) {
connection.rollback();
trySettingAutoCommit(true);
throw e;
} finally {
for (int i = 0; i < columnCount; i++) {
Expand Down
35 changes: 28 additions & 7 deletions java/org/ohdsi/databaseConnector/BatchedQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,28 @@ else if (type == Types.TIMESTAMP)
done = false;
totalRowCount = 0;
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException rollbackEx) {
System.err.println("Error rolling back transaction: " + rollbackEx.getMessage());
}
}
rollbackAndRestoreAutoCommit();
throw e;
}
}

private void rollbackAndRestoreAutoCommit() {
if (connection == null)
return;
// Leaving auto-commit off after an error would keep the failed transaction open, causing
// 'current transaction is aborted' errors on subsequent statements:
try {
if (!connection.getAutoCommit())
connection.rollback();
} catch (SQLException rollbackEx) {
System.err.println("Error rolling back transaction: " + rollbackEx.getMessage());
}
try {
trySettingAutoCommit(true);
} catch (SQLException autoCommitEx) {
System.err.println("Error restoring auto-commit: " + autoCommitEx.getMessage());
}
}

protected void finalize() {
try {
Expand All @@ -200,6 +212,15 @@ protected void finalize() {
}

public void fetchBatch() throws SQLException {
try {
fetchRows();
} catch (SQLException e) {
rollbackAndRestoreAutoCommit();
throw e;
}
}

private void fetchRows() throws SQLException {
cleanUpStrings();
rowCount = 0;
while (rowCount < batchSize) {
Expand Down
94 changes: 94 additions & 0 deletions tests/testthat/test-errorRecovery.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
library(testthat)

isAutoCommitOn <- function(connection) {
if (!inherits(connection, "DatabaseConnectorJdbcConnection") || dbms(connection) == "spark") {
return(TRUE)
}
return(rJava::.jcall(connection@jConnection, "Z", "getAutoCommit"))
}

expectConnectionIsUsable <- function(connection, errorReportFile) {
expect_true(isAutoCommitOn(connection))
# A failing statement must not leave the connection in an aborted transaction:
expect_error(
executeSql(
connection = connection,
sql = "INSERT INTO non_existing_table (x) VALUES (1);",
progressBar = FALSE,
reportOverallTime = FALSE,
errorReportFile = errorReportFile
),
"Error executing SQL"
)
result <- renderTranslateQuerySql(connection, "SELECT 1 AS x;", errorReportFile = errorReportFile)
expect_equal(result[1, 1], 1)
expect_true(isAutoCommitOn(connection))
}

for (testServer in testServers) {
test_that(addDbmsToLabel("Connection is usable after a failed query", testServer), {
connection <- connect(testServer$connectionDetails)
on.exit(disconnect(connection))
errorReportFile <- tempfile(fileext = ".txt")
on.exit(unlink(errorReportFile), add = TRUE)

expect_error(
querySql(connection, "SELECT * FROM non_existing_table;", errorReportFile = errorReportFile),
"Error executing SQL"
)
expectConnectionIsUsable(connection, errorReportFile)

if (dbms(connection) == "postgresql") {
# Error raised while fetching a later batch, after the first rows were already retrieved:
sql <- "SELECT CASE WHEN x = 10000 THEN 1 / (x - x) ELSE x END AS y FROM generate_series(1, 20000) x;"
expect_error(
querySql(connection, sql, errorReportFile = errorReportFile),
"division by zero"
)
expectConnectionIsUsable(connection, errorReportFile)
}
})

test_that(addDbmsToLabel("Connection is usable after a failed insert", testServer), {
if (testServer$connectionDetails$dbms == "bigquery") {
skip("Avoiding rate limit on BigQuery")
}
connection <- connect(testServer$connectionDetails)
options(sqlRenderTempEmulationSchema = testServer$tempEmulationSchema)
on.exit(dropEmulatedTempTables(connection))
on.exit(disconnect(connection), add = TRUE)
errorReportFile <- tempfile(fileext = ".txt")
on.exit(unlink(errorReportFile), add = TRUE)

renderTranslateExecuteSql(
connection = connection,
sql = "CREATE TABLE #not_null_test (x INT NOT NULL);",
progressBar = FALSE,
reportOverallTime = FALSE
)
expect_error(
insertTable(
connection = connection,
tableName = "#not_null_test",
data = data.frame(x = c(1L, NA)),
dropTableIfExists = FALSE,
createTable = FALSE,
tempTable = TRUE,
progressBar = FALSE
)
)
expectConnectionIsUsable(connection, errorReportFile)
result <- renderTranslateQuerySql(
connection = connection,
sql = "SELECT COUNT(*) AS row_count FROM #not_null_test;",
errorReportFile = errorReportFile
)
expect_equal(result[1, 1], 0)
renderTranslateExecuteSql(
connection = connection,
sql = "TRUNCATE TABLE #not_null_test; DROP TABLE #not_null_test;",
progressBar = FALSE,
reportOverallTime = FALSE
)
})
}
Loading