简体   繁体   English

Oracle Toplink:以原子方式递增/递减的量

[英]Oracle Toplink: Atomically incrementing/decrementing amount

I have concurrent application which needs to update the incremented/decremented (change in balance) to the balance column in account table. 我有并发应用程序,需要将增加/减少(余额的变化)更新到帐户表中的余额列。

I'm using Oracle Toplink. 我正在使用Oracle Toplink。 One way to do the concurrent updates is to use Optimistic locking by using version column. 进行并发更新的一种方法是通过使用版本列来使用乐观锁定。

1) Optimistic Locking 1)乐观锁

update account set balance=?, version=? 更新帐户集余额= ?,版本=? where id=? id =? and version=? 和版本=?

2) Atomic update 2)原子更新

update account set balance=balance + ? 更新帐户集余额=余额+? where id=? id =?

I would like to use 2) option as it is easier and doesn't require me to read the value first before updating (as in 1) ). 我想使用2)选项,因为它更容易使用,并且不需要我在更新之前先读取值(如1))。

SQLCall sc = new SQLCall("update account set balance=balance + #delta where id=#id");
DataModifyQuery data = new DataModifyQuery(sc);
data.addArgument("delta", BigDecimal.class);
data.addArgument("id", Long.class);
Vector param = new Vector(2); 
param.add(new BigDecimal(.01));
param.add(new Long(12345));
getSession().executeQuery(data, param);

I'm facing problem in executing the update query. 我在执行更新查询时遇到问题。 The issue is that it doesn't reflect the amount at the end. 问题在于它没有反映出最后的金额。

is 2) option is really updating Atomically? 是2)选项实际上是原子更新的吗?

Did I follow right coding approach. 我是否遵循正确的编码方法。 Anything wrong with this example? 这个例子有什么问题吗?

Please guide 请指导

Your atomic update is not atomic. 您的原子更新不是原子更新。

If multiple transactions do that concurrently, both will increment their own view of the current balance, and the last one will win. 如果有多个交易同时进行,则两个交易都会增加自己对当前余额的看法,最后一个会获胜。 Indeed, transactions are isolated from each other (that's the I in ACID ), and don't see the changes made by other transactions. 实际上,事务是相互隔离的(即ACID中I ),并且看不到其他事务所做的更改。

You still need locking (optimistic or pessimistic) to do that correctly. 您仍然需要锁定(乐观或悲观)才能正确执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM