栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库控制并发字段的设计与想法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库控制并发字段的设计与想法

数据库控制并发字段的设计与想法 0. 原场景

最近在做一些增删改查,发现了一点我之前从未想过的场景,在高并发的场景下,假如多个用户对同一条信息进行修改,那么这个信息该如何去update呢?仅仅根据id条件去更新,它会发生什么?

1. 场景demo

这里使用Spring Boot + Mybatis + Thymeleaf做了一个demo。

界面,如下图所示:

html核心代码如下:
数据库设计很简单,如下图:

Controller层如下:

为了方便看到 是否更新成功,判断了sql执行后的行数

@Controller
public class PersonController {

    @Autowired(required = false)
    PersonService personService;

    @RequestMapping(path = "/person/{id}", method = RequestMethod.GET)
    public String getPerson(@PathVariable(name = "id")int id, Model model){
        Person person = personService.selectById(id);
        model.addAttribute("person", person);
        return "person";
    }

    @RequestMapping(path = "/person/update", method = RequestMethod.POST)
    public String updatePerson(Person person, Model model){
        int r = personService.update(person);
        if (r == 1) {
            System.out.println("更新成功");
        } else {
            System.out.println("更新失败");
        }
        return "redirect:/person/"+person.getId();
    }
}
Service层如下:
@Service
public class PersonService {
    @Autowired(required = false)
    PersonMapper personMapper;

    public Person selectById(int id){
        return personMapper.selectById(id);
    }

    public int update(Person p){
        return personMapper.update(p);
    }
}
Mapper层如下:
@Mapper
public interface PersonMapper {

    Person selectById(int id);

    int update(Person p);
}
Mapper.xml如下:

关于person的entity实体我们要添加两个字段,如下所示:

Controller层同之前的样子,Service层我们要做一些更新,如下所示:

对传入的person对象设置一个新的coluuid

Mapper层同之前的 Mapper.xml如下:

核心就在于在更新的时候是根据id 和 coluuid 去更新,并且设置一个新的coluuid

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NP1T8Spk-1655011661599)(E:我的文章啊博客imgs浅谈数据库的并发控制image-20220612131348785.png)]

4. demo1测试

依旧运行项目,打开两个窗口,模仿多用户同时进行修改:

然后我们非常快速的点击A页面的修改以及B页面的修改,之后我们发现如下所示:

这次和demo不同的是,最先点击修改的用户成功了,之后的修改并未成功,我们看下日志:

2022-06-12 13:15:20.356 DEBUG 9024 --- [nio-8889-exec-5] c.x.example.dao.PersonMapper.update      : ==>  Preparing: update person2 set name=?, money = ?, coluuid = ? where id = ? and coluuid = ?
2022-06-12 13:15:20.365 DEBUG 9024 --- [nio-8889-exec-5] c.x.example.dao.PersonMapper.update      : ==> Parameters: 张三(String), 100(Integer), 3f9b855f-a507-4f6b-8da1-d18b96d67141(String), 1(Integer), 985a3700-5c0f-4531-a599-143c8a9b5378(String)
2022-06-12 13:15:21.074 DEBUG 9024 --- [nio-8889-exec-6] c.x.example.dao.PersonMapper.update      : ==>  Preparing: update person2 set name=?, money = ?, coluuid = ? where id = ? and coluuid = ?
2022-06-12 13:15:21.082 DEBUG 9024 --- [nio-8889-exec-6] c.x.example.dao.PersonMapper.update      : ==> Parameters: 张三(String), 99(Integer), c0e6fabf-c36b-47d8-a412-cc10ae3d0242(String), 1(Integer), 985a3700-5c0f-4531-a599-143c8a9b5378(String)
2022-06-12 13:15:21.085 DEBUG 9024 --- [nio-8889-exec-6] c.x.example.dao.PersonMapper.update      : <==    Updates: 0
2022-06-12 13:15:21.085 DEBUG 9024 --- [nio-8889-exec-5] c.x.example.dao.PersonMapper.update      : <==    Updates: 1
更新失败
更新成功
2022-06-12 13:15:21.161 DEBUG 9024 --- [nio-8889-exec-7] c.x.example.dao.PersonMapper.selectById  : ==>  Preparing: select id, name, money, coluuid from person2 where id = ?
2022-06-12 13:15:21.163 DEBUG 9024 --- [nio-8889-exec-7] c.x.example.dao.PersonMapper.selectById  : ==> Parameters: 1(Integer)
2022-06-12 13:15:21.165 DEBUG 9024 --- [nio-8889-exec-7] c.x.example.dao.PersonMapper.selectById  : <==      Total: 1
2022-06-12 13:15:21.257 DEBUG 9024 --- [nio-8889-exec-8] c.x.example.dao.PersonMapper.selectById  : ==>  Preparing: select id, name, money, coluuid from person2 where id = ?
2022-06-12 13:15:21.260 DEBUG 9024 --- [nio-8889-exec-8] c.x.example.dao.PersonMapper.selectById  : ==> Parameters: 1(Integer)
2022-06-12 13:15:21.263 DEBUG 9024 --- [nio-8889-exec-8] c.x.example.dao.PersonMapper.selectById  : <==      Total: 1

第一次的更新成功,第二次的更新并未成功,我们把SQL拿出来单独研究下:

Preparing: update person2 set name=?, money = ?, coluuid = ? where id = ? and coluuid = ?
Parameters: 张三(String), 100(Integer), 3f9b855f-a507-4f6b-8da1-d18b96d67141(String), 1(Integer), 985a3700-5c0f-4531-a599-143c8a9b5378(String)
Preparing: update person2 set name=?, money = ?, coluuid = ? where id = ? and coluuid = ?
Parameters: 张三(String), 99(Integer), c0e6fabf-c36b-47d8-a412-cc10ae3d0242(String), 1(Integer), 985a3700-5c0f-4531-a599-143c8a9b5378(String)
Updates: 0
Updates: 1
更新失败
更新成功

两次更新都是按照同一个id 和 coluuid去更新的,因为我们先开始查询用户的时候就已经将这个字段信息写死在html文件中的form表单中了,

但是第一次更新的话肯定会成功,并且设置一个新的coluuid,第二次的话就会失败,因为根据原有的id 和 coluuid 已经查询不到那条记录了。

5. 尾记

以上就是我的对数据库控制并发字段的一些浅显的理解,实际上数据库中底层原理的控制并发并不是如我写的一样,而是遵循MVCC多版本并发控制。具体原理有待深入学习,欢迎大家留言讨论。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号