V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dumbbell5kg
V2EX  ›  程序员

请教大家一个 springboot 中 mongodb 事务的问题

  •  
  •   dumbbell5kg · 2022-07-04 21:03:24 +08:00 · 1162 次点击
    这是一个创建于 655 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • mongo 的文档里说默认隔离策略是 Read Uncommitted ,定义为“在事务提交之前,在事务中所做的数据更改在事务外部是不可见的”,原文是这样的“Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.”,链接是 https://www.mongodb.com/docs/v4.2/core/read-isolation-consistency-recency/

    • 而 spring 中对 Read Uncommitted 的定义是事务间可以互相读到未提交的更改

    public enum Isolation {
    	/**
    	 * A constant indicating that dirty reads, non-repeatable reads and phantom reads
    	 * can occur. This level allows a row changed by one transaction to be read by
    	 * another transaction before any changes in that row have been committed
    	 * (a "dirty read"). If any of the changes are rolled back, the second
    	 * transaction will have retrieved an invalid row.
    	 * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
    	 */
    	READ_UNCOMMITTED(TransactionDefinition.ISOLATION_READ_UNCOMMITTED),
    
    • 该怎么理解两者的不同呢,难道这两者本来就是各玩各的吗
    6 条回复    2022-07-06 10:11:16 +08:00
    Nasei
        1
    Nasei  
       2022-07-04 21:49:44 +08:00
    spring 那个就是关系型数据库隔离级别的定义

    mongoDB 和关系型那种不太一样,能不能读到数据和 read concern 有关系,你复制的那段,是它多文档事务的保证
    nothingistrue
        2
    nothingistrue  
       2022-07-05 09:34:24 +08:00
    关系数据库的事务,跟非关系数据库的事务,不是一套体系,不能一起看待。比如说 Redis 的事务仅仅代表多个命令同时执行,都没回滚的。

    Spring 的事务管理体系,是完全基于关系数据库的(它貌似压根没有 ORM 当中 O 层面的事务管理,完全用得是 R 层面的事务管理)。
    dumbbell5kg
        3
    dumbbell5kg  
    OP
       2022-07-05 15:24:55 +08:00
    谢谢大佬们的回复,又涨姿势了
    dumbbell5kg
        4
    dumbbell5kg  
    OP
       2022-07-05 15:46:50 +08:00
    @nothingistrue 请教一下是如何知道 “Spring 的事务管理体系,是完全基于关系数据库的” ,我找了一下 spring 的 java 注释,没有看到类似的提示
    nothingistrue
        5
    nothingistrue  
       2022-07-05 16:48:58 +08:00
    @dumbbell5kg 没有明确的说明,只是从经验上来说是这样,你可以看看 TransactionManager 的实现类。
    dumbbell5kg
        6
    dumbbell5kg  
    OP
       2022-07-06 10:11:16 +08:00
    @nothingistrue 好的 了解了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1009 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:04 · PVG 03:04 · LAX 12:04 · JFK 15:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.