Announcement Announcement Module
Collapse
No announcement yet.
Transaction aspect order Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction aspect order

    I'm using Spring 2.0 declarative transactions with <tx:annotation-driven/> and want an interceptor to be called *before* commit.

    The problem is that the "order" property of the transaction aspect is Integer.MAX_VALUE so I can't use a higher value in my aspect.

    As a workaround I'm also using Integer.MAX_VALUE and it is actually being called before commit, but I'm worried if it is just by luck.

    And what if I have more aspects that must be ordered before commit? Is it possible to change the order property of transaction aspect? The assumption that nothing will intercept after "begin transaction" and before "commit" is too restrictive...

  • #2
    Doesn't <tx:annotation-driven/> have an 'order' attribute, also, to adjust the order aspects are applied ?

    Comment


    • #3
      Unfortunately not...

      Comment


      • #4
        'order' attribute has been added starting with Spring version 2.0.3.
        Take a look at this thread for more details.

        Comment


        • #5
          Thanks! I didn't noticed it before, my IDE was lying to me...

          My problem now is how to put the aspect order from a static field into an attribute! If only it was a property...

          It seems that I will need to convert tx:annotation-driven into a explicit transaction configuration with advices, attributes, etc... Or there is another way?

          Comment


          • #6
            Why? simply specify an order attribute on the annotation-driven tag. If you want it to be referenced from a static from inside a class use a ConstantsBean. Check the util:constant namespace for that.

            Code:
            <tx:annotation-driven order="1"/>

            Comment


            • #7
              Yes, I can use <util:constant> to set a bean *property* not an *attribute*!

              So the code below is not valid:

              <util:constant id="txAspectOrder" static-field="SystemPointcuts.TRANSACTION_ASPECT_ORDER"/>

              <tx:annotation-driven transaction-manager="txManager" order="txAspectOrder"/>

              The element tx:annotation also don't alow children elements so the fragment below is also not valid:

              <tx:annotation-driven transaction-manager="txManager">
              <property name="order" ref="txAspectOrder"/>
              </tx:annotation-driven>

              So, the order attribute must be hardcoded in the xml (unless I don't use annotation-driven).

              My suggestion would be to add an "order" property to tx:annotation-driven... It would be far more flexible.

              Any ideas?

              Comment


              • #8
                What is the difference in specifing the hardcoded value from a static-field or hardcode it in the xml?

                However you can bind everything together with a PropertyPlaceHolderConfigurer.

                Code:
                <bean class="PropertyPlaceHolderConfigurer">
                   <property name="properties">
                      <props>
                         <prop key="txOrder" value-ref="txAspectOrder"/>
                      </props>
                   </property>
                </bean>
                
                <tx:annotation-driven transaction-manager="txManager" order="${txOrder}"/>
                So, the order attribute must be hardcoded in the xml (unless I don't use annotation-driven).
                No it doesn't have to be hardcoded...

                From your first post

                As a workaround I'm also using Integer.MAX_VALUE and it is actually being called before commit, but I'm worried if it is just by luck.
                Order is from 1 to higher.... The one with order 1 is executed before order 2 etc. the transaction will ALWAYS be called last... Read the documentation it is al in there, Ordered. So unless you want the Transaction to commit earlier you should specify a lower order, else leave everything as is.
                Last edited by Marten Deinum; Jun 25th, 2007, 03:16 PM.

                Comment


                • #9
                  Originally posted by mdeinum View Post
                  What is the difference in specifing the hardcoded value from a static-field or hardcode it in the xml?
                  I have many aspects that must be executed at a specified order, so I put the order in static constants, I don't want that information scattered around many xml files...

                  Using a properties file don't solve the problem as my custom aspects implement Ordered interface (yes, I could read the properties file, but it would be cumbersome).

                  Comment


                  • #10
                    To conclude this thread, I resolved it this way:

                    Changed from

                    Code:
                    <tx:annotation-driven transaction-manager="txManager" order="2000000000"/>
                    to

                    Code:
                    <util:constant id="txAspectOrder" static-field="SystemPointcuts.TRANSACTION_ASPECT_ORDER"/>
                    
                    <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
                        <property name="transactionInterceptor" ref="transactionInterceptor"/>
                        <property name="order" ref="txAspectOrder"/>
                    </bean>
                    
                    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
                        <property name="transactionManager" ref="txManager"/>
                        <property name="transactionAttributeSource">
                            <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
                        </property>
                    </bean>
                    so the transaction aspect order can be defined by a constant in a Java class along with the order of all other custom aspects.

                    Comment

                    Working...
                    X