Announcement Announcement Module
No announcement yet.
Conditionally applying aspects (ehcache + transactions) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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...


    • #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 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:

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

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


      • #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:

        <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, 09:41 AM.


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

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

          Edit: You already figured that out yourself .


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