File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes 2 transactions from same users in same milisecond Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "2 transactions from same users in same milisecond" Watch "2 transactions from same users in same milisecond" New topic
Author

2 transactions from same users in same milisecond

Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 114
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

Joined: Mar 02, 2005
Posts: 443
    
    1


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: 114
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: 114
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

Joined: Oct 16, 2003
Posts: 1076
    
  10

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: 114
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
 
Similar Threads
More of Customer Support
Dirty read (?) problem
Call webservices from a JSF page
Conversations with computers users!
Release It!: How much Design and Deploy