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?
And you can 'declare' transactions for EJB methods via annotations. The entire method then runs in a transaction.
SCJP 1.4, OCMJEA/SCEA 5.0.
Joined: Mar 14, 2006
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