开发者

using update statement in toplink in multithreaded environment

i am using toplink, but i am getting some problem while updati开发者_高级运维ng 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜