Unable to rollback transaction using Hibernate + aop
Hi guys, this is my first time here, it would be just great if you guys could help me out. I am having a problem with my configuration, somehow I canīt manage to rollback my transactions, but everything seems to be fine to me.
My application is separated in 3 layers: Web Layer, Service Layer (Interfaces and Implementations), and Persistence layer (DAO extending HibernateDAOSupport). What I am trying to achieve is to put transactions around the service objects/methods, not the DAO.
I am pasting some code, configuration and console output to see if you guys can find out what is wrong:
Code:
<!-- ======Transaction config in applicationContext.xml ====== -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"
rollback-for="com.integra.excepciones.LogicaException"/>
</tx:attributes>
</tx:advice>
<!-- AOP proxy configuration: the target is the interface, is that correct?
-->
<aop:config>
<aop:pointcut id="operacionesServicios"
expression="execution(* com.integra.servicios.interfaces.IServicios*.*(..))" />
<aop:advisor pointcut-ref="operacionesServicios" advice-ref="txAdvice" />
</aop:config>
This is the intervace and the method declaration that throws LogicaException:
Code:
public interface IServiciosUsuarios {
public void altaUsuario(Usuario usuario,String equipo)throws LogicaException;
}
I have an Implementation of that Interface that calls the service method.
Code:
]
public class ServiciosUsuariosImp implements IServiciosUsuarios {
//This is injected by Spring using wire byName strategy.
private LogicaUsuario logicaUsuario;
public void altaUsuario(Usuario usuario,String equipo) throws LogicaException {
this.logicaUsuario.altaUsuario(usuario,nombreEquipo);
}
}
Here its the code that causes the exception:
Code:
public class LogicaUsuario {
public void altaUsuario(Usuario usuario, String equipo)throws LogicaException {
try {
//do some stuff
usuario.setEquipo(equipo);
//Call DAO method and saves the user.
this.usuarioDAO.saveUsuario(usuario);
//Now I am forcing the exception to be thrown
usuario=null;
System.out.println(usuario.getNombre());
} catch (Exception e) {
e.printStackTrace();
//Catch and throws the Exception that is configured in app context.
//It is suppose to rollback when this exception is called but the record
//is saved anyway.
throw new LogicaException(e.getMessage());
}
}
}
And finally here itīs my stack trace. You can see that aop proxy is working well and that the exception is effectively thrown but somehow the record is stored despites the Layerīs Exception.
Code:
java.lang.NullPointerException
at com.integra.logica.negocio.LogicaUsuario.altaUsuario(LogicaUsuario.java:93)
at com.integra.servicios.implementacion.ServiciosUsuariosImp.altaUsuario(ServiciosUsuariosImp.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy8.altaUsuarioDT(Unknown Source)
at com.integra.controller.RegistrarUsuarioAction.registrarUsuario(RegistrarUsuarioAction.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.lang.Thread.run(Unknown Source)
com.integra.excepciones.LogicaException
at com.integra.logica.negocio.LogicaUsuario.altaUsuario(LogicaUsuario.java:99)
at com.integra.servicios.implementacion.ServiciosUsuariosImp.altaUsuario(ServiciosUsuariosImp.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy8.altaUsuarioDT(Unknown Source)
at com.integra.controller.RegistrarUsuarioAction.registrarUsuario(RegistrarUsuarioAction.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
I hope that I was clear enough and that all relevant information has been posted. Sorry for the Spanish source code but I think that you guys wonīt have trouble becouse of this. I have been working with this and googling around for the last two days so itīs honest to say that you guys are my last resort !.
Any help will be greatly appreciated...thanks in advance.
Regards!