• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 transactions from same users in same milisecond

 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can 'declare' transactions for EJB methods via annotations. The entire method then runs in a transaction.
 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic