Multiple threads update same row in database at a time how to maintain consistence

后端 未结 5 1524
既然无缘
既然无缘 2021-01-14 04:19

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

for example

current row value count =0; 
thread 1          


        
相关标签:
5条回答
  • 2021-01-14 04:57

    Here is a detailed answer to this question: How to properly handle two threads updating the same row in a database To summarize:

    The biggest question is: Are the two threads trying to persist the same data ?To summarize the content of the linked answer. Lets name the two threads T1 and T2. There are couple of aproaches:

    Approach 1, this is more or less the last to update Wins situation. It more or less avoids the optimistic locking (the version counting). In case you don't have dependency from T1 to T2 or reverse in order to set status PARSED. This should be good.

    Aproach 2 Optimistic Locking This is what you have now. The solution is to refresh the data and restart your operation.

    Aproach 3 Row level DB lock The solution here is more or less the same as for approach 2 with the small correction that the Pessimistic lock dure. The main difference is that in this case it may be a READ lock and you might not be even able to read the data from the database in order to refresh it if it is PESSIMISTIC READ.

    Aproach 4 application level synchronization There are many different ways to do synchronization. One example would be to actually arrange all your updates in a BlockingQueue or JMS queue(if you want it to be persistent) and push all updates from a single thread. To visualize it a bit T1 and T2 will put elements on the Queue and there will be a single T3 thread reading operations and pushing them to the Database server.

    0 讨论(0)
  • 2021-01-14 04:59

    If you're working on a DB that has sequence generators (Oracle, Postgres, ...) you should consider using those. Assuming you're always doing the same increment value and it's no that one thread increments by one and another by two then that should be a good solution.

    0 讨论(0)
  • Your question is not 100% clear, but I guess you're looking for the different locking strategies: http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#locking

    0 讨论(0)
  • 2021-01-14 05:08

    Incrementing a counter in this way is hard to manage concurrently. You really need to use pessimistic locking to solve this particular problem.

    SELECT 1 FROM mytable WHERE x IS TRUE FOR UPDATE
    

    This will force each thread to wait until the previous one commits before it reads the counter.

    This is necessary because you have two potential issues, the first is the read race and the second is the write lock. The write lock gets taken automatically in most RDBMSs but unless you take it explicitly before you read, the counter will be incremented once by both threads together (because both read the original value before the update).

    If you need to have parallel writes, then you need to insert a table and then materialize an aggregate later. That is a more complex design pattern though.

    0 讨论(0)
  • 2021-01-14 05:18

    There are two possible ways to go.

    • 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

    0 讨论(0)
提交回复
热议问题