3 ms·
If the exception is something you can handle at that function’s scope, just put it inside a try-catch. Otherwise, let it bubble up and let it close the transact
by kaba0 4y ago
If the exception is something you can handle at that function’s scope, just put it inside a try-catch. Otherwise, let it bubble up and let it close the transaction (or however you want to handle that). If anything, I feel it is easier to do with Java than in, say, Haskell though I will add that my experience with Haskell doing db queries is limited.
- lmm 4y agoUsually what you want is to go through the rest of the rows, finish the batch and close the transaction, and then handle all the errored rows at a higher level. "Bubble up by default" works in simple cases but falls apart as soon as you're doing any remotely high-throughput work (which has to mean chunk-at-a-time processing decoupled from the orchestration), and ends up doing more harm than good, IME. And note that in Java it's outright impossible to catch exceptions from a generic expression while preserving their type - you have to copy/paste your functions for each number of possible exception types you want to be able to handle.
- kaba0 4y agoI don’t see why this wouldn’t be performant: void transactionBoundary() { var list = someLongListOfElems; var resultList = List.of(); // a sum type for success and error conditions for (var l : list) { try { resultList.add(process(l)); } catch (SpecificException e) { // add to resulList or errorList or whatever to further process those instances. // You can just use SpecificException’s fields … } } }