I have a project where I'm using JPA with JTA. In the gravar
method, I put the @Transactional(Transactional.TxType.REQUIRED)
annotation and there is a call to a validation method that can throw an exception called ValidacaoException
that inherits from RuntimeException
.
In the documentation I saw that when the exception is of type RuntimeException
, the container will do the rollback of the transaction. And in my case it really is, except that this salvar
method is being called inside a ManagedBean and in try / catch, the exception that is caught is TransactionalException
and it does not have the message that I put in the validation.
How do I get the original message?
Below is the code. Class ContaServicoImpl
:
private void validarGravar(Conta conta) {
List<String erros = new ArrayList<();
if (conta.getDescricao() == null || conta.getDescricao().trim().length() < 3) {
erros.add("Descrição é obrigatória ter ao menos 3 caracteres");
}
if (conta.getEmissao() == null) {
erros.add("Emissão é obrigatória");
}
if (conta.getVencimento() == null) {
erros.add("Vencimento é obrigatório");
}
if (conta.getEmissao() != null
&& conta.getVencimento() != null
&& conta.getVencimento().before(conta.getEmissao())) {
erros.add("Vencimento tem que ser igual ou superior a data de emissão");
}
if (conta.getValor() == null) {
conta.setValor(BigDecimal.ZERO);
}
if (conta.getValor().doubleValue() < 0.0) {
erros.add("O valor não pode ser negativo");
}
if (conta.getSaldo().doubleValue() < 0.0) {
erros.add("Total liquidado é superior ao valor da conta");
}
for (Liquidacao l : conta.getLiquidacoes()) {
if (l.getLiquidacao().before(conta.getEmissao())) {
erros.add("Existe(m) liquidação(ões) com data anterior a data de emissão da conta");
break;
}
}
if (!erros.isEmpty()) {
StringBuilder sb = new StringBuilder("Validação: \n");
for (String s : erros) {
sb.append(s).append("\n");
}
throw new ValidacaoException(sb.toString());
}
}
@Override
@Transactional(Transactional.TxType.REQUIRED)
public void gravar(Conta conta, List<Liquidacao liquidacoesRemovidas) {
validarGravar(conta);
contaDao.gravar(conta);
conta.getLiquidacoes().stream().forEach((l) - {
liquidacaoDao.gravar(l);
});
liquidacoesRemovidas.stream().forEach(l - {
liquidacaoDao.remover(l);
});
}
Class ContaEdicaoMB
:
public String gravar() {
try {
contaServico.gravar(conta, liquidacaoRemovidas);
JsfUtil.mensagemInformacao("Gravado com sucesso", "Gravado com sucesso");
} catch (TransactionalException ex) {
JsfUtil.mensagemErro("Falha ao gravar", ex.getMessage());
return null;
}
return "filtro?faces-redirect=true";
}
The error obtained:
Informações: In REQUIRED TransactionalInterceptor
Informações: Managed bean with Transactional annotation and TxType of REQUIRED called outside a transaction context. Beginning a transaction...
Informações: About to setRollbackOnly from @Transactional interceptor on transaction: JavaEETransactionImpl: txId=1 nonXAResource=null jtsTx=null localTxStatus=0 syncs=[]
Informações: Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit {0}
javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:445)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
at com.sun.enterprise.transaction.TransactionManagerHelper.commit(TransactionManagerHelper.java:81)
at org.glassfish.cdi.transaction.TransactionalInterceptorRequired.transactional(TransactionalInterceptorRequired.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:74)
at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeAroundInvoke(InterceptorMethodHandler.java:84)
at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:72)
at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:56)
at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:79)
at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:68)
at br.com.tebosoftware.financeiro.servico.ContaServicoImpl$Proxy$_$$_WeldSubclass.gravar(Unknown Source)
at br.com.tebosoftware.financeiro.servico.ContaServicoImpl$Proxy$_$$_WeldClientProxy.gravar(Unknown Source)
at br.com.tebosoftware.financeiro.managedBean.conta.ContaEdicaoMB.gravar(ContaEdicaoMB.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.el.ELUtil.invokeMethod(ELUtil.java:332)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:283)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)