This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking peseduo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Locking peseduo" Watch "Locking peseduo" New topic
Author

Locking peseduo

GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Hi guys, as everyone says locking can be a brain twister and it is. What do you all think of the following pseudo code:

Book a record:

1.Lock the database
2.Lock the record
3.Read the record from the database to get the most recent data
- If record is deleted or already booked inform user
Refresh cache memory with current data from database
Refresh JTable to display the most recent data
Unlock database (Finally statement to ensure its always executed)
Unlock record (Finally statement to ensure its always executed)

-If record is not deleted or not booked
Update database with new booking
Refresh cache memory with current data from database
Refresh JTable to display the most recent data
Unlock database(Finally statement to ensure its always executed
Unlock record (in Finally statement to ensure its always executed


Delete a Record:

1.Lock the database
2.Lock the record
3.Read the record from the database to get the most recent data
If record is deleted inform user
Refresh cache memory with current data from database
Refresh JTable to display the most recent data
Unlock database (Finally statement to ensure its always executed)
Unlock record (Finally statement to ensure its always executed)

-If record is not deleted
Update database ie delete the record or flag it to be deleted
Refresh cache memory with current data from database
Refresh JTable to display the most recent data
Unlock database(Finally statement to ensure its always executed)
Unlock record(Finally statement to ensure its always executed)


I presume create() will be similiar
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi GD,

Your psuedo-code shows you
  • lock database
  • lock record
  • ...
  • unlock database
  • unlock record]


  • Aren't items 4 & 5 the wrong way around?

    For that matter, why are you locking the database? All you should need to do from your "booking" method is to lock the individual record. Individual methods within the Data class might have to lock the database while they complete, but these should only be temporary locks. You do not want the entire database locked for the entire book method as this reduces concurrency.

    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
     
    wood burning stoves
     
    subject: Locking peseduo
     
    Similar Threads
    Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird)
    [B&S] lock/update/read method
    last one gets the lock gets the contractor?
    Locking strategy URLyBird 1.1.1 with ReentrantLock
    Best way to keep track of record in JTable