File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Why is locking needed? 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 "Why is locking needed?" Watch "Why is locking needed?" New topic

Why is locking needed?

binu sk

Joined: Nov 17, 2006
Posts: 2
Hello All,

I am required to implement lock/unlock methods for the data access class. Tried to test the application I have developed so far (locking not implemented, implemented rmi part and swing client) with multiple clients connecting to RMI server. I Don't find any issues, there is no data corruption. Records get updated correctly. My question is, what difference it makes when I implement locking?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
apart from an automatic failure if you don't you mean?

If you do a proper test you'll soon find that if you don't lock there's a very real chance of loosing data.

Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Correct me if I'm wrong someone but here's an example of why we want to lock records:

CSR 1 wants to update record x
Half way through the process his thread is put back in the queue
CSR 2 wants to update record x
Now the database is in a inconsistant state and CSR won't successfully be able to update record x.

SCJP 1.4
Miles Toliver

Joined: Dec 07, 2005
Posts: 12

I just posted a related topic. Not sure exactly what binu sk means, but perhaps I can expand on his question. I think we all agree that a synchronization mechanism is required so that all record modifications are atomic and don't step on one another. This would be at the java thread level.

However, our projects require us to implement a logical locking framework to lock records. Consider a project implemented with the following characteristics:

1) There is a synchronization mechanism in place that ensure mutual exclusion between java threads for critical sections (i.e. record updates).

2) It is acceptable to have "stale" data appearing on a client's GUI. For example, there may be records displayed on a client's GUI that have since been updated by another client. Therefore, whenever an update is made we check to ensure that the record we are updating is still in the same state. If it is not then we throw some sort of exception.

3) Project implements "thin" clients. Meaning that for records updates, there is an update() method on the server that does something like this:

public void update(String[] newData)
long lockToken = dao.lock();

if (recordHasBeenModified() == true)
throw new ModifiedRecordException();

dao.update(newData, lockToken);

Given these assumptions, would you really need a logical locking mechanism? Would you not just need to synchronize access to the update method above?

I realize we "must" create the logical locking mechanism, so this discussion is pretty much moot, but wanted to see if I was missing something.
[ November 17, 2006: Message edited by: Miles Toliver ]
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
you can try the testData class that i posted recently. See if your code does not break after that!!

-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
binu sk

Joined: Nov 17, 2006
Posts: 2
The problem is that I am not able to break the code (code without any locking stuff), so when I write the lock/unlock methods I will know why I am doing it..

Will try now with the TestData class posted by Sam Codean.

Hopefully I can break my code with this!
I agree. Here's the link:
subject: Why is locking needed?
It's not a secret anymore!