在多线程环境中使用toplink中的update语句
我正在使用 toplink,但在更新值时遇到一些问题。这是我的代码片段
ExpressionBuilder builder = new ExpressionBuilder();
Expression expr = builder.get("addressId").equal("2");
Address address1 = (Address)uow.readObject(Address.class, expr);
address1.setPincode(address1.getPincode() + 1);
uow.registerObject(address1);
uow.writeChanges();
,因为我的用例是我在多线程环境中执行相同的代码(例如 10 个线程),因此执行后,如果初始值为 0,我应该得到 10 作为 DB 中 pincode 的值。 但是当我执行代码时,我没有得到正确的值。 谁能帮帮我吗
i am using toplink, but i am getting some problem while updating the values. this is my code snippet
ExpressionBuilder builder = new ExpressionBuilder();
Expression expr = builder.get("addressId").equal("2");
Address address1 = (Address)uow.readObject(Address.class, expr);
address1.setPincode(address1.getPincode() + 1);
uow.registerObject(address1);
uow.writeChanges();
as my use case is that i executing the same code in multi threaded environment for say 10 threads, so after the execution i should get 10 as the value for pincode in DB if initial value was 0.
but when i am executing the code i am not getting proper values.
can anyone please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你这里有赛车条件。从读取对象开始到写入更改的操作序列应被视为原子操作,这意味着如果另一个线程未完成其部分,则不允许任何线程读取。
我没有看到任何同步代码来实现这一点。如果没有这样的代码而不是 10,您将随机收到 1 到 10 之间的任何数字
It sounds like you have a racing condition here. The operation sequence starting from read object through the write changes should be treated as atomic, which means that no thread can be allowed to read if another thread is not completed its part.
I do not see any synchronization code to achieve this. Without such code instead of 10 you will randomly recieve any number from 1 to 10