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

Locking assumption + possible typo?

 
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've been working on my Data class and have two queries;

1) Is it ok to assume that the thread which locks a record will also be the thread that unlocks it?

I've got tests which work when the same thread locks a record then releases it but cannot get a test working when one thread requests the lock and a separate thread releases it. In the latter case, I've got 6 threads requesting 3 locks, and 6 releasing those locks. Occasionally, my LockManager throws a Security Exception (not java.lang, but my own) saying the wrong the given cookie hasn't got the lock I'm trying to release.

2) In my problem description I have the following line;

"2 byte flag. 00 implies valid record, 0x8000 implies deleted record"

should the 0x8000 be 0x800? 0x8000 is 32768 while 0x800 is 2048. In Java a short (2 byte number!) can be up-to 32767 (2^15 -1) so 0x8000 cannot be read into 2 bytes! I think 0x800 is the way to go since I've got the data file read in using that.

Have I completely missed something here?

Regards,

John
 
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
Hi John,

1/ If you have an interface with a lockCookie, it's easy: the record can only be unlocked with the same lockCookie, generated when locking the record. Your assumption "the thread which locks a record will also be the thread that unlocks it" is wrong, because if you use RMI you will not have that guarantee.

2/ It would be really weird if it is a typo, because these assignments are used for a really long time I think this thread contains valuable information for you.

Kind regards,
Roel
 
John McParland
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

thanks again for the prompt reply. Re;

1) The problem was in my tests, I wasn't sleeping in the releaser threads between attempts to get the cookie from the requester threads. This and simplifying how I set up the test fixed it.

2) Ah I tried some other methods of RandomAccessFile out from what I was already using and it works! A little more in-depth reading of the API was required.

Thanks again,

John
 
Roel De Nijs
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
Glad I could help and to hear you were able to fix your issues

Have a nice weekend!
Kind regards,
Roel
 
Good night. Drive safely. Here's a tiny ad for the road:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic