This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

isLocked (int recNo) is not used

 
Himai Minh
Ranch Hand
Posts: 1233
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I defined code for isLocked(int recNo) method in Data.java as a "must" requirement. I just realize it is not used. I use another similar method , not in Data.java, to check if the record is locked instead.
Just like create/delete methods. I never use them even though I write code for them.

Will I fail because I don't use isLock() in Data.java?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Will I fail because I don't use isLock() in Data.java?


Well, I think that it will depend if your locking mechanism works properly. If it doesn't, then you'll fail. I myself used the [tt]Data.isLocked(int)[/ii] method in my class that exposes the business methods remotely.
 
Roel De Nijs
Sheriff
Posts: 9838
103
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Will I fail because I don't use isLock() in Data.java?

No. Just like you won't fail because you didn't use create/delete methods. But you have of course to make sure your locking mechanism works flawless and your code may not result in a deadlock at all.
 
Robin van Riel
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About this getting the locking mechanism to work flawlessly, I'm still in the process of tackling this.

I've synchronized the relevant methods within the Data class and am currently using the following construction for the read, update, book and delete methods:

while rec x is locked => wait
- try
-- lock rec x
-- perform action on rec x
- finally
-- unlock rec x
-- notifyAll

The test class I'm using seems to be happy, but since I hardly ever use locking during real life projects, I hope I didn't overlook anything.
Thanks in advance for any feedback!
 
Roel De Nijs
Sheriff
Posts: 9838
103
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are completely on the wrong path. The lock-method should decide if a record can be locked or not (and if it's already locked, let the thread wait). The update/delete methods should just perform a check to see if the record is successfully locked (by the thread/client that's executing the update/delete request). The read-method just has to read the record, no locking provided. My Data class doesn't have a book-method (the book-method is a business service method, which is nothing more than a call to the update-method). And finally the unlock-method should release the lock on a given record and notify all waiting threads. But maybe I have misunderstood your explanation...
 
Robin van Riel
Greenhorn
Posts: 20
IntelliJ IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your fast reply Roel!

You were right, I was on the wrong path.
My main mistake was trying to implement the locking mechanism on a higher abstraction level, while I should have kept it as close to the file manipulation classes as possible.

I'm still doing something wrong (test class fails), but those remaining bugs will be squashed .
 
Himai Minh
Ranch Hand
Posts: 1233
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin Van Riel buddy,

I suggest this logic for your lock(int rec) method:
-- try
-- lock
-- while (isLocked(rec X){
current thread waits
}

-- perform action on rec x
-- finally
-- unlock
For your unlock (int rec) method:
-- notifyAll waiting threads

Read Roberto's test, he used this logic:
data.lock(1)
data.update(...)
data.unlock(1);

I recommend you to read the Java API about lock. There is a good example of lock and unlock.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic