Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Locking confusion -- help

 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I got confused about the locking mechnism:

in the DBAccess, I implemented sync blocks in lock, unlock, update, and delete. all on one static map of cached records.

in the business logic (with thin client so the logic is on server), I have book and show, where book calls the sequence of DBAccess: lock -- read -- compare -- update -- unlock.

however, these operations on the business layer are not sync. So I think there is a hole between the sync operations, because even business layer called lock, it becomes unsync'ed after exiting its synchronized block. May I miss something here: should I put the book in the business to be synced?

Please help. Thanks.
 
Anton Golovin
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Zhu:
Hey, I got confused about the locking mechnism:

in the DBAccess, I implemented sync blocks in lock, unlock, update, and delete. all on one static map of cached records.

in the business logic (with thin client so the logic is on server), I have book and show, where book calls the sequence of DBAccess: lock -- read -- compare -- update -- unlock.

however, these operations on the business layer are not sync. So I think there is a hole between the sync operations, because even business layer called lock, it becomes unsync'ed after exiting its synchronized block. May I miss something here: should I put the book in the business to be synced?

Please help. Thanks.



No, because the record in question is locked and cannot be modified by any other thread.
 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Anton:

I am trying to clear my mind: in lockRecord, sync is on the map of locked record. It that recNo is already in the map, then that thread in book of business logic will go to wait for the locker. If it is not, then the thread will obtain that locker and other thread won't lock on it. Ooh, even though the lockRecord exits its sync block, the locked map already booked it and this is how it reaches atomic op in business layer. Thanks, Anton.

One more, how did you implement sync on modifiable operation at DB access: record level or database level, or simply the file pointer? I implemented on the database level, and would like to know if this will result deduction of marks.
 
Anton Golovin
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Zhu:
Hey, Anton:

I am trying to clear my mind: in lockRecord, sync is on the map of locked record. It that recNo is already in the map, then that thread in book of business logic will go to wait for the locker. If it is not, then the thread will obtain that locker and other thread won't lock on it. Ooh, even though the lockRecord exits its sync block, the locked map already booked it and this is how it reaches atomic op in business layer. Thanks, Anton.

One more, how did you implement sync on modifiable operation at DB access: record level or database level, or simply the file pointer? I implemented on the database level, and would like to know if this will result deduction of marks.


Sync on modifiable operations in data logic was by synchronizing the method itself (find, delete.) Sync on business logic (findRoom, bookRoom) was achieved via some more interesting operations. I hope they do not delete this post, and hope it is useful for you:



 
Andy Zhu
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Anton, for your posting. No, my last question is about the access level, my implementation is lock on the cached map (I think you mentioned a cache for this performance in another thread, but I take it as an object for locking mechnism). so it coms to me, if sun will like it or not, since it seems that I locked whole db for modifiable operation. I guess I may did too much. But I am not certain and want input about this.

Thank u.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic