• 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

Sychronisation and Transactions

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am really stuck ! Please see if you can help with this problem - looking for a high level architecture for the solution.

We have a stock system. The system worked previously on Unqiue Serial numbers per item. We could easily reserve and item, mark it as sold or get the QTYs at different status.

This work through a Singleton object with a synchronised method called AssignMeStock(). Since this is a multihreaded single JVM we can ensure that StockItems are updated to a new status in order of request to the method. This works well

Now we need the concept of RESERVING stock. This means we need to say "Reserve X items of STKCODE" but in order to do this I need to make sure that multple requests and synchornised.

For example I might have these methods

CheckStockLevel(Stkcode)
ReserverStock(Stkcode,Qty)
AssignStock(Stkcode, Qty)


if someone calls RserverStock but at the same time someone is calling AssignStock I need to make sure the first request "wins".

AssingStock(Stkcode,Qty) might do this


int level = CheckStockLevel(X) ;
if ( level > 0 )
//update the stock item to the new status


RserverStock might do this
int level = CheckStockLevel(X) ;
if ( level > 0 )
/update resrervation table to show the new QTYS



So I have two methods that I need to be synchronised with each other not just the one. Either could be called at any time and one might call the other. How do I make sure this all happens !

I dont know how to do in MYSQL and Its all very well doing it for Single JVM at the moment - so help with that would be great - but later on if I want to scale this to multiple JVMS its not going to work either !

Thanks

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have more than one JVM instance running this process, you need to implement the locking mechanism in the database (it shouldn't be too difficult, but it might require to choose the correct isolation level for your transaction).

If there's just one JVM, simply make all the necessary methods of your singleton object's class synchronized. The synchronization is done on the object, not on the method, so if there are several synchronized methods of the same object, only one of them can be active at any given time - all other threads will wait.

Edit: and welcome to the Ranch!
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a proper DBMS you can start a transaction and then lock the necessary tables on the DB, perform the required queries, and finally unlock the tables and commit the transaction. Transactions make your batch of queries atomic, so that if one fails, all changes will be rolled back to when the transaction started. Locking the tables will prevent other connections from changing the tables while you're performing your transaction. You could do all of this in prepared statements in your data access object. Synchronization is then performed by the DBMS, as opposed to your own code, as Martin mentioned.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic