多个线程一次更新数据库中的同一行如何保持一致性 [英] Multiple threads update same row in database at a time how to maintain consistence

查看:997
本文介绍了多个线程一次更新数据库中的同一行如何保持一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序中,多个线程一次更新同一行如何获得一致性结果?

In my java application multiple threads update same row at a time how to get consistence results ?

例如

current row value count =0; 
thread 1 updating it to count+1=1;
thread 2 updating at the same time count+1=2

    but it should not happen like this 
    thread 1 updating it to count+1=1;
    thread 2 updating at the same time count+1=1;

    both threads should not catch the same value because both are running same time 


    how can we achieve this in jdbc  hibernate , database ??  


推荐答案

有两种可行的方法。


  • 您要么选择悲观方法,要么锁定行,表甚至行范围。

  • Either you choose a pessimistic approach and lock rows, tables or even ranges of rows.

或者您使用版本化的实体(乐观锁定)。

Or you work with versioned Entities (Optimistic Locking).

也许您会在这里找到更多信息:

Maybe you can will find more information here:

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html

这篇关于多个线程一次更新数据库中的同一行如何保持一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆