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