Results 1 to 10 of 10

Thread: Transaction aspect order

  1. #1
    Join Date
    Aug 2004
    Posts
    10

    Default 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. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

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

  3. #3
    Join Date
    Aug 2004
    Posts
    10

    Default

    Unfortunately not...

  4. #4
    Join Date
    Mar 2007
    Posts
    515

    Default

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

  5. #5
    Join Date
    Aug 2004
    Posts
    10

    Default

    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?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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"/>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Aug 2004
    Posts
    10

    Default

    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?

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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 at 03:16 PM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Aug 2004
    Posts
    10

    Default

    Quote 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).

  10. #10
    Join Date
    Aug 2004
    Posts
    10

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •