Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sychronisation and Transactions  RSS feed

 
James McArthur
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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!
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!