In the context of spring we always need to make sure, that a method with @Transactional
is public and called from the outside of the service class. The reason behind that is documented multiple times in stackoverflow or in spring guides.
With Quarkus there seems to be the same case. Let's take the following example:
@Path("/testing")
public final class TestingResource {
@Inject
JustATestService justATestService;
@GET
@Path("testa")
public Response testA() {
justATestService.doWithPublicTransaction();
return Response.ok().build();
}
@GET
@Path("testb")
public Response testB() {
justATestService.doWithPrivateTransaction();
return Response.ok().build();
}
}
And here is the service class:
@ApplicationScoped
public class JustATestService {
@Inject
TransactionManager tm;
@Transactional
public void doSomethingInTransaction() throws SystemException {
System.out.println("Active transaction: " + tm.getStatus());
}
public void doWithPrivateTransaction() throws SystemException {
System.out.println("Not in a transaction: " + tm.getStatus());
doSomethingInAPrivateMethode();
System.out.println("Still not in a transaction: " + tm.getStatus());
}
@Transactional
private void doSomethingInAPrivateMethode() throws SystemException {
System.out.println("Private method will not start a transaction: " + tm.getStatus());
}
}
The results will be:
testing/testa
will show Status.STATUS_ACTIVE (=0) in the consoletesting/testa
will always show Status.STATUS_NO_TRANSACTION (=6) in the console
Is my assumption right? Do we need to make sure a @Transactional
annotated method needs to be called from the outside of the service to work? Quarkus will behave in this case "like" spring?