Class TxResult

java.lang.Object
dmx.fun.quarkus.TxResult

@NullMarked @ApplicationScoped public class TxResult extends Object
CDI bean that executes a Result-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 Result<V, E> captures failure as a return value, no exception escapes, and the transaction commits even on error — silently persisting partial writes. TxResult solves this by inspecting the returned Result: if Result.isError() is true, the transaction is rolled back before returning.

Quick start

@ApplicationScoped
public class OrderService {
    @Inject TxResult tx;

    public Result<Order, OrderError> createOrder(OrderRequest req) {
        return tx.execute(() ->
            validate(req)
                .flatMap(this::persistOrder)
                .flatMap(this::notifyInventory)
        );
    }
}
See Also:
  • Constructor Details

    • TxResult

      protected TxResult()
  • Method Details

    • execute

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

      The transaction commits if the action returns Result.isOk(). The transaction is rolled back when:

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

      public <V,E> Result<V,E> executeNew(Supplier<Result<V,E>> 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
      E - the error type
      Parameters:
      action - the transactional action; must not be null and must not return null
      Returns:
      the Result returned by action
      Throws:
      NullPointerException - if action is null or returns null