Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

isLocked (int recNo) is not used

 
Ranch Foreman
Posts: 1898
12
  • 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?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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 Foreman
Posts: 1898
12
  • 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.

 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic