-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchesAn issue that has been backported to maintenance branchestype: bugA general bugA general bug
Milestone
Description
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.
testValuesShouldNotBeAddedToCacheWhenCacheRequestedForFirstTimeAndTransactionIsRolledBackfails as the values are added to the cache though they should not have been.testValuesShouldNotBeAddedToCacheWhenCacheRequestedSeveralTimesAndTransactionIsRolledBacksucceeds 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:
- Properly wrap runtime-registered caches with TransactionAwareCacheDecorator [SPR-11518] #16143 Properly wrap runtime-registered caches with TransactionAwareCacheDecorator
Referenced from: commits ef1748f, 42dec02
Backported to: 3.2.8
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchesAn issue that has been backported to maintenance branchestype: bugA general bugA general bug