Я тестировал функцию отката транзакций, используя Spring Data MongoDB (2.2.5) и MongoDB 4.2.5 Версия сообщества . Как упомянуто в официальной документации Spring Data, я настроил MongoTransactionManager в классе конфигурации и создал набор реплик с 3 членами в серверной части.
Также я использовал репозиторий для транзакций mon go db. Аннотировали мой метод Дао, используя @Transactional. Даже когда я реализовал область транзакции в методе, мои данные все еще сохраняются в коллекциях (Person, а также Address), и на уровне Databse исключений во время выполнения в приложении не происходит. Я предоставил необходимый исходный код и трассировку из своего приложения. Пожалуйста, помогите разобраться с истиной, и, пожалуйста, позвольте мне, если я что-то пропущу с моей стороны.
@Slf4j
@Configuration
@EnableMongoRepositories
@EnableTransactionManagement(proxyTargetClass=true)
class ApplicationConfig extends AbstractMongoClientConfiguration {
@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
log.info("Creating dbFactory {}", dbFactory);
MongoTransactionManager manager=new MongoTransactionManager(dbFactory);
log.info("Creating manager {}",manager);
return manager;
}
@Override
public MongoClient mongoClient() {
return MongoClients.create("mongodb://localhost:27020,localhost:27021,localhost:27022/?replicaSet=rs0");
}
@Override
protected String getDatabaseName() {
return "testDB";
}
public @Bean MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(), "testDB");
}
}
@Slf4j
@Service("TestDao")
public class TestDaoImpl implements TestDao{
@Autowired
PersonRepository repository;
@Autowired
MongoOperations ops;
@Autowired
AddressRepository addressRepo;
@Transactional
public void save(List<PersonVO> personList) {
//if(!p.getNationaliy().equalsIgnoreCase("AU"))
AddressVO addVo=new AddressVO();
addVo.setCity("COCHIN");
addVo.setCountry("IN");
addVo.setHouseNo("Rose Villa 123");
Address addr=AddressMapper.INSTANCE.ToAddress(addVo);
addressRepo.save(addr);
//ops.insert(addr);
log.info("Saving....");
for(PersonVO vo:personList) {
log.info("Saving person");
Person p=PersonMapper.INSTANCE.ToPerson(vo);
repository.save(p);
}
throw new RuntimeException();
}
}
Вот журнал, исключение и трассировка стека.
2020-04-01 01:21:24.106 INFO 7696 --- [nio-7092-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-01 01:21:24.106 INFO 7696 --- [nio-7092-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-04-01 01:21:24.127 INFO 7696 --- [nio-7092-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 21 ms
2020-04-01 01:21:24.189 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : Creating new transaction with name [com.example.mongo.demo.service.TestServiceImpl.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2020-04-01 01:21:24.227 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : About to start transaction for session [ClientSessionImpl@3e82b37d id = {"id": {"$binary": "zw5ZFyfdRPSSOgZ/pW/D3A==", "$type": "04"}}, causallyConsistent = true, txActive = false, txNumber = 0, error = d != java.lang.Boolean].
2020-04-01 01:21:24.228 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : Started transaction for session [ClientSessionImpl@3e82b37d id = {"id": {"$binary": "zw5ZFyfdRPSSOgZ/pW/D3A==", "$type": "04"}}, causallyConsistent = true, txActive = true, txNumber = 1, error = d != java.lang.Boolean].
2020-04-01 01:21:24.238 INFO 7696 --- [nio-7092-exec-2] c.e.mongo.demo.service.TestServiceImpl : Saving
2020-04-01 01:21:24.239 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : Participating in existing transaction
2020-04-01 01:21:24.285 DEBUG 7696 --- [nio-7092-exec-2] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [houseNo, city, country, _class] in collection: address
2020-04-01 01:21:24.334 INFO 7696 --- [nio-7092-exec-2] org.mongodb.driver.connection : Opened connection [connectionId{localValue:7, serverValue:37}] to localhost:27021
2020-04-01 01:21:24.501 INFO 7696 --- [nio-7092-exec-2] com.example.mongo.demo.dao.TestDaoImpl : Saving....
2020-04-01 01:21:24.501 INFO 7696 --- [nio-7092-exec-2] com.example.mongo.demo.dao.TestDaoImpl : Saving person
2020-04-01 01:21:24.530 DEBUG 7696 --- [nio-7092-exec-2] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [name, age, gender, nationaliy, _class] in collection: person
2020-04-01 01:21:24.532 INFO 7696 --- [nio-7092-exec-2] com.example.mongo.demo.dao.TestDaoImpl : Saving person
2020-04-01 01:21:24.533 DEBUG 7696 --- [nio-7092-exec-2] o.s.data.mongodb.core.MongoTemplate : Inserting Document containing fields: [name, age, gender, nationaliy, _class] in collection: person
2020-04-01 01:21:24.536 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : Participating transaction failed - marking existing transaction as rollback-only
2020-04-01 01:21:24.536 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : Initiating transaction rollback
2020-04-01 01:21:24.537 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : About to abort transaction for session [ClientSessionImpl@3e82b37d id = {"id": {"$binary": "zw5ZFyfdRPSSOgZ/pW/D3A==", "$type": "04"}}, causallyConsistent = true, txActive = true, txNumber = 1, error = d != java.lang.Boolean].
2020-04-01 01:21:24.538 DEBUG 7696 --- [nio-7092-exec-2] o.s.d.mongodb.MongoTransactionManager : About to release Session [ClientSessionImpl@3e82b37d id = {"id": {"$binary": "zw5ZFyfdRPSSOgZ/pW/D3A==", "$type": "04"}}, causallyConsistent = true, txActive = false, txNumber = 1, error = d != java.lang.Boolean] after transaction.
2020-04-01 01:21:24.554 ERROR 7696 --- [nio-7092-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException] with root cause
java.lang.RuntimeException: null
at com.example.mongo.demo.dao.TestDaoImpl.save(TestDaoImpl.java:57) ~[classes/:na]
at com.example.mongo.demo.dao.TestDaoImpl$$FastClassBySpringCGLIB$$8c1970a4.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) ~[spring-tx-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) ~[spring-tx-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at com.example.mongo.demo.dao.TestDaoImpl$$EnhancerBySpringCGLIB$$8ea2a8.save(<generated>) ~[classes/:na]
at com.example.mongo.demo.service.TestServiceImpl.save(TestServiceImpl.java:46) ~[classes/:na]
at com.example.mongo.demo.service.TestServiceImpl$$FastClassBySpringCGLIB$$949c2e6a.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) ~[spring-tx-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) ~[spring-tx-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at com.example.mongo.demo.service.TestServiceImpl$$EnhancerBySpringCGLIB$$7980a7ba.save(<generated>) ~[classes/:na]
at com.example.mongo.demo.controller.TestController.savePersons(TestController.java:33) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109) ~[spring-boot-actuator-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.31.jar:9.0.31]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]