Skip to content

Commit

Permalink
rollback transactions if they fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ablack3 committed Aug 25, 2024
1 parent 93c031d commit 4ff0177
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 52 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: DatabaseConnector
Type: Package
Title: Connecting to Various Database Platforms
Version: 6.3.3
Version: 6.3.3.9000
Date: 2024-06-13
Authors@R: c(
person("Martijn", "Schuemie", email = "[email protected]", role = c("aut", "cre")),
Expand Down
Binary file modified inst/java/DatabaseConnector.jar
Binary file not shown.
12 changes: 10 additions & 2 deletions java/org/ohdsi/databaseConnector/BatchedInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,18 @@ public boolean executeBatch() throws SQLException, ParseException {
statement.close();
connection.clearWarnings();
trySettingAutoCommit(true);
return true;
} catch (SQLException e) {
if (!dbms.equals(SPARK)) {
connection.rollback();
}
throw e;
} finally {
for (int i = 0; i < columnCount; i++) {
columns[i] = null;
}
rowCount = 0;
}
return true;
}

/**
Expand Down Expand Up @@ -195,13 +200,16 @@ private boolean executeBigQueryBatch() throws SQLException, ParseException {
trySettingAutoCommit(true);
offset += BIG_DATA_BATCH_INSERT_LIMIT;
}
return true;
} catch (SQLException e) {
connection.rollback();
throw e;
} finally {
for (int i = 0; i < columnCount; i++) {
columns[i] = null;
}
rowCount = 0;
}
return true;
}

private static long[] convertFromInteger64ToLong(double[] value) {
Expand Down
117 changes: 68 additions & 49 deletions java/org/ohdsi/databaseConnector/BatchedQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,54 +135,76 @@ public BatchedQuery(Connection connection, String query, String dbms) throws SQL
this.connection = connection;
this.dbms = dbms;
trySettingAutoCommit(false);
Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(FETCH_SIZE);
resultSet = statement.executeQuery(query);
resultSet.setFetchSize(FETCH_SIZE);
ResultSetMetaData metaData = resultSet.getMetaData();

columnTypes = new int[metaData.getColumnCount()];
columnSqlTypes = new String[metaData.getColumnCount()];
for (int columnIndex = 0; columnIndex < metaData.getColumnCount(); columnIndex++) {
columnSqlTypes[columnIndex] = metaData.getColumnTypeName(columnIndex + 1);
int type = metaData.getColumnType(columnIndex + 1);
String className = metaData.getColumnClassName(columnIndex + 1);

System.out.println("======================== debug ====================");
System.out.println("type= " + type);
System.out.println("className= " + className);
System.out.println("columnSqlTypes[columnIndex]= " + columnSqlTypes[columnIndex]);
System.out.println("Types.BOOLEAN=" + Types.BOOLEAN);

Statement statement = null;
try {
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(FETCH_SIZE);
resultSet = statement.executeQuery(query);
resultSet.setFetchSize(FETCH_SIZE);
ResultSetMetaData metaData = resultSet.getMetaData();
columnTypes = new int[metaData.getColumnCount()];
columnSqlTypes = new String[metaData.getColumnCount()];

for (int columnIndex = 0; columnIndex < metaData.getColumnCount(); columnIndex++) {
columnSqlTypes[columnIndex] = metaData.getColumnTypeName(columnIndex + 1);
int type = metaData.getColumnType(columnIndex + 1);
String className = metaData.getColumnClassName(columnIndex + 1);

//Types.BOOLEAN is 16 but for a boolean datatype in the database type is -7.
int precision = metaData.getPrecision(columnIndex + 1);
System.out.println("precision=" + precision);
int scale = metaData.getScale(columnIndex + 1);
if (type == Types.BOOLEAN || className.equals("java.lang.Boolean") || columnSqlTypes[columnIndex] == "bool"
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision == 1))
columnTypes[columnIndex] = BOOLEAN;
else if (type == Types.INTEGER || type == Types.SMALLINT || type == Types.TINYINT
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision > 0 && precision != 19 && scale == 0))
columnTypes[columnIndex] = INTEGER;
else if (type == Types.BIGINT
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision > 0 && scale == 0))
columnTypes[columnIndex] = INTEGER64;
else if (type == Types.DECIMAL || type == Types.DOUBLE || type == Types.FLOAT || type == Types.NUMERIC || type == Types.REAL)
columnTypes[columnIndex] = NUMERIC;
else if (type == Types.DATE)
columnTypes[columnIndex] = DATE;
else if (type == Types.TIMESTAMP)
columnTypes[columnIndex] = DATETIME;
else
columnTypes[columnIndex] = STRING;
/*
System.out.println("======================== debug ====================");
System.out.println("type= " + type);
System.out.println("className= " + className);
System.out.println("columnSqlTypes[columnIndex]= " + columnSqlTypes[columnIndex]);
System.out.println("Types.BOOLEAN=" + Types.BOOLEAN);
*/

//Types.BOOLEAN is 16 but for a boolean datatype in the database type is -7.
int precision = metaData.getPrecision(columnIndex + 1);
System.out.println("precision=" + precision);
int scale = metaData.getScale(columnIndex + 1);
if (type == Types.BOOLEAN || className.equals("java.lang.Boolean") || columnSqlTypes[columnIndex] == "bool"
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision == 1))
columnTypes[columnIndex] = BOOLEAN;
else if (type == Types.INTEGER || type == Types.SMALLINT || type == Types.TINYINT
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision > 0 && precision != 19 && scale == 0))
columnTypes[columnIndex] = INTEGER;
else if (type == Types.BIGINT
|| (dbms.equals("oracle") && className.equals("java.math.BigDecimal") && precision > 0 && scale == 0))
columnTypes[columnIndex] = INTEGER64;
else if (type == Types.DECIMAL || type == Types.DOUBLE || type == Types.FLOAT || type == Types.NUMERIC || type == Types.REAL)
columnTypes[columnIndex] = NUMERIC;
else if (type == Types.DATE)
columnTypes[columnIndex] = DATE;
else if (type == Types.TIMESTAMP)
columnTypes[columnIndex] = DATETIME;
else
columnTypes[columnIndex] = STRING;
}
columnNames = new String[metaData.getColumnCount()];
for (int columnIndex = 0; columnIndex < metaData.getColumnCount(); columnIndex++)
columnNames[columnIndex] = metaData.getColumnLabel(columnIndex + 1);
reserveMemory();
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());
}
}
throw e;
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException closeEx) {
// Log close exception
System.err.println("Error closing statement: " + closeEx.getMessage());
}
}
}
columnNames = new String[metaData.getColumnCount()];
for (int columnIndex = 0; columnIndex < metaData.getColumnCount(); columnIndex++)
columnNames[columnIndex] = metaData.getColumnLabel(columnIndex + 1);
reserveMemory();
done = false;
totalRowCount = 0;
}

public void fetchBatch() throws SQLException {
Expand Down Expand Up @@ -234,7 +256,6 @@ public void fetchBatch() throws SQLException {
trySettingAutoCommit(true);
break;
}

}
totalRowCount += rowCount;
}
Expand All @@ -246,8 +267,6 @@ public void clear() {
byteBuffer = null;
} catch (SQLException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}

Expand Down

0 comments on commit 4ff0177

Please sign in to comment.