Announcement Announcement Module
Collapse
No announcement yet.
Problems when using transactions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems when using transactions

    hi to all,

    we have an automatic process to execute some database updates and file translation every 30 minuts. We need a backoffice to give the opportunity to the user of forcing the automatic process when he needs. In order to avoid collisions between different threads when processing files, we implement a simple synchronization mechanism:

    1) The automatic process always checks a value on the database, if the value says "turn off", then it starts de process, otherwise it waits until de value changes from "turn off" to "turn on".

    2) The manual process(the one which is called from the backoffice), always switch the value to "turn off" and does the process. Obviously, when it finishes, it changes the value to "turn on".

    Specifically, the manual process does:

    1) call to switch(turn off)
    2) call to doWork() (does some file translation and updates the database)
    3) call to switch(turn on)

    Each of these steps are madden into different methods and called sequentially. Our application is written using Hibernate for persistence and Spring for transactions management.

    Our springapp-transaction.xml file:

    <beans>
    <bean id="txProxyTemplate" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean" abstract="true">
    <property name="transactionManager" ref="myTransactionManager" />
    <property name="transactionAttributes">
    <props>
    <prop key="doWork*">PROPAGATION_REQUIRED, + ourException</prop>
    <prop key="switch*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
    </props>
    </property>
    </bean>
    </beans>

    We need to commit after each method and do not propagate transactions between calls.
    It is more complicated even. Sometimes, a custom exception may be thrown by some function called into the doWork() method. In those situations, the doWork() method needs to catch the exception, update the database and throw up the exception(for the controller be aware of the situation and notify to the user).

    Eventually, when a custom exception is throwned, the transactionInterceptor tries to commit but throws an exception:

    2010-05-25 19:22:01,560 ERROR [org.springframework.transaction.interceptor.Transa ctionInterceptor] - <Application exception overridden by commit exception>
    es.bcn.smc.bo.exception.SMCModelException
    at es.bcn.smc.bo.xslengine.XSLEngineBOImpl.executeXml Xsl(XSLEngineBOImpl.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:310)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:182)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :149)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:106)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy3.executeXmlXsl(Unknown Source)

    .......



    If we remove, the "+ ourException" option to force rollback when any exception is throwed then we get the exception:

    2010-05-25 18:39:13,890 ERROR [es.bcn.smc.web.exceptionHandler.GenericExceptionHa ndler] - <org.hibernate.HibernateException: Found two representations of same collection: es.bcn.smc.dto.EntornProducteDto.detalleCopiesEnto rnProducte
    org.hibernate.engine.Collections.processReachableC ollection(Collections.java:153)
    org.hibernate.event.def.FlushVisitor.processCollec tion(FlushVisitor.java:37)
    org.hibernate.event.def.AbstractVisitor.processVal ue(AbstractVisitor.java:101)
    org.hibernate.event.def.AbstractVisitor.processVal ue(AbstractVisitor.java:61)
    org.hibernate.event.def.AbstractVisitor.processEnt ityPropertyValues(AbstractVisitor.java:55)
    org.hibernate.event.def.DefaultFlushEntityEventLis tener.onFlushEntity(DefaultFlushEntityEventListene r.java:124)
    org.hibernate.event.def.AbstractFlushingEventListe ner.flushEntities(AbstractFlushingEventListener.ja va:195)
    org.hibernate.event.def.AbstractFlushingEventListe ner.flushEverythingToExecutions(AbstractFlushingEv entListener.java:76)
    org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:26)
    org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:985)
    org.hibernate.impl.SessionImpl.managedFlush(Sessio nImpl.java:333)
    org.hibernate.transaction.JDBCTransaction.commit(J DBCTransaction.java:106)
    org.springframework.orm.hibernate3.HibernateTransa ctionManager.doCommit(HibernateTransactionManager. java:606)
    org.springframework.transaction.support.AbstractPl atformTransactionManager.processCommit(AbstractPla tformTransactionManager.java:709)
    org.springframework.transaction.support.AbstractPl atformTransactionManager.commit(AbstractPlatformTr ansactionManager.java:678)
    org.springframework.transaction.interceptor.Transa ctionAspectSupport.commitTransactionAfterReturning (TransactionAspectSupport.java:321)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:116)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    $Proxy27.turnOff(Unknown Source)
    es.bcn.smc.web.actions.smc.SmcLlistatAction.genera te(SmcLlistatAction.java:252)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.struts.actions.DispatchAction.dispatchM ethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(D ispatchAction.java:170)
    org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:425)
    org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:228)
    org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    es.bcn.smc.web.filter.GenericDataFilter.doFilter(G enericDataFilter.java:40)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    es.bcn.smc.web.filter.SecurityFilter.doFilter(Secu rityFilter.java:38)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:75)
    org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235)
    org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:852)
    org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:619)

    As you can see, we are not able to find the solution. We have tried too doing all the operations inside a method and trying to say "this method is not transactional"... but without lucky... Could you help us?
    Thanks.
Working...
X