Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S: locking conflict when using findByCriteria method from createRecord method?

 
Grary Stimon
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've raked over the posts, but, as often seems true of concurrency questions, no discussion is sufficiently close to my question...

For me, a B&S DuplicateKeyException is thrown from createRecord when the contractor name and location to be created matches one already in the database. I thought it natural to call my findByCriteria method from createRecord to test the condition for DuplicateKeyException.

Now, I'm concerned about a potential conflict between the locking schemes for findByCriteria and createRecord. My findByCriteria search code is protected by a ReadWriteLock readLock, while createRecord is protected by a writeLock from the same ReadWriteLock object. Can I call findByCriteria (readLock) from within createRecord (writeLock)?

Thanks for any thoughts,

Grary
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Grary,

Are you using the ReadWriteLock design pattern? From my understanding, depend on how you code your lock checking code, read lock and write lock would not interfere. But somethings the approach you use for the checking may cause deadlock during your situation.

Are you using a service layer or the Data class directly? Is your findByCriteria method in your service layer? The locking methods in the Data class? If so, you could simply do the duplicate checking in the service layer before executing the create method in the Data class.

In my implementation of URLyBird, I used my find method of my service layer to check for duplicate records, if duplicate pop up error else call create method. Hope this helps.
 
Grary Stimon
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K,

Thanks for your response.

No, the B&S instructions require the implementation of a specified interface, in which findByCriteria and createRecord are methods. So, my implementation cannot rely on my service layer. I think my concern is somewhat technical in nature, namely, using the same ReadWriteLock instance, can a method whose code is thread-safe by a readLock be called from inside a another method body protected by a writeLock? My concern is to honor the recommendation in Andrew Monkhouse's SCJD book not to nest locks!

Thanks,

Grary
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic