基于_version进行乐观锁并发控制

  1. 先构造一条数据出来
    PUT /test_index/test_type/7
    {
      "test_field": "test test"
    }
  2. 模拟两个客户端,都获取到了同一条数据
    GET test_index/test_type/7
    
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 1,
      "found": true,
      "_source": {
        "test_field": "test test"
      }
    }
  3. 其中一个客户端,先更新了一下这个数据

    同时带上数据的版本号,确保说,es中的数据的版本号,跟客户端中的数据的版本号是相同的,才能修改
    PUT /test_index/test_type/7?version=1 
    {
      "test_field": "test client 1"
    }
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "7",
      "_version": 2,
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": false
    }
    View Code

相关文章:

  • 2022-12-23
  • 2021-04-18
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
猜你喜欢
  • 2021-11-12
  • 2021-10-29
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案