• 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

[B&S] lock/update/read method

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!!

I think of the operation flow for server like this.
lock(recNo)->read(recNo)/update(recNo)->unlock(recNo)

And for the lock method in the given interface, the comment are following.

// Locks a record so that it can only be updated or deleted by this client.
// If the specified record is already locked, the current thread gives up
// the CPU and consumes no CPU cycles until the record is unlocked.
public void lock(int recNo) throws RecordNotFoundException;

Does it means lock method should use ONLY for update/delete operation??
If it is, I must not use lock/unlock method for read operation so I write my own locking code inside the read method.
[ November 04, 2004: Message edited by: Sung Kum Her ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I understand it to mean that only update and delete require a lock. My version requires a lockCookie to be sent to the update, delete, and unlock methods. Also, update, delete, and unlock are the only methods that throw a SecurityException. Therefore, only these methods require a record lock prior to being called.


[ November 04, 2004: Message edited by: Jared Chapman ]
 
Chris Hani
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jared

Thank you for showing your interface file. It is pretty interesting. I have B&S version 2.3.2 and it is slightly different from yours(I don't use those cookies).

Anyway, here's my problem.
B&S problem is for searching contractors, showing and booking one of them. If a client read a record during another client update it(It could happen if I only use lock for update/delete record), the client that read the record would show incorrect information. In worst case, the record may be booked from two different client which is not possible in real world. That is why I want to lock the "read operation".

But as you can see, the spec exclusively mentions that the lock method is for update/delete operation.

How do you deal with this problem?

Now I am thinking that the comment for the lock method don't need to be strictly followed. After I saw your interface I realize that some comments are exactly same(I mean word by word). They might use phrase carelessly. After all, it is not the "must" situation, isn't it?
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If a client read a record during another client update it(It could happen if I only use lock for update/delete record), the client that read the record would show incorrect information.


This can't be helped. Whenever a record is displayed in the gui, you should always assume that it is outdated. What happens if the user performs a search, goes out to lunch, comes back, and then tries booking one of the records? So think of the information in your GUI as a snapshot of the database from a previous instance in time.

Locking the record comes into play when you actually want to manipulate the data in a record (for example, when you want to book a record). This is when it becomes important that the record you read does not change. You need to insure that you can read the record, change the data, and write the record as one atomic operation.

How do you deal with this problem?


When a request comes from the client to the server to book a particular record, I do not sent that request directly to the Data class. This is because booking a record isn't just updating the owner field of the record. It involves several steps:
  • 1. lock the record to be booked to insure it's data cannot be changed by another client
  • 2. read the record from the database to get its current state
  • 3. if the record is already booked, inform the client that the record is already booked
  • 4. if not booked, update the record
  • 5. unlock the record

  • Instead, I have a DataAdapter class that talks to the Data class. In the DataAdapter class, I have a method called bookRecord. Inside this method, I call Data.lock(recNo), Data.read(recNo), verify some information, Data.update(recNo, newData), Data.unlock(recNo).
    [ November 06, 2004: Message edited by: Jared Chapman ]
     
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    About the DBAccess interface. can I modify a bit of the class function like

    public String[] read(long recNo) throws RecordNotFoundException;

    The variable type for recNo is long. I want change it to int. Is the assignment allow me to do that?
     
    Chris Hani
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by YH Lai:
    About the DBAccess interface. can I modify a bit of the class function like

    public String[] read(long recNo) throws RecordNotFoundException;

    The variable type for recNo is long. I want change it to int. Is the assignment allow me to do that?



    Hmm, here is another different version from mine but I don't think you can modify the method because it is the MUST situation.
    In my B&S spec,
    Your data access class must be called "Data.java", must be in a package called "suncertify.db" and must implement the following interface(DBMain)
    Do you have those sentence in yours?(I guess so)
    If you change the method, how you could implement DBMain correctly?
     
    reply
      Bookmark Topic Watch Topic
    • New Topic