Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update, delete methods in Data.java

 
Ade Bams
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First I'll like to say hi and thanks to all the good people who take time out to answer questions on this forum. My question is about the implementation of my update and delete methods in Data.java. I am implementing my Data.java as a facade that hands off its operations to two worker classes - fileAccess and LockManager. my fileAccess takes care of all the file management and LockManager takes care of the logical locking. For my update method in Data.java it will look like this



This doesn't look right to me because the lockCookie is expected to be passed to the update method. Do I assume that the user of my Data.java will call lock() then pass the lockcookie to my method so that my method looks like :



I am working on URLybird 1.1.1

 
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
You have to develop the application, so you have to use the API (Data class) correctly. According to the instructions each call to update and delete, must be surrounded by a call to lock (which could return a lockCookie, depending on the interface you've got) and a call to unlock method
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well champ, the thing is, before updating or deleting a record in your system, it must be locked by a client. So, when someone calls the lock() method, you'll generate a long number (a System.currentTimeMillis() will do) and pass it to the caller. So, in this step, you'll have to save this info: recNo -> cookie. After that, the client who locked it will call the update() or delete() methods passing this number back, and here you'll verify if the info recNo -> cookie match. After that, you'll call the unlock() method, again using the number you generated in the first step.
 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Roberto fair answer you gave. But I still have a doubt, I know I have to make assumptions (it is part of the exam), but do I have to lock a record or lock the Java API object that I'm using to access de file (data base file).

Because, I have already developed the file access methods, and they work directly with the file through the Java API Object. So I figure, that this is the object that I have to lock, otherwise how would I lock just a single record?
 
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 Eugenio,

First of all your Data class has to be thread-safe, so it can be used in a multi-threaded environment.

Secondly when you want to update/delete a record, you have to call lock before you can update/delete the record. Why you have to follow this approach is excellently described in the ScjdFaq. In my Data class I keep a HashMap with record numbers and locking identifications. A record that is considered "locked", has an entry in this map. When the record is unlocked, this entry is removed from the map, and the record is back available for other threads/clients to be locked. In the update/delete method, the map is used to validate if the thread/client which tries to update/delete the record, is the thread/client which has locked the record (otherwise an exception is thrown).

Kind regards,
Roel
 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, thanks a lot for your answer.

I read the FAQ. So, know I understand that the lockRecord and unlock methods lock/unlock a record logically.

Thanks again.
 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, now that I am writing the lock related methods, another question arose. I was using the Facade approach(for the Data class) with two working classes. One class for the data access related methods, and the second one for the lock related ones. But the lockRecord method throws a RecordNotFoundException. So I figure that I need to check the database file in order to find out if the record number exists, if it doesn't, the exception will be thrown.

I already have a method that performes this search, but in the data access working class. If a create an instance of this class(data access working class) in the record locking working class, both classes would be tightly coupled, therefore what would be the point of having a Facade, but with two tightly coupled classes?

Could be a good approach just to implement the DBAccess interface in the Data class?
 
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
In my approach the Data class just implements the interface without using Facade pattern, so the Data class does all the work. But plenty of other people used a Facade, so they maybe can explain how they did it.
 
Eugenio Flores
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Roel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic