Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CPU cicles in lock method

 
John Oconnor
Ranch Hand
Posts: 67
Java Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi for all,

In my DB interface (and assignment) have the next indication:




I have made the next(suppose two clients try lock some record)

client A lock recNo 1;
client A return lock cookie;
client B try lock recNo 1;
because recNo 1 already is lock, for client B thows a SecurityException where indicate that is already lock.


I am ok?

Thanks
 
Roel De Nijs
Sheriff
Posts: 9780
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With such an approach you will automatically fail, because you are violating a must-requirement defined by the interface you have to implement!

clientB has to wait and when record 1 is unlocked by clientA, clientB will be notified and tries again to lock record 1.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Oconnor wrote:I am ok?


Hum... no. Because, if record 1 is locked by client A, then when client B tries to lock it, it must not consume CPU cycles... with this approach of yours, it is consuming CPU cycles. In the lock method, while a record to be locked is already locked, then the client that is trying to lock it must wait().
 
John Oconnor
Ranch Hand
Posts: 67
Java Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:while a record to be locked is already locked, then the client that is trying to lock it must wait(). .


Ok, so I have only that call a wait(), while my records lock map contains the record number. In a syncronized context clear.


OKOKOK, I am wrong, but, now while i have re-view the code, I have worked with a ReentrantLock, for this reason, the first thread that take the lock, work fine until lock.unlock.
So allways if there are anothers theads try take the lock canĀ“t because already is lock, only when the current thread release the lock, another thread can execute the lock block code.

Now this approuch is like SCJD exam with J2SE by Monkhouse.

in my block code, is verify that record number is a valid record in database through read(recNo). but I trhow a securityException when record is locked with another lockCookie

this is the reason because I have throw a SE if already is lock some register for a second client.
So i have not consuming cpu cicles thanks to use lock.lock and lock.unlock (reentrantLock) and also i have validate that map contains a lock cookie for fist client that obtain the lock.

sorrry for my fist expose in post, but is good have a re-view for code, at this moment i have re-view all!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic