wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Advice upon delete method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Advice upon delete method" Watch "Advice upon delete method" New topic
Author

Advice upon delete method

Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi,
Currently doing the UrlyBird project and need advice upon the delete method.
My lock method states I must lock upon recordNo.
But for my delete method I have a cache which I delete passed record in cache and on file.
The problem is when I call my unlock method and I must pass the same recordNo, but clearly this number has been deleted from cache and it must throw a recordNotFoundException.
Any ideas simplest way around this?
Any ideas appreciated.
Thanks.
Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Simple answer - don't use a cache! :-)

In all seriousness though, I've never seen the benefit or point of using a record cache for any of the JCD projects. Performance isn't a design consideration and neither is the number of times you hit your data file - so why bother?

Chris

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

Hi Chris (and Mark),

Totally disagree with you Using a cache makes your code a lot easier (no need to catch those IOException in every method for example). And just using a map instead of a raf is a lot easier. Your file access is narrowed down to 2 methods (a readFromFile-method which is creating the cache and will read the file and a writeToFile-method which write cache back to file). But that's just a design decision and afterwards I was pretty happy to have chosen the cache (and the singleton), because you can't get simpler code than with this approach.

Now concerning Mark's issue. I used a cache and first of all I did a whole lot of thinking about it and then using my common sense I came up with this solution: because before every update and delete you have to call lock with recNo as parameter. If the record does not exist, a RNFE is thrown here. And only here! A RNFE can't be thrown from update or delete or unlock, because that would mean there is something wrong with your locking mechanism. So in my own custom interface I override these 3 methods (update, delete and unlock) and just skipped the "throws RNFE" clause. And of course I documented this heavily in my choices.txt

Hope it helps!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers,
Thanks for your great points of view. Just regarding the caching Roel, when you say you "skipped the throws RNFE" clause", does this mean you just caught the exception in the overridden method but just swallowed the exception, doing nothing with it?
or did you extend the given interface, ignore the recordNotFoundExceptions and implement this interface instead, is this allowed? If so, did you have to do same with rmi/network part?
Thanks.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

Hi Mark,

I have following code making all this happen:


That's what I did, the delete-method in my interface is a valid override of the same method in sun's interface. So i'm pretty sure it is allowed (like I said before I documented this decision quiet heavily in choices.txt), because I passed with a nice score

Concerning the RMI-part: I used a business service with just 2 methods: find rooms and book room. This service/interface throws business exceptions (and RemoteException) no RNFE or other database-exceptions

Kind regards,
Roel
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Wow, this is interesting. A couple of questions Roel, if you don't mind as I'm quite new to project development.
(1) If one defines their own interface as above as in "MyInterface", is one allowed to refer to this interface instead of the "SunInterface" which accessing data across the network and for local use.
(2) Am interested in the RMI part and using business methods. Hadn't really though of posting majority of work to server instead of a thick client which I prefer not to go with.
Would the business methods work as follows, for example, for method bookRoom()
On Network.
Business interface: has for example bookRoom and throws remoteException and accesses "MyInterface"
Businesss interface implementation: example, for bookRoom (get lock,update,releaselock)
Locally:
Local Interface: same as business interface, does not throw remote exception, and access "MyInterface"
Local Interface impelementaiton: same as business interface implementation but no RemoteException.
Thanks for all help and ideas. Excited about possibility of not using a thick client.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

Hi Mark,

(1) of course, otherwise you can't use your own defined methods. Like in this example:


(2) Take a look at this thread, it certainly will be helpful and i described there how I implemented the business layer (but with a dummy example of course). Both my service implementations (local and network one) use the MyInterface reference. And it has the implementation you described, except for one extra addition: don't forget to check if a room is already booked, because otherwise rooms can be booked by more than 1 different client and that's not good because they all show up and claim they have made a valid booking

a bit off topic: any chance you are related to the great ronnie 'the rocket' o'sullivan?

Kind regards,
Roel
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
No, do running though. Ya, watching some of the snooker at moment, Ken doherty doing well 1 quick question, for the lock and unlock, I presume the recordNotFoundException is raised, if a record is not found in the text file or marked as deleted and not in, say the lockManager map for mapping record numbers to cookie values. I hope this is correct assumption. For example, if 30 records in file and someone enters record number 31, this raises a recordNotFoundException. This means the methods lock and unlock can be viewed independently.
Thanks.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

Hi Mark,

And 1 quick answer My lock-method throws RNFE if the record is marked as deleted or does not exist. So no access to the map with locked records for checking if a recNo is valid.

Kind regards,
Roel
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi Roel,
Sorry as regards this, "don't forget to check if a room is already booked, because otherwise rooms can be booked by more than 1 different client and that's not good because they all show up and claim they have made a valid booking", I presume this has to do with the "Date Available" field and this ties in with the "48 hours of the start of room occupancy". Therefore if someone booked a room on the 2003/04/20, this would be stored in database as 2003/04/22, which is the next date the room is available. I'm asking because I have seen threads where people say 1 only updates the owner field, but I think this 48 hour thing needs to be implemented.
Thanks very much and would very much appreciate your views.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

Mark,

This has nothing to do with that "48 hours" rule, because I didn't implement that myself.

A small example to make clear what I was trying to tell you: let say we have 1 room available in Carlton Ritz hotel in Paris, really a nice hotel room.

So customer A calls to CSR1 and wants to book that room. At the same time customer B calls to CSR2 wanting too book that room too. CSR1 and CSR2 search for the appropriate room, hit the book button, fill in appropriate customer-id and confirm booking. Thanks to your flawless locking mechanism one of the book-requests is put on hold, the other one (let say CSR1) has booked the room. So CSR1 sends confirmation to customer A with the successful booking. The record becomes unlocked and CSR2 grabs the lock and he also will end up with a successful booking.
So at a given night 2 customers will show up at the Ritz hotel having a paper telling them they have a successful booking. Ritz hotel will complain by urlybird, both customers will complain to urlybird,...

If you check if a room is already booked, CSR2 will get an exception telling him the room was already booked. and only customer A will show up in the Ritz hotel, get his room and have a lovely and romantic holiday with his wife/girlfriend in Paris. Another happy customer of URLyBird.

Kind regards,
Roel
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Hi Roel,
Thanks for explanation. Have that idea, just wondering does 1 assume that if a room is booked, that it can never be unbooked in that case? That all the booking does is check if room is booked, if not, try to book a room for customer (test for 1 or more people at same time for locking/unlocking). Therefore all application does is fill up all booking, modifying the CSR's for each room, there is no notion of ever unbooking in application, but once all rooms are filled with CSR's, apart from search functionality, that's the application complete? Just asking, because I keep thinking there's more to application than that and I must be missing something.
Thanks.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5132
    
  12

No just book and search for the assignment. That's it! (and believe me, to do it it's more than hard work).

Kind regards,
Roel
Mark O' Sullivan
Ranch Hand

Joined: Aug 17, 2009
Posts: 160
Cheers, thanks. Well believe it. Looking forward to the fun with locks. Bring it on!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Advice upon delete method
 
Similar Threads
rethinking my design
Delete Method
my question about LockManager.java
Comments on lock/unlock please
is my locking sufficient