• 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

Request For Comment - Locking Strategy

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

After much deliberation, pseudo code writing, diagram drawing and some revision of this post, I have come up with the following plan for locking. Please let me know if you think there is something I am over looking.


NOTE: I keep a static key,value (primary key,locking instance of Data) hashmap of the currently locked records named reservations.

to read a record from the database:
the following steps are all inside the synchronized read method of my singleton DatabaseAccess class.
1 - readLock the dbIndexLock
2 - read from the db file
3 - unlock the dbIndexLock

to update a record in the database:
the following steps are all inside the synchronized update method of my singleton DatabaseAccess class.
1 - writeLock the dbIndexLock
2 - lock the record in reservations
3 - read from the db file
4 - update the in memory record
5 - write to the updated record to file
6 - unlock the dbIndexLock
7 - unlock the record in reservations
8 - notify all

Be gentle
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Be gentle


I'll try

Your update-method should only do what the method name suggests: updating the record that is. Not locking the record (that's courtesy of the lock-method), not unlocking record (courtesy of unlock method). Just updating the record. That's it! Of course you'll also have to do a check to see if the record was successfully locked by the thread/client that's wanting to update the record. If the check completes successfully, the record is updated and that's really it for this method. Why would you otherwise have methods like lock/unlock?
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks yet again Roel.

Surprise surprise, I have another question:

In standalone mode, we know that only one client will ever try to book a record, should I still go through the locking / unlocking of the index file and the record each time a user books/returns a contractor, even tho' technically, there is no need?

Using the oft quoted KISS methodology, I would say no.

However, for consistency if nothing else, it feels like I should route these functions of my application through the same locking / unlocking methods.

What is your opinion?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on the given interface you have to lock the record before you can update the record (and your update record is checking if it's correctly locked). You definitely don't want a seperate update implementation for standalone mode. Your Data class should be completely unaware of the mode it's running in. So you'll have to call lock/update/unlock to successfully update a record.
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic