Class TxResult
java.lang.Object
dmx.fun.quarkus.TxResult
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<V,E> Result <V, E> Executesactioninside a new JTA transaction.<V,E> Result <V, E> executeNew(Supplier<Result<V, E>> action) Executesactionin a brand-new JTA transaction (REQUIRES_NEW semantics), suspending any currently active transaction first and resuming it afterwards.
-
Constructor Details
-
TxResult
protected TxResult()
-
-
Method Details
-
execute
Executesactioninside 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 typeE- the error type- Parameters:
action- the transactional action; must not benulland must not returnnull- Returns:
- the
Resultreturned byaction - Throws:
NullPointerException- ifactionisnullor returnsnull
- the action returns
-
executeNew
Executesactionin a brand-new JTA transaction (REQUIRES_NEW semantics), suspending any currently active transaction first and resuming it afterwards.- Type Parameters:
V- the success value typeE- the error type- Parameters:
action- the transactional action; must not benulland must not returnnull- Returns:
- the
Resultreturned byaction - Throws:
NullPointerException- ifactionisnullor returnsnull
-