• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The method public void lock(int recNo) throws RecordNotFoundException does not make sense to me

 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I am struggling to implement public void lock(int recNo) throws RecordNotFoundException.
My problem is what is the int recNo for ?

My plan is to synchronize on the database object. All the methods that are responsible for manipulating the database will syncronize on the database object preventing any data corruption issues caused by multithreading.

I went through Danny's DVD source code, there I saw a solution that makes use of locking. On Denny's DVD there are no record numbers, so they used locks on the map that stores location of records in the file. In my case I have record numbers. I also do not keep a copy of the database in main memory. Does this mean I still have to make use of the recNo even though I do not think I need it. I plan to have an empty method body and have the public boolean isLocked(int recNo) return false always.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mxolisi,

If you want to pass SCJD, I'm pretty sure you'll have to implement Sun's interface completely and take the comments by each method into account.

The lock and unlock method have another purpose than preventing data corruption. First of all you have to make sure your Data class is thread-safe, this can be done by (like you say) synchronize on the database object. I made my Data class thread-safe by applying the singleton design pattern and mark every method in Data class as synchronized (in my opinion the easiest approach possible).

The purpose of lock and unlock methods are perfectly described in the ScjdFaq, so just read this (even better just read the complete faq, because it contains valuable information for someone starting with scjd). Implementations of lock/unlock is definitely needed!

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Mxolisi. Welcome to JavaRanch, champion!

My problem is what is the int recNo for ?


Well, first, you have to call lock(int) in order to lock one particular record, so that only the client that locked it can update or delete it. This is to avoid one client overwriting data provided by another client (please take a look at the link provided by my good buddy Roel). So, the lock(int) method is how you say "only this particular client can update/delete it". You may also think of the lock(int) method as a way to begin a transaction (something like javax.transaction.UserTransaction.begin()).

My plan is to synchronize on the database object. All the methods that are responsible for manipulating the database will syncronize on the database object preventing any data corruption issues caused by multithreading.


Ok, that's a valid approach. You have to somehow guarantee that the Data object is Thread-safe, so it's ok.

Does this mean I still have to make use of the recNo even though I do not think I need it.


Well, you are going to need it.

I plan to have an empty method body and have the public boolean isLocked(int recNo) return false always.


Hum... are you saying that the body of the lock(int) method will be empty? Please don't do that! I'd say that, in this case, you will fail. Also, you also have to provide a valid implementation for the isLocked(int) method. This method has to inform whether a particular record is locked, given its number.

And finally, you have to think about reuse. For instance, if a method of another application expects the interface that was provided to you in the assignment, it should be possible to use your implementation of it, and it should work as expected (or, how the comments provided in the interface suggest).
 
Mxolisi Veco
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roeland Roberto. Thanks for the information.

Let me go through the http://faq.javaranch.com/java/ScjdFaq and especialy the topic "Can't we synchronize the update() method and ignore the lock() methods? Why do we have lock() methods in the Data class?"

After reading this I should have a better understanding.

Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic