如何从java代码中编写乐观和悲观锁定代码

时间:2021-08-27 00:47:14

I know what optimistic and pessimistic locking is, but when you write a java code how do you do it? Suppose I am using Oracle with Java, do I have any methods in JDBC that will help me do that? How will I configure this thing? Any pointers will be appreciated.

我知道乐观和悲观锁定是什么,但是当你编写java代码时,你是如何做到的?假设我在Java中使用Oracle,我在JDBC中有任何方法可以帮助我做到这一点吗?我该如何配置这个东西?任何指针将不胜感激。

2 个解决方案

#1


11  

You can implement optimistic locks in your DB table in this way (this is how optimistic locking is done in Hibernate):

您可以通过这种方式在数据库表中实现乐观锁(这是在Hibernate中完成乐观锁定的方式):

  1. Add integer "version" column to your table.
  2. 将整数“version”列添加到表中。
  3. Increase the value of this column with each update of corresponding row.
  4. 每次更新相应行时,请增加此列的值。
  5. To obtain lock, just read "version" value of the row.
  6. 要获得锁定,只需读取行的“版本”值即可。
  7. Add "version = obtained_version" condition to where clause of your update statement. Verify number of affected rows after update. If no rows were affected - someone has already modified your entry.
  8. 将“version = obtain_version”条件添加到update语句的where子句中。验证更新后受影响的行数。如果没有行受到影响 - 有人已经修改了您的条目。

Your update should look like

您的更新应该是这样的

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

Of course, this mechanism works only if all parties follow it, contrary to DBMS-provided locks that require no special handling.

当然,这种机制只有在所有各方都遵循它的情况下才有效,这与DBMS提供的无需特殊处理的锁相反。

Hope this helps.

希望这可以帮助。

#2


3  

Suppose I am using Oracle with Java, do I have any methods in JDBC that will help me do that?

假设我在Java中使用Oracle,我在JDBC中有任何方法可以帮助我做到这一点吗?

This Oracle paper should provide you with some tips on how to do this.

本Oracle论文应该为您提供有关如何执行此操作的一些提示。

There are no specific JDBC methods. Rather, you achieve optimistic locking by the way that you design your SQL queries / updates and where you put the transaction boundaries.

没有特定的JDBC方法。相反,您可以通过设计SQL查询/更新的方式以及放置事务边界的位置来实现乐观锁定。

#1


11  

You can implement optimistic locks in your DB table in this way (this is how optimistic locking is done in Hibernate):

您可以通过这种方式在数据库表中实现乐观锁(这是在Hibernate中完成乐观锁定的方式):

  1. Add integer "version" column to your table.
  2. 将整数“version”列添加到表中。
  3. Increase the value of this column with each update of corresponding row.
  4. 每次更新相应行时,请增加此列的值。
  5. To obtain lock, just read "version" value of the row.
  6. 要获得锁定,只需读取行的“版本”值即可。
  7. Add "version = obtained_version" condition to where clause of your update statement. Verify number of affected rows after update. If no rows were affected - someone has already modified your entry.
  8. 将“version = obtain_version”条件添加到update语句的where子句中。验证更新后受影响的行数。如果没有行受到影响 - 有人已经修改了您的条目。

Your update should look like

您的更新应该是这样的

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

Of course, this mechanism works only if all parties follow it, contrary to DBMS-provided locks that require no special handling.

当然,这种机制只有在所有各方都遵循它的情况下才有效,这与DBMS提供的无需特殊处理的锁相反。

Hope this helps.

希望这可以帮助。

#2


3  

Suppose I am using Oracle with Java, do I have any methods in JDBC that will help me do that?

假设我在Java中使用Oracle,我在JDBC中有任何方法可以帮助我做到这一点吗?

This Oracle paper should provide you with some tips on how to do this.

本Oracle论文应该为您提供有关如何执行此操作的一些提示。

There are no specific JDBC methods. Rather, you achieve optimistic locking by the way that you design your SQL queries / updates and where you put the transaction boundaries.

没有特定的JDBC方法。相反,您可以通过设计SQL查询/更新的方式以及放置事务边界的位置来实现乐观锁定。