首页 > 数据库 >spring 中mongoDB事务配置

spring 中mongoDB事务配置

时间:2023-04-11 19:46:16浏览次数:42  
标签:事务 transaction spring 配置 索引 mongoDB 注解

配置事务

事务管理器配置代码:

@Configuration
public class TransactionConfig {
    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory factory) {
        return new MongoTransactionManager(factory);
    }
}

在对应方法加上事务注解。

事务中只能读主库,从库不能读。

错误:Read preference in a transaction must be primary。

mongoDB配置:

mongodb://xxxxx?readPreference=secondaryPreferred&authMechanism=SCRAM-SHA-256&maxPoolSize=20&minPoolSize=5&connectTimeoutMS=10000&socketTimeoutMS=10000&serverSelectionTimeoutMS=5000

去掉配置:readPreference=secondaryPreferred

 

不能创建索引:

Command failed with error 263 (OperationNotSupportedInTransaction): 'Cannot run 'createIndexes' in a multi-document transaction。

由于代码中的PO代码加了创建索引的注解:

@Data
@Document(collection = "xxx")
@CompoundIndex(name = "xx", def = "{'xx' : 1}")
public class xxxPO {

}

类似错误:

(OperationNotSupportedInTransaction) Cannot create namespace test.application in multi-document transaction 错误的解决方法 - 水郁 - 博客园

How to prevent Spring from trying to create indexes during MongoDB transactions? - Stack Overflow

去掉创建索引的注解。

 

参考:https://blog.csdn.net/weixin_43931625/article/details/102236889

标签:事务,transaction,spring,配置,索引,mongoDB,注解
From: https://www.cnblogs.com/lukelhi/p/17306272.html

相关文章

  • skywalking 监控 springboot项目
     部署探针打开idea开发工具Run-》EditConfigurations点击Modifyoptions-》AddVMoption-javaagent:E:\projectdeploy\apache-skywalking-apm-9.2.0\apache-skywalking-apm-bin\agent\skywalking-agent.jar-Dskywalking.agent.service_name=service-mylesson-jav......
  • Spring Boot 整合 Kafka
    Kafka环境搭建kafka安装、配置、启动、测试说明:1.安装:直接官网下载安装包,解压到指定位置即可(kafka依赖的Zookeeper在文件中已包含)下载地址:https://kafka.apache.org/downloads示例版本:kafka_2.13-2.8.0.tgz下载后可本地解压安装,解压位置自选,如D:\Java下解压命令:tar......
  • Springboot集成dubbo完整过程(三)
    准备工作1,准备mysql服务环境2,准备redis服务环境3,准备zookeeper服务环境4,准备逆向生成bean的xml配置文件5,准备slf4j日志xml配置文件6,准备一个sql脚本1,搭建创建服务工程1,创建一个空的父工程,用来统一管理依赖2,创建一个interface接口工程,主要存放业务bean,接口类3,创建一......
  • #yyds干货盘点 springboot和vue搭建前后端项目实现员工的增删改查
    前言我是歌谣今天继续带来前后端项目的开发上次已经开发了部门管理,今天继续开发员工管理后端第一步empcontroller代码packagecom.itheima.controller;importcom.itheima.pojo.Emp;importcom.itheima.pojo.PageBean;importcom.itheima.pojo.Result;importcom.itheima.s......
  • SpringBoot整合ElasticSearch8.x 踩坑记录
    背景jdk版本openjdk-17springboot版本2.6.11pom.xml<!--ElasticSearch提供的依赖--><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>8.6.2</version>......
  • redis-3,redis事务和管道和发布订阅
    redis事务保证数据的一致性,具有原子性的命令setk1v1getk1在一个队列里有一次性的,排他性的执行一系列操作multicmd1cmd2execredis数据库事务redis的事务仅仅是保证事务的操作会被连续单独的执行,redis命令的执行是单线程架构,在执行完事务内所有的命令前是不会执行......
  • Spring-Security
    SecurityConfig@Configuration@EnableWebSecuritypublicclassSecurityConfigimplementsWebMvcConfigurer{@BeanpublicSecurityFilterChainfilterChain(HttpSecurityhttpSecurity)throwsException{//httpSecurity.authorizeHttpRe......
  • spring事务传播行为
      ......
  • spring声明式事务(XML格式)
             ......
  • 【Spring MVC】简单数据绑定
    实体类:  spring-mvc.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:......