V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nightYe
V2EX  ›  问与答

spring @Transactional 隔离级别没有达到预期的效果

  •  
  •   nightYe · 2017-10-09 11:34:09 +08:00 · 1359 次点击
    这是一个创建于 2560 天前的主题,其中的信息可能已经有所发展或是发生改变。

    @Transactional(isolation = Isolation.READ_UNCOMMITTED)

    public void test(int id) {
        User user = userMapper.getOne(1L);
        System.out.println(user.toString());
        int resultStatus = userMapper.updateUser();
        System.out.println(resultStatus);
        if(id == 1) {
            try {
                Thread.sleep(20000); // 20 s
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    issue: 两次请求访问同一个方法 test,第一次请求 param=1,更新 user 表 id=1 的用户信息,然后睡眠线程 20S,在此期间,第二次请求过来,param=2,此时按照理想状态下,应该是直接查询到的 user 是修改过后未 commit 的 user,并且不会被阻塞,直接结束,但是实际情况是 User user = userMapper.getOne(1L)查询到的还是未被修改之前的 user,而且线程因为第一次请求阻塞的原因,也被阻塞了;

    我在 mysql 里开启事务,然后跟新用户信息,不提交事务,然后访问这个方法的时候,是能拿到更新后未提交的数据的,是 sleep 的线程出现了问题吗;

    等一个吊大的大佬;

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1207 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:13 · PVG 02:13 · LAX 11:13 · JFK 14:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.