• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 transactions from same users in same milisecond

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic