SCJP2, SCWCD
1) The assignment mentions that the application will be used by the client as an exercise before going to a web based system. In addition, the client does not expect much reuse of this application. In this case I wouldn't be concerned with a design that accommodates two databases because the future use of the application is uncertain. Even more, they mention that features that deviate from specs will not receive full credit. I know that you guys are discussing some designs that accommodate multiple databases, Lock Managers, but it looks to me the simple design suggested by Max in his book is appropriate (multiple instances of Data sharing static member).
2) I cannot find anywhere in the assignment saying that the record locked by a client can only be unlocked by the same client (Contractors assignment). Am I correct assuming that establishing locks on the records can be done at a minimum in this case keeping track of record#?
3) If the application is required only to search, display and book records, why would I care about deleting and adding records? I try to keep things simple here.
SCJP2, SCWCD
SCJP<br />SCJD
I think I'll go with something in between, a solution that adds a little more on the required functionality/design. That's the problem, once you open the door to improvements, it's hard to stop. And you can't tell when is the right time to stop.
By the way, the assignment doesn't say for whom is a "learning exercise", for the IT team or for the Customer Service .
Do you really think they thought of the CSRs there ?
QUOTE]
Of course not, but I was pointing to the overall ambiguity of the assignment.
On the other hand, still no comment on the record lock. Why track users who lock when there is no need? I'd go with locking records only if they don't require something else.
Cheers,
Adrian
SCJP2, SCWCD
Why is it that you insist that delete/update must not be available to the client?
If so, the you suggest a very simplistic approach that I had not though of.
In my (submitted) assignmentI just used the schema to determine the look of my GUI and provided a very thin client that "used" all the methods in the DBAccess interface. And provided a JTable for search results. Do you think I will be penalised for providing an explicit update and a delete function?
Originally posted by Adrian Muscalu:
2) I cannot find anywhere in the assignment saying that the record locked by a client can only be unlocked by the same client (Contractors assignment). Am I correct assuming that establishing locks on the records can be done at a minimum in this case keeping track of record#?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Niall ORiordan:
In my (submitted) assignmentI just used the schema to determine the look of my GUI and provided a very thin client that "used" all the methods in the DBAccess interface. And provided a JTable for search results. Do you think I will be penalised for providing an explicit update and a delete function?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
SCJP2, SCWCD
Originally posted by Adrian Muscalu:
You have a point saying that only the client that put a lock on a record has the right to unlock it. No argue here. I only suggested that I see no reason in keeping a reference to the client/data_object/thread that initiated the action, something like:
I am probably missing something here. I thought that this can be solved by implementing corectly the code in lock and unlockmethods. While Thread1 has a lock on Record1, any thread that intends to get a lock on Record1 waits. Who else can unlock Record1 other than Thread1?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
Unfortunately while you are correct that client's will block if they try and lock an already locked record, this is not the same as saying that only the client that locked a record may update it.
You still have the loophole that client A could lock the record, then client B could just call update() without calling lock(), in which case your solution would not care that client B does not hold a lock - it will allow the update to go ahead.
Unfortunately you cannot even claim that it is only your client that will ever be used. It is quite possible that if they go to a web based solution, they may keep your server as the back end serving both local and web based clients.
To ensure that this does not happen, you are going to have to have some way of identifying the owner of the lock. Then within the update() and delete() methods you are going to have to verify if the calling client owns the lock, otherwise disallow the operation.
SCJP2, SCWCD
Andrew:
It is quite possible that if they go to a web based solution, they may keep your server as the back end serving both local and web based clients.
Adrian:
Now I see where you�re coming. But I still think you�re wrong.
Adrian:
Client B cannot call update() without calling lock(). It's part of the design.
A client cannot directly invoke lock() or unlock(). Instead, has access to update(), add(), etc. The call to lock is part of the upload() method in this case.
No need to identify the client. The current client has the lock on the record and is the only one who can release the lock (remember, nobody calls unlock without having the lock first).
If Joe locked the record, I don't care if it was Joe. All I care is that nobody else but Joe can access that record until Joe finishes the operation and unlocks the record.
Originally posted by Philippe Maquet:
But you don't care that Jim uses Joe's lock to update the record in his place, or that Jim unlocks the same record, do you ? Now if your version use lock cookies, it may be different.
SCJP2, SCWCD
The requierement states:
"Locks a record that so that it can only be updated or deleted by this client."
Originally posted by Philippe Maquet:
I have a few questions. Where is your modify() method coming from ? Does it belong to Data ? (I ask that because mine is called updateRecord() and our assignments are different). If true, is it its responsabity to call lock() by itself or should lock() be called before (but independantly of) your modify() method ?
Could it make sense that some programmer calls lock(), then read() to check if the record changed, before deciding to update() the record or not ?
SCJP2, SCWCD
SCJP2, SCWCD
Andrew:
It is quite possible that if they go to a web based solution, they may keep your server as the back end serving both local and web based clients.
Phil:
That sentence looks like you were still arging in "the big thread", and betraying your camp.
Adrian:
my modify() method is implemented in the Data class. The name is update() but is not the name we are after; it's the location. It is indeed it's responsability to call lock.
Phil:
Could it make sense that some programmer calls lock(), then read() to check if the record changed, before deciding to update() the record or not ?
Adrian:
It makes perfect sense to do that since several threads might wait to get a hold on a particular record. Once they get a chance to do it, the record might be already deleted by one of the previous threads.
Adrian:
It is also true that we might have the following situation: client1 is modifying record1, client2 tries the same and waits. Client1 finishes job, notifies and client2 locks the object, modifies it. This is some sort of "dirty modify" because client2 just modified a record he doesn't know. Should we take this into account? Not for this assignemnt I would say.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
So as I understand this, your Data.update() method calls Data.lock() itself. In which case there is no value in Data.lock() being public. (But it has to be, since that is the signature in the interface - why do they have such an method signature then?)
Client 1 believes that they have booked this Contractor and paid their money. Client 2 believes that they have booked this Contractor and paid their money.
SCJP2, SCWCD
Adrian:
The link you've provided in the previous message had a strong impact on the way I intend to design.
Andrew:
So as I understand this, your Data.update() method calls Data.lock() itself. In which case there is no value in Data.lock() being public. (But it has to be, since that is the signature in the interface - why do they have such an method signature then?)
Adrian:
My Data.update() calls indeed Data.lock() but the latter only dispatches to the lock() method of the LockManager class. It is this class that handles all the locking process and every instance of Data has a reference to it.
Adrian:
I'm sure that whatever design will choose, we'll meet at the other side.
Adrian:
Phil, you've made an excellent case in that thread.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew:
So as I understand this, your Data.update() method calls Data.lock() itself. In which case there is no value in Data.lock() being public. (But it has to be, since that is the signature in the interface - why do they have such an method signature then?)
Adrian:
My Data.update() calls indeed Data.lock() but the latter only dispatches to the lock() method of the LockManager class. It is this class that handles all the locking process and every instance of Data has a reference to it.
Andrew:
But can some other method in the server (or from the client) also call the Data.lock() method?
SCJP2, SCWCD
Does this result say us my program works fine?
SCJP2, SCWCD
Originally posted by Adrian Muscalu:
Maybe. I don't see where you handled reentrancy. What happens if one record is beeing modified as another thread still waits for it?
Originally posted by Adrian Muscalu:
How can your unlock() implementation throw RecordNotFoundException? In the example you've provided there is no way. Unless you are just trying to prove a concept and the code is only an example.
Originally posted by Philippe Maquet:
isLocked() returns false all the time
isLocked() doesn't need its while loop neither the wait() call
lock() should have such a while loop along with a wait() call
Is a Vector of recNos enough ? Don't you need to track lock owners ?
// Locks a record so that it can only be updated or deleted by this client.
// Returned value is a cookie that must be used when the record is unlocked,
// updated, or deleted. If the specified record is already locked by a different
// client, the current thread gives up the CPU and consumes no CPU cycles until
// the record is unlocked.
public long lockRecord(long recNo) throws RecordNotFoundException;
// 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;
I can change my code and place wait in lock(), but generally are you agree
we need not track owners?
// 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;
SCJP 1.4, SCJD, SCWCD 1.4
Depending on your own version of the instructions, I could reply : yes, no or maybe.
So after you'll have posted them, we'll - hopefully - have some more useful discussion.
I can see the benefit of using a lock cookie to ensure that the client that locked the record is the one doing the modification,
So would we be talking about not exposing the interface to the Client as specifed, but wrapping it in an Adapter class? If so, should we therefore go the whole hog and hide everything in a Buisness Tier
No need to remember owners?
Originally posted by Philippe Maquet:
...Locks a record so that it can only be updated or deleted by this client....
How could you ensure this if you don't track lock owners ?
Now just a few comments about the code you posted (as you posted it, I guess I may comment it ) :
isLocked() : You don't need the if-else construct.
lock() : You probably read Max's book and it's a great one. But notifyAll() is useless in lock().
book() : What's the int values you return ? What about making it void and just throwing exceptions to signal exceptional cases ?
...Locks a record so that it can only be updated or deleted by this client....
How could you ensure this if you don't track lock owners ?
Phillippe, thank you, but I think in one question you are not right...
To make your application more enhanced you will need to keep track of lock owners. Why? I would leave it for Phil, because it is his "favorite story"
Best,
Vlad
Don't get me started about those stupid light bulbs. |