6
I’m having trouble trying to "inherit" the transaction from Java EE of my layer of service to the layer of repository. My intention is for the transaction to target my service function. Enabling me to do rollback if you have an error in your execution.
However, when I define the @Transactional
of repository as Mandatory
:
- Returns a error saying that no active transaction exists
When I leave no parameter, default REQUIRED
:
- The repository creates a new transaction when an error occurs
- Won’t let me do rollback of the modifications
How to send the transaction and allow to do rollback without problems using the @Transacional
?
Service class.
@Inject
@InstanciaInject
MandadoRepositorio mandadoRepositorio;
@Transactional(rollbackOn = RuntimeException.class,
value = Transactional.TxType.REQUIRES_NEW)
protected void metodoPrincipal() {
String idPessoa = cookie.getIdPessoa().toString();
mandadoPagRepositorio.alterarSituacaoMandadoBB(codigoDocumento, idPessoa);
}
Repositorio.class
public class MandadoRepositorio {
@Transactional(Transactional.TxType.MANDATORY)
public int alterarSituacaoMandadoBB(String param1, String param2) {
return criarProcedure("PROCEDURE_NAME")
.registrarInputOpcionalComValorPorNome("codigo", param1)
.registrarInputOpcionalComValorPorNome("id", param2)
.executeUpdate()
.getUpdateCount();
}
}
POM.xml
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
<version>1.2.2</version>
</dependency>
Editing:
- The class has been updated Mandadorepositorio.class
- No use of Annotations
@Stateless
or@Statefull
of the EJB
could you kindly include your class calling the repository with the Imports? And any statements if there are any (
@Stateless
,@TransactionManagement
...)– nullptr
@nullptr I tried to do the editing with as much detail as possible, just modifying the names of the methods and class. All annotations are duly recorded.
– Victor Henrique