为 Couchbase 应用程序添加事务管理器。在用于 Junit 测试的 SpringBoot 2 应用程序中
add transaction manager for a Couchbase app. in a SpringBoot 2 app for Junit tests
我有一个 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 接口,但 @Transactional 接口特定于关系数据库。
Couchbase 中的更新是原子的(全有或全无),并且由于建模数据的方式不同于关系数据,因此您通常不需要那么频繁的事务。想想一个用户,例如,与其相关的所有数据(偏好、地址、信用卡)都将存储在同一个文档中,这就是为什么在这种情况下通常不需要交易的原因。
但是,如果您需要同时更新多个文档,那么您可以考虑使用目前仅在 SDK 3 上可用的新事务支持(https://www.couchbase.com/transactions)
我有一个 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 接口,但 @Transactional 接口特定于关系数据库。
Couchbase 中的更新是原子的(全有或全无),并且由于建模数据的方式不同于关系数据,因此您通常不需要那么频繁的事务。想想一个用户,例如,与其相关的所有数据(偏好、地址、信用卡)都将存储在同一个文档中,这就是为什么在这种情况下通常不需要交易的原因。
但是,如果您需要同时更新多个文档,那么您可以考虑使用目前仅在 SDK 3 上可用的新事务支持(https://www.couchbase.com/transactions)