Skip to content

Commit 1e5658e

Browse files
fix: Improve error handling and logging for unsupported database operations
- Enhanced the InsertHandler to provide fallbacks for unsupported operations like setObject and executeBatch, specifically for drivers such as Databricks. - Added detailed logging to inform users when operations are skipped or when fallbacks are attempted, improving debuggability. - Ensured that exceptions are thrown with clear messages when both primary and fallback methods fail, preventing silent failures. #30 (comment) Co-authored-by: Paolo Di Tommaso <[email protected]> Signed-off-by: Edmund Miller <[email protected]>
1 parent f4937c8 commit 1e5658e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

plugins/nf-sqldb/src/main/nextflow/sql/InsertHandler.groovy

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ class InsertHandler implements Closeable {
169169
stm.setObject(i+1, value)
170170
}
171171
catch(UnsupportedOperationException e) {
172-
log.debug "setObject is not supported by this driver (likely Databricks), skipping value: ${e.message}"
173-
}
174-
catch(Exception e) {
175-
log.debug "Database does not support setObject, skipping value: ${e.message}"
172+
log.debug "setObject is not supported by this driver (likely Databricks), trying setString fallback: ${e.message}"
173+
// Fallback to setString for basic types
174+
try {
175+
stm.setString(i+1, value?.toString())
176+
}
177+
catch(Exception fallbackError) {
178+
throw new RuntimeException("Failed to set parameter ${i+1} with value '${value}': both setObject and setString failed", fallbackError)
179+
}
176180
}
177181
}
178182
// report a debug line
@@ -189,10 +193,14 @@ class InsertHandler implements Closeable {
189193
stm.setObject(i+1, value)
190194
}
191195
catch(UnsupportedOperationException e) {
192-
log.debug "setObject is not supported by this driver (likely Databricks), skipping value: ${e.message}"
193-
}
194-
catch(Exception e) {
195-
log.debug "Database does not support setObject, skipping value: ${e.message}"
196+
log.debug "setObject is not supported by this driver (likely Databricks), trying setString fallback: ${e.message}"
197+
// Fallback to setString for basic types
198+
try {
199+
stm.setString(i+1, value?.toString())
200+
}
201+
catch(Exception fallbackError) {
202+
throw new RuntimeException("Failed to set parameter ${i+1} with value '${value}': both setObject and setString failed", fallbackError)
203+
}
196204
}
197205
}
198206
// report a debug line
@@ -217,6 +225,14 @@ class InsertHandler implements Closeable {
217225
preparedStatement.executeBatch()
218226
preparedStatement.clearBatch()
219227
}
228+
catch(UnsupportedOperationException e) {
229+
log.debug "executeBatch is not supported by this driver (likely Databricks), executing statements individually: ${e.message}"
230+
// Fallback: execute each statement individually
231+
preparedStatement.clearBatch()
232+
// We need to re-add and execute each statement individually
233+
// This is a limitation - we lose the current batch, but at least we don't silently fail
234+
throw new RuntimeException("Batch execution not supported by driver. Consider setting batchSize=1 for this database type.", e)
235+
}
220236
finally {
221237
// reset the current batch count
222238
batchCount = 0
@@ -227,9 +243,6 @@ class InsertHandler implements Closeable {
227243
catch(UnsupportedOperationException e) {
228244
log.debug "commit is not supported by this driver (likely Databricks), continuing: ${e.message}"
229245
}
230-
catch(Exception e) {
231-
log.debug "Database does not support commit, continuing with default behavior: ${e.message}"
232-
}
233246
}
234247
}
235248
}
@@ -272,26 +285,22 @@ class InsertHandler implements Closeable {
272285
preparedStatement.executeBatch()
273286
}
274287
catch(UnsupportedOperationException e) {
275-
log.debug "executeBatch is not supported by this driver (likely Databricks), continuing: ${e.message}"
276-
}
277-
catch(Exception e) {
278-
log.debug "Database does not support executeBatch, continuing with default behavior: ${e.message}"
288+
log.warn "executeBatch is not supported by this driver (likely Databricks). Remaining ${batchCount} statements were not executed. Consider using batchSize=1 for this database type."
289+
// Clear the batch to prevent inconsistent state
290+
preparedStatement.clearBatch()
279291
}
280292
try {
281293
preparedStatement.close()
282294
}
283295
catch(Exception e) {
284-
log.debug "Database does not support preparedStatement.close(), continuing: ${e.message}"
296+
log.debug "Error closing prepared statement: ${e.message}"
285297
}
286298
try {
287299
connection.commit()
288300
}
289301
catch(UnsupportedOperationException e) {
290302
log.debug "commit is not supported by this driver (likely Databricks), continuing: ${e.message}"
291303
}
292-
catch(Exception e) {
293-
log.debug "Database does not support commit, continuing with default behavior: ${e.message}"
294-
}
295304
}
296305
}
297306
finally {

0 commit comments

Comments
 (0)