This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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.
 
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 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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • 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
 
Destroy anything that stands in your way. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic