Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EhCacheCacheManager does not wrap runtime-registered caches with TransactionAwareCacheDecorator [SPR-11407] #16034

Closed
spring-projects-issues opened this issue Feb 9, 2014 · 4 comments

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 9, 2014

Christoph Strobl opened SPR-11407 and commented

EhCacheManager fails to return a decorated Cache when requesting a Cache that has been added at runtime, for the first time, from the backing CacheManager. This causes values to be added to the cache in case the transaction is rolled back. Any subsequent operations will work fine off the second request EhCacheManager returns the properly decorated instance.

The test listed below reproduces the scenario.

  • testValuesShouldNotBeAddedToCacheWhenCacheRequestedForFirstTimeAndTransactionIsRolledBack fails as the values are added to the cache though they should not have been.
  • testValuesShouldNotBeAddedToCacheWhenCacheRequestedSeveralTimesAndTransactionIsRolledBack succeeds as the cache was requested one time before using it within the transaction.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager")
public class TransactionalEhCacheManagerUnitTests {

  // some transactional service where caching is used somewhere
  private @Autowired FooService transactionalService;

  // spring cacheManager
  private @Autowired CacheManager cacheManager;

  // backing EhCacheManager used to add Cache at runtime and get cache statistics
  private @Autowired net.sf.ehcache.CacheManager ehCacheManager;

  @Configuration
  @EnableCaching
  public static class Config {

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {

      DataSourceTransactionManager txmgr = new DataSourceTransactionManager();
      txmgr.setDataSource(dataSource());
      txmgr.afterPropertiesSet();

      return txmgr;
    }

    @Bean
    public DataSource dataSource() throws SQLException {

      DataSource dataSourceMock = mock(DataSource.class);
      when(dataSourceMock.getConnection()).thenReturn(mock(Connection.class));

      return dataSourceMock;
    }

    @Bean
    public CacheManager cacheManager() {

      EhCacheCacheManager cacheManager = new EhCacheCacheManager();
      cacheManager.setCacheManager(ehCacheManager());
      cacheManager.setTransactionAware(true);

      return cacheManager;
    }

    @Bean
    public net.sf.ehcache.CacheManager ehCacheManager() {
      return net.sf.ehcache.CacheManager.newInstance();
    }

    @Bean
    public FooService fooService() {
      return new FooService();
    }

    @Bean
    public BarRepository barRepository() {
      return new BarRepository();
    }
  }

  @AfterTransaction
  public void after() {
    assertThat(ehCacheManager.getCache("bar").getStatistics().getSize(), equalTo(0L));
  }

  /**
   * If the {@link Cache} is added at runtime the cache manager should use {@link TransactionAwareCacheDecorator} to
   * decorate cache and use the transaction aware one for caching operations.
   */
  @Rollback(true)
  @Test
  public void testValuesShouldNotBeAddedToCacheWhenCacheRequestedForFirstTimeAndTransactionIsRolledBack() {

    ehCacheManager.addCache("bar");
    transactionalService.foo();
  }

  @Rollback(true)
  @Test
  public void testValuesShouldNotBeAddedToCacheWhenCacheRequestedSeveralTimesAndTransactionIsRolledBack() {

    ehCacheManager.addCache("bar");
    // get the cache explicitly one time so that the next call will get the decorated cache instance
    cacheManager.getCache("bar");

    transactionalService.foo();
  }

  static class FooService {

    private @Autowired BarRepository repo;

    @Transactional
    public String foo() {
      return "foo" + repo.bar();
    }
  }

  static class BarRepository {

    @Cacheable("bar")
    public String bar() {
      return "bar";
    }

  }
}

The same issue should exist with JCacheManager.


Affects: 3.2.7, 4.0.1

Issue Links:

  • #16143 Properly wrap runtime-registered caches with TransactionAwareCacheDecorator

Referenced from: commits ef1748f, 42dec02

Backported to: 3.2.8

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2014

Stéphane Nicoll commented

Christoph, I tried to run your sample but I am wondering which ehcache version you are using. getStatistics returns a snapshot and not the most accurate data by default. getLiveStatistics does. I changed your test to use that and both are passing for me with 4.0.1.RELEASE (not at the same time because it complains the cache "bar" was already added).

Have you noticed the issue outside your unit test?

Thanks!

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2014

Christoph Strobl commented

Hi Stéphane.

I was using ehcache:2.8.1 and came across the issue when working on RedisCacheManager.

Howerver the Cache returned when calling EhCacheManager.getCache("bar") should always return TransactionAwareCacheDecorator in case transactionAware is true. In first tests this is not the case, at least not as far as I experienced.

Cheers, Christoph.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 11, 2014

Stéphane Nicoll commented

PR available #461

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 14, 2014

Juergen Hoeller commented

Fixed with a minimal-impact change for the time being, based on the algorithm in the pull request. In particular with respect to 3.2.8 there, I opted for the simplest possible solution. However, for 4.1, let's revisit the approach in the pull request and introduce a proper template method arrangement for it.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants