[英]@Caching method called from spring boot test [annotated with @Transactional] not working
I'm using redis caching and spring boot annotations[@Cacheable and @CahePut], I made RedisManager transactionAware, which will use the outer transaction[callee of caching layer] 我正在使用Redis缓存和Spring Boot批注[@Cacheable和@CahePut],我做了RedisManager transactionAware,它将使用外部事务[缓存层的被调用者]
@Bean
public RedisCacheManager cacheManager() {
RedisCacheManager rcm =
RedisCacheManager.builder(redisConnectionFactory())
.cacheDefaults(cacheConfiguration())
.transactionAware()
.build();
return rcm;
}
while testing as below, I'm using embedded redis-: 在进行如下测试时,我正在使用嵌入式redis-:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase
@Transactional
public class RoleServiceImplTest extends TestingProfile {
@Before
public void setup() throws Exception {
//setup server and services
redisServer = new RedisServer(redisPort);
redisServer.start();
}
@Test
public void getUsersForRoleForTemplateRole() {
// call to caching layer methods directly annotated with @Cachable
}
... Both times [ with and without @Transactional ] spring calls cache.put(key,result)
without exception but it only persists values in case of without @Transactional. ...两次[带有和不带有@Transactional] spring都无例外地调用
cache.put(key,result)
,但是仅在没有@Transactional的情况下才持久保存值。
Couldn't find much on internet, kudos to any help in advance. 无法在互联网上找到太多东西,因此无法获得任何帮助。
In short just put @Commit
or Rollback(false)
annotation over your class or test method. 简而言之,只需在类或测试方法上放置
@Commit
或Rollback(false)
批注。
Spring by default rollback every Transaction after the test method. 默认情况下,Spring在测试方法之后回滚每个Transaction。
https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-tx https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-tx
In the TestContext framework, transactions are managed by the
TransactionalTestExecutionListener
, which is configured by default, even if you do not explicitly declare@TestExecutionListeners
on your test class.在TestContext框架中,事务由
TransactionalTestExecutionListener
进行管理,该TransactionalTestExecutionListener
是默认配置的,即使您没有在测试类上显式声明@TestExecutionListeners
也不@TestExecutionListeners
。 To enable support for transactions, however, you must configure aPlatformTransactionManager
bean in theApplicationContext
that is loaded with@ContextConfiguration
semantics (further details are provided later).但是,要启用对事务的支持,必须在
ApplicationContext
中配置使用@ContextConfiguration
语义加载的PlatformTransactionManager
bean(稍后将提供更多详细信息)。 In addition, you must declare Spring's@Transactional
annotation either at the class or the method level for your tests.另外,您必须在测试的类或方法级别声明Spring的
@Transactional
批注。
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/transaction/TransactionalTestExecutionListener.html https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/transaction/TransactionalTestExecutionListener.html
Declarative Rollback and Commit Behavior
声明式回滚和提交行为
By default, test transactions will be automatically rolled back after completion of the test;
默认情况下,测试事务将在测试完成后自动回滚; however, transactional commit and rollback behavior can be configured declaratively via the
@Commit
and@Rollback
annotations at the class level and at the method level.但是,可以在类级别和方法级别通过
@Commit
和@Rollback
批注声明性地配置事务提交和回滚行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.