OK, client crash can leave locked record...I'm agree.
How "WeekReference" can store references to clients?
Originally posted by Philippe Maquet:
When you think "db", just think "db" : the interface you were provided and its comments. Don't think how you will use that interface, but how anyone reading it could use it.
You need [to track owners] IMO. While you have no specific comments for update() and delete() as I have, your provided lock() method comment is clear enough :
"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 ?
Originally posted by Peter Kovgan:
Cause while client "B" has the lock, client "A" inside the while loop,
periodically call vector.wait and do nothing.
Only client, which has the lock can do unlock, cause
lock() and unlock() called from the same method book().
Client "A" can't exit from while loop, while client "B" has the lock.
You are half right - in your usage of Data class in your server you are safe.
However any other programmer who comes along and decides to use your data class directly will be in trouble - they expect that the contract of the interface has been fulfilled, when it hasn't. Sun didn't just give you a data file and tell you to work with it, they gave you an explicit contract to fulfill in the form of the interface. This to me implies that they intend to have other applications written to use that interface.
And reading Max's book I did not find any attempts to avoid deadlock as result of clients crash.
Max believes that we only have to deal with problems that can occur in normal operations - so ensuring your code does not cause deadlock, that sort of thing. He believes we do not have to worry about intangible problems - network crash, computer crash etc. I can understand this perspective, and certainly people have passed with very good scores without worrying about handling crashed clients. However I think handling client crashes can be done easily (if required by the server you are writing), and doing so makes the code more robust.
If you have your book() method on the server side (which you must since you are not tracking owners of locks) then you should be safe: if you are writing a Sockets solution, your code can unlock records if the client crashes; if you are writing an RMI solution, then the book() method should run to completion, which will result in the record being unlocked anyway.
I try to keep things simple and don't create connection factories, additional objects and so on, and ask you : is it generally enough for the project if I'll build the model as Max showed ih his book?
What you should be concerned about is whether you meet the criteria of your assignment. I know of three major assignment types, each of which has multiple versions, with each version having minor differences. There is no way Max could write his DVD application in such a way that it exactly matches the requirements of any assignment, let alone all of them.
Which is a round about way of saying that I think you can follow the general concepts Max used, but you will have to verify for yourself that your solution will be acceptable, and you will have to adapt Max's sample code to meet your requirements (or throw out Max's code where it just doesn't fit your requirements).
Regards, Andrew
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
// Locks a record so that it can only be updated or deleted by this client.
SCJP 1.4, SCJD, SCWCD 1.4
SCJP 1.4, SCJD, SCWCD 1.4
SCJP 1.4, SCJD, SCWCD 1.4
Client A has the lock, Client B waits, Client A crashes (and hence the lock is released). Now how does the Client B get the notify? I guess it's not sufficient to just let Client B wait around until Client C comes in and calls notifyAll!
I'm also a bit confused about this mutex stuff
Originally posted by Peter Kovgan:
My "Contractors" has very idiotic interface
Originally posted by Peter Kovgan:
Have you some thread or link which can help me to make fat client?
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 Stephen Galbraith:
Whilst I like Andrews Server side Buisness tier logic ensuring that any Client side crashes will not cause record lock, I still have a nagging feeling when reading the instructions.
...
Hence Phillipe comments look very important, and we can't leave it up to the BuisnessTier (which I like!)
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 Stephen Galbraith:
[The instructions] explicity say in the db interface that the interface needs to be
which to me means that my Data classs must ensure this. Else any programmer that in the future comes along and just reads the interface and notices that the data class implements this will assume that contract is met by the data class.
Originally posted by Stephen Galbraith:
if each Client instantiated a new Data class on connection then I guess we could use the Data instance as the value in a WeakHashMap with the recNo as the key to ensure this.
Originally posted by Stephen Galbraith:
I am thinking that the situation [Vlad is] alluding to is where Client A has the lock, Client B waits, Client A crashes (and hence the lock is released). Now how does the Client B get the notify? I guess it's not sufficient to just let Client B wait around until Client C comes in and calls notifyAll!
Originally posted by Vlad Rabkin:
I must admit I didn't think how to notify waiting threads in case of WeakHashMap (since I never considered this solution).
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
[Stephen:]
Whilst I like Andrews Server side Buisness tier logic ensuring that any Client side crashes will not cause record lock, I still have a nagging feeling when reading the instructions.
[Andrew:]
Did we start arguing for each others position accidentally?
If they do not match, you update your count and call notifyAll(). You might be interested in reading the topic "Single table / Simple Locking - WeakHashMap vs WeakReferences".
....I suspect you are asking what unique object can you use to identify the client with. In which case the answer depends on whether you are writing a Sockets or an RMI solution. If Sockets, then you can probably use the thread that you create for each connection. If RMI you might want to have a ConnectionFactory that creates a new remote object for each connected client - in which case the instance of the class becomes your unique identifier (if you search for "ConnectionFactory" in this forum you will find out more about it).
SCJP 1.4, SCJD, SCWCD 1.4
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|