Aklakan commented on code in PR #3027:
URL: https://github.com/apache/jena/pull/3027#discussion_r2081618510


##########
jena-arq/src/main/java/org/apache/jena/system/Txn.java:
##########
@@ -137,4 +139,46 @@ private static <T extends Transactional> void 
onThrowable(Throwable th, T txn) {
             txn.end();
         } catch (Throwable th2) { th.addSuppressed(th2); }
     }
+
+    /**
+     * Begins a transaction and returns a transaction control instance suitable
+     * for use with try-with-resources blocks.
+     * See {@link #begin(Transactional, TxnType)}.
+     */
+    public static AutoTxn begin(Transactional txn, ReadWrite readWrite) {
+        return begin(txn, TxnType.convert(readWrite));
+    }
+
+    /**
+     * Begins a transaction and returns a transaction control instance suitable
+     * for use with try-with-resources blocks.
+     * Allows for raising checked exceptions in an idiomatic way.
+     * Closing the TxnCtl instance will abort the transaction unless it
+     * has been manually committed.
+     * <p>
+     *
+     * Usage example:
+     * <pre>
+     * public void myMethod() throws IOException {
+     *   try (AutoTxn txn = Txn.begin(dataset, TxnType.WRITE)) {
+     *     // Do work
+     *     if (someError) {
+     *         throw new IOException();
+     *     }
+     *     // Must manually call commit on success.
+     *     txn.commit();

Review Comment:
   When `AutoTxn.close()` is called by the try-with-resources block, then there 
seems to be no standard way for close to detect whether it is called after 
normal or exceptional block exit. Therefore the logic is that always 
exceptional block exit is assumed (-> rollback) and commit() has to be called 
manually.
   It might be a bit counterintuitive but I think the price is still acceptable 
for being able to manage txns with try-with-resources and raise checked 
exceptions.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org
For additional commands, e-mail: pr-h...@jena.apache.org

Reply via email to