Class TxTry

java.lang.Object
dmx.fun.quarkus.TxTry

@NullMarked @ApplicationScoped public class TxTry extends Object
CDI bean that executes a Try-returning action inside a JTA-managed transaction, automatically rolling back when the result represents a failure.

Quarkus's @Transactional rolls back only when an unchecked exception escapes the annotated method. Since Try<V> captures failure as a return value, no exception escapes, and the transaction commits even on failure — silently persisting partial writes. TxTry solves this by inspecting the returned Try: if Try.isFailure() is true, the transaction is rolled back before returning.

Quick start

@ApplicationScoped
public class ReportService {
    @Inject TxTry tx;

    public Try<Report> generate(ReportRequest req) {
        return tx.execute(() ->
            Try.of(() -> reportRepo.save(build(req)))
               .map(r -> { auditLog.record(r); return r; })
        );
    }
}
See Also:
  • Constructor Details

    • TxTry

      protected TxTry()
  • Method Details

    • execute

      public <V> Try<V> execute(Supplier<Try<V>> action)
      Executes action inside a new JTA transaction.

      The transaction commits if the action returns Try.isSuccess(). The transaction is rolled back when:

      • the action returns Try.isFailure(), or
      • the action throws an unchecked exception (propagates to the caller).
      Type Parameters:
      V - the success value type
      Parameters:
      action - the transactional action; must not be null and must not return null
      Returns:
      the Try returned by action
      Throws:
      NullPointerException - if action is null or returns null
    • executeNew

      public <V> Try<V> executeNew(Supplier<Try<V>> action)
      Executes action in a brand-new JTA transaction (REQUIRES_NEW semantics), suspending any currently active transaction first and resuming it afterwards.
      Type Parameters:
      V - the success value type
      Parameters:
      action - the transactional action; must not be null and must not return null
      Returns:
      the Try returned by action
      Throws:
      NullPointerException - if action is null or returns null