Announcement Announcement Module
Collapse
No announcement yet.
Conditionally applying aspects (ehcache + transactions) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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, 10:40 AM.

      Comment


      • #4
        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, 10:41 AM.

        Comment


        • #5
          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 .

          Comment


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

            Comment

            Working...
            X