How to write optimistic and pessimistic locking code from Java code

I know what optimistic and pessimistic locking are, but how do you do it when you write java code? Suppose I use Oracle and Java, is there any way I can help me do this in JDBC? How do I configure this thing? Any pointers would be appreciated

Solution

You can implement optimistic locking in database tables in this way (this is the way to complete optimistic locking in Hibernate):

>Adds the integer version column to the table. > Increase the value of this column each time the corresponding row is updated. > To obtain the lock, just read the "version" value of the row. > Add the "version = got_version" condition to your update declaration in the where clause Number of rows affected after validation update If no rows are affected – someone has modified your entry

Your update should look like this

UPDATE mytable SET name = 'Andy',version = 3 WHERE id = 1 and version = 2

Of course, this mechanism is only effective when all parties follow it, which is different from the phase-locked anti lock mechanism provided by DBMS without special processing

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>