Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: [URLyBird] unlock(...) and not locked record

 
Maksim Golubkow
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, what do you do if the parameter recNo represents a record that was not locked by lock(...) method:
  • throw new SecurityException
  • throw new IllegalStateException with the message "the record is not locked"
  • simple return and do nothing


  • Regards, Maksim
     
    Mark Spritzler
    ranger
    Sheriff
    Posts: 17278
    6
    IntelliJ IDE Mac Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I believe the instructions states that a client that does not own the lock of a record cannot call unlock.
    So basically any call to unlock for a record that is not locked or is locked by a different user, then nothing should happen in that call. You can always throw an exception if you want. However, if you code your application that the only time unlock is called is quickly after lock has been called, then you don't have to do anything. Meaning in your booking method you call lock-read-modify-unlock. Then all calls to unlock will be by the client that locked the record.
    Mark
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree with Mark. I don't know for sure about URLyBird, but the Contractors assignment has the following statement in the API for unlock():
    Cookie must be the cookie returned when the record was locked; otherwise throws SecurityException.

    No room for argument there. However check your own instructions to be sure.
     
    Maksim Golubkow
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark, Jim thanks for yours replies, so I have to throw SecurityException.
    Regards, Maksim
     
    Ta Ri Ki Sun
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Maksim Golubkow:
    Mark, Jim thanks for yours replies, so I have to throw SecurityException.
    Regards, Maksim

    Hi again Maksim, are you sure your instructions state what Mark just mentioned?
    because I'm also doing URLyBird, and my instructions do not state this, if yours does please post a snippet of your instructions file, I want to confirm that ours is different,
    Thanks, TQ
     
    Maksim Golubkow
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi TQ,
    here is my instructions of the unlock method:
    Releases the lock on a record. Cookie must be the cookie returned when the record was locked; otherwise throws SecurityException.

    What exception should be thrown if the record was not locked before? My first implementation has thrown IllegalStateException with the message "The record was not locked by lock(...) method!" to force the user of my API to write good code.
    Regards, Maksim
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11914
    207
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Maksim
    With the instruction you mentioned, you should throw SecurityException.
    TQ: This is in the comments before the unlock method signature in the interface definition in version 1.1.1 of UrlyBird:

    Regards, Andrew
     
    Maksim Golubkow
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Andrew, I'm doing version 1.2.2 and there is no RecordNotFoundException in the throws clause.
    Regards, Maksim
     
    Ta Ri Ki Sun
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi guys, I'm doing version 1.3.1, and heres my snippet

    // Releases the lock on a record.
    public void unlock(int recNo) throws RecordNotFoundException;
    // Determines if a record is currenly locked. Returns true if the
    // record is locked, false otherwise.

    I got that assignment after they mailed me to get a new download because I was missing the db file, I then planned my project for a week before starting to code, and I got a new download then as well, and my file had not changed, so I started coding to that spec and wont even think about getting another download at this point, too risky, so this is what I'm submitting.
    Quite strange that I also get 80 marks for locking, I hope I dont lose marks for having not implemented things they never asked for.
     
    Ta Ri Ki Sun
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I also still have my 1.2.1 instructions file and unlock has the following

    // Releases the lock on a record. Cookie must be the cookie
    // returned when the record was locked; otherwise throws SecurityException.
    public void unlock(long recNo, long cookie)
    throws SecurityException;
     
    Rob Pearson
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    I'm doing 1.3.3 of URLyBird and my unlock is
    // Releases the lock on a record.
    public void unlock(int recNo) throws RecordNotFoundException;

    I've got a question on the lock method:
    // 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;

    I assume this should have some call to wait() in it, but this throws an InterruptedException. Since I can't just declare it as thrown, because that changes the interface, what should I do with it? Re-throw as a RecordNotFoundException? Just ignore it (not sure if I can)? Fall over in a screaming heap and exit the server!
    Any thoughts?
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11914
    207
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    G'day Rob,
    Personally I would catch it, then rethrow it as a RecordNotFoundException, with a suitable reason.
    If you are using JDK 1.4 you can nest exceptions, so you could nest the InterruptedException within the RecordNotFoundException.
    Regards, Andrew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic