简体   繁体   English

从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作

[英]@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. 简而言之,只需在类或测试方法上放置@CommitRollback(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 a PlatformTransactionManager bean in the ApplicationContext 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.

相关问题 使用 @Transactional(propagation = Propagation.REQUIRES_NEW) 注释的服务方法的 Spring Boot 集成测试 - Spring Boot Integration Test for Service method annotated with @Transactional(propagation = Propagation.REQUIRES_NEW) 从服务类中调用时,Spring @Transactional不适用于带注释的方法 - Spring @Transactional doesn't work for an annotated method when called from within service class 如果在内部@Spring注释了方法,则Spring @Transactional注释方法是否会覆盖@Transactional(readOnly = true)方法? - Does a Spring @Transactional annotated method overwrite an @Transactional(readOnly=true) method if called within it? Spring Boot 使用@Transactional 注解方法,调用其他事务性方法并抛出异常 - Spring Boot With @Transactional annotated method, calls other transactional methods and throws an Exception 从另一个@Transactional注释方法调用@Transactional注释方法 - Call @Transactional annotated method from another @Transactional annotated method 从@MvcTest注释测试调用时,Spring Boot Controller的行为有所不同 - Spring Boot Controller differs in behaviour when called from @MvcTest annotated test Spring 为在 @Transactional 注释方法中调用的每个 JpaRepository 方法打开一个新事务 - Spring opens a new transaction for each JpaRepository method that is called within an @Transactional annotated method 从 Spring Boot 中具有 @transactional 注释的方法调用时,@Cacheable 不创建缓存键 - @Cacheable not creating cache keys when called from a method having @transactional annotation in spring boot Spring AOP:具有@Transactional注释方法的方法的注释切入点? - Spring AOP : Annotated pointcuts for a method with @Transactional annotated method? Spring Boot 测试@Transactional 不保存 - Spring boot test @Transactional not saving
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM