为Couchbase应用添加交易管理器。在SpringBoot 2应用中进行Junit测试

问题描述 投票:2回答:1

我有一个SpringBoot 2应用程序,它使用Couchbase作为Spring Data Couchbase的数据库使用

我不想添加事务管理器以在测试中添加注释@Transactional以进行回滚,否则会出现此错误:

java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test: [DefaultTestContext@3bf9ce3e testClass = RequestServiceIntegrationTest, testInstance = com.pxs.rqm.requestcrud.service.RequestServiceIntegrationTest@1ebcfcf1, testMethod = shouldSaveWhenDataIsOK@RequestServiceIntegrationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@16610890 testClass = RequestServiceIntegrationTest, locations = '{}', classes = '{class com.pxs.rqm.requestcrud.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@971d0d8, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@564718df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@36f0f1be, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2145433b], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]

    at org.springframework.util.Assert.state(Assert.java:94)
    at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:185)
spring-boot spring-data-jpa spring-data couchbase spring-data-couchbase
1个回答
0
投票
尽管我们尝试支持大多数Spring接口,但@Transactional接口特定于关系数据库。

Couchbase中的更新是原子性的(全部或全部),并且由于数据建模的方式与关系式不同,因此通常不需要经常进行事务处理。考虑一个用户,例如,与该用户有关的所有数据(首选项,地址,信用卡)都将存储在同一文档中,这就是为什么在这种情况下通常不需要进行交易的原因。

但是,如果您需要同时更新多个文档,则可以考虑使用对事务的新支持,该支持目前仅在SDK 3上才可用(https://www.couchbase.com/transactions]

© www.soinside.com 2019 - 2024. All rights reserved.