• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

delete in B&S

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

When user will call delete method on deleted record for which deleted flag =1 then we should throw RNFE exception?

let say for record 1 deleted flag =1

delete(1);


???
should throw RNFE ?


please reply

thanks
pramod
 
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
because you can not delete a record without locking it first, your lock method should throw a RNFException in my opinion. And the call to the delete-method will never be reached
 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel ,

But how can we make sure user will first call lock and then delete .
I saw some posting for test cases ,there first call to lock then delete , update .

But we do not know how Sun will test all method of Interface (interface that Sun Provided in instruction.html)
?
do we need to provide the test class for all method to Sun ?

I do not think so .

And we can not bound Sun (may be some junior programmer who will test our submission) first call lock and then call delete .

please suggest .
I am stuck.

bye
pramod
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But how can we make sure user will first call lock and then delete .
I saw some posting for test cases ,there first call to lock then delete , update .



But this is what is supposed to happen. First call lock, then update/delete.

These are the comments of my lock method:



Unless you have something different, this is how your locking mechanism is supposed to work: first call lock, then update/delete.

You can make sure by verifying in your delete method if the user trying to delete the record is the same one that previously locked it. In my case, if the record was not locked, then I throw an IllegalStateException.
 
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

Roberto Perillo wrote:
You can make sure by verifying in your delete method if the user trying to delete the record is the same one that previously locked it. In my case, if the record was not locked, then I throw an IllegalStateException.



that's exactly what i do too

if the interface says: first call lock, then update/delete. you have to implement this interface, so you must take care that you handle each situation according to your interface. and if i use your implementation, i have to look to the interface and use it correctly. Just like when you want to use some class from the java api, you have to look to the javadoc to see what it does and how you should use it (like Regular Expressions). and if you don't use it like is told in the javadoc you will not get correct result, but that's your responsibility, not the one of the developer making the api.
 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel and Robertto

You mean to say that we should first call lock method inside delete and update method ? and end of delete and update method we should call unlock method ?
But no need to call lock/unlock in create , find and read method , correct ?
But lock method return type is void then how you will come to know record is locked or not then you are throwing IllegalStateException?
You know one thing I saw in denny's project

private static ReadWriteLock recordNumbersLock = new ReentrantReadWriteLock();


before accessing any record should we do like this

recordNumbersLock.writeLock().lock();

and after accessing should we do like this

recordNumbersLock.writeLock().unlock();


if we need to call lock /unlock inside delete and updated do we still need to use ReentrantReadWriteLock ?

I am just deviating from my basic question that do we need to provide test class in submission?


Please reply.
 
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
no pramod

you may NOT call lock/unlock in your delete/update. it's meant to invoke lock before update/delete-method is invoked. Adter invocation of the update/delete-method you have to call unlock-method.

From my assignment:


// 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;



so if i want to update record 1, i have to do it like this.



if i try something like this


an exception should be thrown telling me i'm trying to update a record that's not locked.

in my assignment no "must" about adding test classes in the submission jar, so i will not add them, but just use them during development to check everything is still working. my test-class of my implementation has about 75 different tests.

Greetz,
Roel
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradmo,

If your delete method throws SecurityException and RecordNotFoundException, a simple way to code it should be like




The call to delete() method should be preceded by a call to lock(), but it is perhaps not safe to count on lock() to performs some work which must be made by delete().
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put all deleted records in a HashSet. I throw the RecordNotFoundException a) if the record is in the HashSet b)if the record is out of bounds of the records in the file (>= file.size() ). I just change the message accordingly. It's either deleted or doesn't exist. I also re-use deleted records for create if any exist, so I use the deleted records HashSet for that, too. Hope this helps.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:... if i want to update record 1, i have to do it like this.



I hope there is a verify record status between steps 2 & 3.

Regards, Andrew
 
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

Andrew Monkhouse wrote:
I hope there is a verify record status between steps 2 & 3.



Not sure what you mean by "verify record status". But if you mean a check to see if record 1 is a valid record or a deleted one. I don't think such a status check is necessary because in my lock-method i check if it's a valid record or a deleted one. if it's a deleted one, i throw RNFE, else the record is locked. And because the record is locked, no other thread can modify or delete the record before unlock is called. So i don't see a need to put such a check in between the lock and update call.

Kind regards
Roel

 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I mean business logic that checks that the record can still be booked (or possibly deleted). Something like:

  • lock record
  • check if it has been booked
  • if not, then book record
  • otherwise display "sorry" message to user
  • always unlock

  • If you don't do this, then it is no different to making the update method logically atomic, which would allow for:

  • client A checks record is still available for booking
  • client B checks record is still available for booking
  • client A modifies record
  • client B modifies record

  • Now both client A and client B believe they have successfully booked the contractor. Oops.

    See also the JavaRanch SCJD FAQ entry on "Can't we synchronize the update() method and ignore the lock() methods? Why do we have lock() methods in the Data class?".

    Regards, Andrew
     
    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

    Andrew Monkhouse wrote:No, I mean business logic that checks that the record can still be booked (or possibly deleted).



    Yes, i added such a check, but i didn't mention it in my post because it's not really related to the subject of this thread.

    Andrew Monkhouse wrote:
    # lock record
    # check if it has been booked
    # if not, then book record
    # otherwise display "sorry" message to user
    # always unlock



    i added a flow like this to my standalone implementation, but it isn't necessary because only one client is working with the data (and regarding to the instructions: only one program at a time is accessing the db-file). Or should i leave it in the program to make the standalone and the rmi-implementation following the same flow and so for a junior programmer it would be easier to understand it?
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My personal preference is to abstract the connectivity code as much a possible, so that the code providing the client GUI doesn't know whether it is connected locally or remotely. In that way, you will never have to worry about the client code for standalone and the client code for networked diverging. Given that, I would still have the lock-verify-book-unlock logic being called no matter for both standalone and networked.

    Regards, Andrew
     
    pramod karnani
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Roel and Andrew

    I added this flow in both mode standalone and networked in GuiController

    # lock record
    # check if it has been booked
    # if not, then book record
    # otherwise display "sorry" message to user
    # always unlock


    I totally agreed with this but I have other doubts.

    Data class which is acutally implementing the DBMain (Sun given interface)
    I implemented the delete and update functions in different Helper class.
    Data class is calling update method of Helper class , in Data class should also we add given flow ?

    # lock record
    # check if it has been booked
    # if not, then book record
    # otherwise display "sorry" message to user
    # always unlock


    Data class is calling delete method of Helper class , in Data class should also we add the given flow?

    #lock record
    #delete record
    #unlock record

    because when deleting the record , there is no need to verify record status .


    Please reply.



     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is my belief that the Data class should not have any business logic in it. Verifying that a record can only be booked if it is currently not booked is business logic. Likewise verifying that a record cannot be deleted if it is booked is business logic (albeit logic that you probably wont implement, as your instructions don't require it).

    Depending on the instructions given in your supplied interface, you may have to verify that the record is locked before allowing an update or delete, but that is about all.

    If you look hard at the Data class and the interface it implements, you should realize that there is nothing in it that is specific to Contractors. You could use the same class to access a data file for Customers or for any other purpose. But if you start adding business logic into the Data class, then it will get locked down to the business logic. What I mean by this is that if you add logic into the update method of the Data class such that you cannot update a record that is already booked, then you are tying it down to only allowing updates of Contractor booking information, and (even worse) you do not even allow for fixing typos in a data record for the Contractor record.

    Regards, Andrew
     
    pramod karnani
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Andrew and Roel

    I totally agreed with the we should not add business logic "verify record status " in Data class .

    but as in instruction is given

    // Modifies the fields of a record. The new value for field n
    // appears in data[n].
    public void update(int recNo, String [] data)



    // Deletes a record, making the record number and associated disk
    // storage available for reuse.
    public void delete(int recNo) throws RecordNotFoundException;




    I think we should only these flow where we are invoking delete /update method in Data class

    flow for delete
    #lock(recNo)
    #delete(recNo)
    #unlock (recNo)


    flow for update

    #lock(recNo)
    #update(int recNo, String [] data)
    #unlock(recNo)

    but not "verify record status" business logic

    Am I correct ?

    thanks
    pramod

     
    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
    pramod,

    i think your flow is wrong.

    you don't add business logic to the updat-method because it's (like andrew said in his post) a generic class that can be used for saving, deleting, reading other objects than contractors/hotel rooms.

    if you are going to book a contractor or a hotel room you have to check first (before updating) if it's already booked or not. because if you don't do this check and a room is already booked by customerA you are just going to update it and it will be booked to customerB. so you will have 2 customers thinking they have booked the room and will show up in the hotel and that's no good. but this was already explained by andrew's former post

    kind regards,
    roel
     
    pramod karnani
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Roel ,

    But calling lock /unlock for update and delete in Data class in not business logic , verifying the record status is business logic , that I understand.
    But I am not going to verify record status in Data class just call lock and unlock before and after inovking update and delete . correct ?

    If not so then I need to handle RNFE exception in delete and update method . As I do not need to give test class for interface function . Let say when examiner will test delete / update function , can he call delete /update without calling lock /unlock ? Here I am not talking about standalone mode or network mode. Examiner can make a call to delete /update from simple java program to test these functions of interface. I am putting business logic (verify record status)only in GuiController .
    do you think calling lock / unlock method for delete/update is business logic ?


    thanks
    pramod
     
    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
    pramod,

    when you want to book a room, you have to follow this flow:
    - lock the record
    - read the record and verify it's still available
    - if it is, update the according record
    - and finally delete this record

    if you want to just test your update method by a test-class (of like you say a sun examiner wants to see if you have correctly implemented the update-method) you also have to lock the record first before you can update. and in my opinion adding logic in the update method to test if the record is locked by the correct client is necessary to make sure you implement the given interface the correct way. Because in the interface it says:


    // 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;



    if the sun's examiner tries to update a record without locking it first, he will get an illegalstateexception being thrown (in my program that is, how you handle this situation is up to you) because it clearly says that lock method should be called before calling update/delete

    kind regards,
    Roel
     
    pramod karnani
    Ranch Hand
    Posts: 88
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Roel

    when you want to book a room, you have to follow this flow:

    - 1.lock the record
    2. read the record and verify it's still available
    -3. if it is, update the according record
    -4. and finally delete this record



    in last you are deleting the record , that does not sound great ,steps 1-3 looks okay

    As your opinion to throw IllegalStateException if examiner does not call lock before update/delete but lock method return type is void , how can we say that lock method is not successful ?
    Condition to not successful of lock mehthod
    1. If record does not exist or deleted then RNFE exception will be thrown by lock

    2. If record is already locked by same thread , then it has to wait .

    which condition are you throwing IllegalStateException ?

    One more thing there is no need to throw RNFE exception inside update/delete since lock will be called first and it will throw RNFE ,if record is deleted or does not exist.


    thanks
    pramod
     
    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
    your first remark is of course a typo from me. I meant

    and not delete it (because that would be very bad practise indeed)

    it all depends on your interface. my lock-method has also a void return type. so if the recNo is not found i throw a RNFE. if record is already locked by another thread, then thread has to wait.

    in update i check if record is locked (by the same thread that's requesting the update). if it isn't, i throw IllegalStateException (same logic applies for delete-method).

    your last remark is correct and i would even say it's not needed to throw RNFE from update, delete and unlock (because it should not occur at all, if it occurs your lock-method's implementation or your synchronization/concurrency is wrong)

    Kind regards and good luck
    Roel
     
    Ranch Hand
    Posts: 158
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roel De Nijs wrote:
    your last remark is correct and i would even say it's not needed to throw RNFE from update, delete and unlock (because it should not occur at all, if it occurs your lock-method's implementation or your synchronization/concurrency is wrong)


    I think this is the third thread about this problem which was active in the last few days, but I'll say it again - it's not an error as long as you don't recheck after locking that the record is still valid. Update and delete will check again when their turn comes, and that's how they can throw RNFE.
     
    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
    how can a record (that was valid when it was locked) become invalid at the time update or delete is reached? It's locked, so the state of the record can only be changed by the thread that's owning its lock
     
    Alecsandru Cocarla
    Ranch Hand
    Posts: 158
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The record might be deleted while your thread is waiting for it to become unlocked. If you only check before locking, but not after, then it is possible. There is no problem in locking an already deleted record. create() does not care about locking, so it's still possible to create at that point, read() doe not care either, and update() or delete() can check for themselves if the record is still valid or not.
     
    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
    you are suggesting implementation like this:


    in my implementation i switched (1) and (2) from position, so only valid record can be locked making RNFE in update/delete/unlock obsolete
     
    Alecsandru Cocarla
    Ranch Hand
    Posts: 158
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes. Otherwise, if you have a separate lock manager (not your case) you have to do something like:



     
    If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic