Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
* Originally when a client wanted to reserve a record, I'd allowed
the record to be locked before the GUI displayed a dialog box
requesting the customer id. This meant, however, that if the user
opened the dialog box and walked away, the record would be locked
indefinitely, and so would other clients wanting to reserve that
record. Instead I changed that piece so that the customer Id must
be input prior to the record being locked.
SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Originally posted by Oliver Roell:
Anton: Your deadlock-definition sounds good, but I think my question "I don't understand, how a deadlock can occur" was a little bit misleading. It would be better to ask "I don't understand, whether a deadlock can occur in my design", because there are many discussions and solutions in this forum to prevent deadlocks. Thus, I was not sure whether I must implement explicitly one of these "deadlock preventing solutions".
a)
1. Client A locks record 1.
2. Client B locks record 2.
3. Client A try to lock record 2 and wait().
4. Client B try to lock record 1 and wait().
b)
try {
long lockCookie = data.lock(recNo);
data.update(recNo, data, lockCookie);}
catch (Exception ex) {
throw ex;
}
finally {
data.unlock(recNo, lookCookie);
}
Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
If you throw an Exception from within the catch block, finally will not to execute because control is transferred elsewhere. Therefore, the record is never unlocked. Finally is not guaranteed to always execute, by the way, especially if there is an exception thrown from catch or there is a return statement in try or catch.
Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I learn something new everyday.
Both rethrown exception and return still have finaaly execute.
I wonder what happens after finally executes: does control get transferred as expected?
![]()
But I'm always not sure what could happen that thread 1 or 2 in the above example are never come back to unlock the records.
Robert, the scenario you describe supposes a "fat client" design, while Oliver chose the "thin" one (db and business tiers server-side).
Your dialog-box to insert the client-id is an interesting design-choice. I think about another way to insert the client-id: a text-field in the main-GUI. But I'm not sure whether I should use a text-field or a dialog-box. The dialog-box looks better. But to handle a text-field in the main-GUI is easier to program (I'm a lazy guy, I know). Why do you have choosen the dialog-box rather than a simple text-field?
SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
SCJP 1.4, SCJD
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|