| Author |
2 transactions from same users in same milisecond
|
Michal Glowacki
Ranch Hand
Joined: Mar 14, 2006
Posts: 110
|
|
hi,
2-3 times per day I got a situation, when I see that customer has made 2 different transactions in exactly same time. It is problem as my (web) application is operating on money, and in this situation the balance is not correctly updated. As I observed it is not delay in database, as my session bean logs same time for both operations. I don't see anything wrong with my code and I wonder how it is possible.
Anyway I need to solve it somehow. As it is heavy traffic application I cannot make method responsible for updating balance in database synchronized (ideally it would be synchronized at level of customer). So I got a bizarre idea of making small synchronized function, not operating on database in front of balance update. In my idea it should put transactions one after another.
What do you think about it? Perhaps someone could share with better idea?
Regards,
Michal
|
 |
Raj Kamal
Ranch Hand
Joined: Mar 02, 2005
Posts: 400
|
|
Instead of making the methods synchronized from Java why not start a transaction on the Database end when updating/modifying the data? I feel that it should take care of data inconsistencies.
Cheers,
Raj.
|
 |
Michal Glowacki
Ranch Hand
Joined: Mar 14, 2006
Posts: 110
|
|
Thanks for idea.
But if I use EJB is there no way to solve it at this level? Problem is, that data to be updated are calculated on java side and it would take some time to move it all to database.
|
 |
Michal Glowacki
Ranch Hand
Joined: Mar 14, 2006
Posts: 110
|
|
|
What about optimistic locking in EJB? If I add version column to customer account table, where the balance is stored? That should help right?
|
 |
Ranganathan Kaliyur Mannar
Ranch Hand
Joined: Oct 16, 2003
Posts: 386
|
|
|
And you can 'declare' transactions for EJB methods via annotations. The entire method then runs in a transaction.
|
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
|
 |
Michal Glowacki
Ranch Hand
Joined: Mar 14, 2006
Posts: 110
|
|
I can't declare whole process as one transaction - it's too complex and under heavy load I would get into trouble.
Adding integer column to customer table and marking it with @Version annotation in entity bean works perfectly. It did not hit the performance but solved the problem! Now I see in the logs that there is 4-5 per day ObjectStaleException thrown which prevents the problem from occurring - perfect mechanism.
|
 |
 |
|
|
subject: 2 transactions from same users in same milisecond
|
|
|