Results 1 to 6 of 6

Thread: Conditionally applying aspects (ehcache + transactions)

  1. #1
    Join Date
    Jul 2011
    Posts
    4

    Default Conditionally applying aspects (ehcache + transactions)

    Hi all,
    I'm using EHCache to cache some methods in my service. When executing those methods, if EHCache finds their results in its cache, should *NOT* be transactional. Instead, if EHCache does not find their results in the cache, they *SHOULD* be transactional.
    The fact is that when applying aspects (using @Transactional and @Cacheable annotations), what is really happening is that my classes are being wrapped (proxied), and so when I'm applying two aspects they are proxied twice, that is, first a proxy is created around my bean and then another proxy on the first one. So: isn't there a way to tell one of them to apply its wrapping code only if the first one didn't apply (in configuration, not touching code)? Or even to tell one of them not to proxy my bean at all?
    I think it would be desirable to provide the proxies with something like a chain of responsibility or similar, to propagate the proxied behavior depending on the result of previous links in the chain.
    Thank you in advance

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Add an order, let the caching execute BEFORE the transaction and you get what you want...
    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

  3. #3
    Join Date
    Jul 2011
    Posts
    4

    Default

    Thank you Marten, but:

    How can I achieve that? I mean, I'm using annotation driven management for both transactions and EHCache. So, I didn't implement any Aspect nor class annotated with @Aspect by myself. I'm using @Transactional and @Cacheable (that from http://code.google.com/p/ehcache-spring-annotations project) annotations, and they don't support anything about orders, and neither their XML configurations seem to do. Where can I set their orders? My EHCache configuration looks like follows:

    Code:
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        	<property name="configLocation" value="classpath:ehcache.xml"/>
    	</bean>
    
    	<ehcache:config cache-manager="ehCacheManager">
            <ehcache:evict-expired-elements interval="60" />
        </ehcache:config>
    ... and my transactions management configuration:

    Code:
    <bean id="postgresTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="postgresEntityManagerFactory" />
    	</bean>
    	<tx:annotation-driven transaction-manager="postgresTransactionManager" />
    That's all I have. Thank you in advance (again)
    Last edited by sonxurxo; Jul 19th, 2011 at 09:40 AM.

  4. #4
    Join Date
    Jul 2011
    Posts
    4

    Default

    Ok, I'm done!
    Just for anyone that can read this in the future, here's the solution (thank you Marten!). Put in your transaction management and EHCache configuration sections:

    Code:
    <tx:annotation-driven transaction-manager="postgresTransactionManager" order="2"/>
    
    ........
    
    <ehcache:annotation-driven cache-manager="ehCacheManager" order="1"/>
    Explanation: apply the order (less is priority) and that's all, that's the place where it has to be. Thank you again Marten!
    Last edited by sonxurxo; Jul 19th, 2011 at 09:41 AM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use [ code][/code ] tags when posting code/xml ...

    As stated provide an order (and I was under the impression you used spring 3.1 with the build in cache support).

    Code:
    <ehcache:config cache-manager="ehCacheManager" order="1">
    <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <tx:annotation-driven transaction-manager="postgresTransactionManager" order="2" />
    Judging from their sources it is supported...

    Edit: You already figured that out yourself .
    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

  6. #6
    Join Date
    Jul 2011
    Posts
    4

    Default

    Sorry about the use of tags, I fixed it. Thank you again

Tags for this Thread

Posting Permissions

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