Announcement Announcement Module
Collapse
No announcement yet.
Unable to rollback transaction using Hibernate + aop Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!
    Last edited by vamoelbolso; Feb 5th, 2011, 11:25 AM.

  • #2
    Is there any typo mistake in your code ?
    look at the exception package. They are different as defined in transaction advice and actual exception stack trace.


    com.integra.exepciones.LogicaException
    com.integra.excepciones.LogicaException

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="*" propagation="REQUIRED"
    rollback-for="com.integra.exepciones.LogicaException"/>
    </tx:attributes>
    </tx:advice>

    com.integra.excepciones.LogicaException
    at com.integra.logica.negocio.LogicaUsuario.altaUsuar ioDT(LogicaUsuario.java:99)
    at com.integra.servicios.implementacion.ServiciosUsua riosImp.altaUsuarioDT(ServiciosUsuariosImp.java:27 )
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:182)

    Comment


    • #3
      Thanks Rohand.
      That was just a mistake when pasting the code to the thread.
      The actual code has no typo, sorry about that.

      Comment


      • #4
        If you use a MySQL database make sure to use transactional tables (InnoDB) else tx is pretty much useless.

        Post the configuration for hibernate and also note that it isn't recommended to use HibernateDaoSupport or HibernateTemplate anymore... (Search the forum for why I answered that question numerous times before).

        Comment


        • #5
          Hi Marten, thank you very much for your response.
          I was in fact using MyIsam as my storage engine, I have now tried with InnoDB and transactions are working correctly.
          Again thanks for your help, you saved the day!

          Regards.

          Comment

          Working...
          X