代码之家  ›  专栏  ›  技术社区  ›  user2083529

数据库调用的Spring引导并发问题

  •  0
  • user2083529  · 技术社区  · 6 年前

    我正在开发一个Spring引导应用程序。我面临的问题与集群有关。下面是一段示例代码。 假设,我想创建一个订单,其中每个人都有一个数字(只是一个假设)。要创建订单,首先从foo表中获取编号。然后将订单保存在订单表中。之后,数字将递增并保存在foo表中。

    现在,如果我们有一个集群环境,并且同一个人的并发请求到达createOrder(),那么我希望锁定foo表(用于读/写操作)。如果此人不同,则不应执行锁定。

    在Spring引导中是否有任何解决方案,不管数据库是什么。

    @Transactional
    public void createOrder(String person){
      Long number = getNumber(person);
      //some other operations on person
      // save the order with the particular number and person
      // increment the number
      number++;
      setNumber(number, person);
    }
    
    public int getNumber(String person){
      // gets the number for the particular person from the database
    }
    
    public void setNumber(int number, String person){
      //saves the number for the particular person in the database
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Brandon    6 年前

    1. createOrder
    2. auto_increment id = (previous id++)

    https://en.wikipedia.org/wiki/Universally_unique_identifier

        2
  •  0
  •   Karol Dowbecki    6 年前

    Hibernate docs, Chapter 5. Locking This answer @org.springframework.data.annotation.Version