aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [B&S] lock/update/read method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[B&S] lock/update/read method " Watch "[B&S] lock/update/read method " New topic
Author

[B&S] lock/update/read method

Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
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 ]
Jared Chapman
Ranch Hand

Joined: Feb 23, 2004
Posts: 81
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 ]

B.S. University of Wisconsin<br />SCJP 1.4 (85%)<br />SCJD 1.4 (92%) B&S Contractors
Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
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

Joined: Feb 23, 2004
Posts: 81
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 ]
    YH Lai
    Greenhorn

    Joined: Apr 15, 2004
    Posts: 16
    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

    Joined: Jun 03, 2004
    Posts: 42
    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?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: [B&S] lock/update/read method
     
    Similar Threads
    NX: Client side lock vs Server side lock
    URLyBird 1.2.1 - SecurityException
    Question about lock()
    Locking strategy URLyBird 1.1.1 with ReentrantLock
    NX: About data consistent