• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to implement Locking functionality?

 
Alberto Ivo
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have the follow interface:



all of those methods has a recNo as parameter, the problem is: I have just learned how to create Locking functionality inside create() or delete() methods ( lock.lock() ....... lock.unlock() ), but until now, I have no idea how to Lock only the record I want, with the methods above.

Can you tell some tutorial where I can learn this?

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

In fact there is no "best way" but there is a "proper way" given the given situation.

I have just learned how to create Locking functionality inside create() or delete() methods


If you know how to lock within create/delete methods then you are doing the locking within those methods. But the specs do say the locking is outside (except create because no recNum is passed in). Meaning you need to:



Do check your private message for your question to me earlier.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would actually be missing an important step in that process, namely verifying that the record is still available before doing the update. See the JavaRanch SCJD FAQ question "Why do we have lock() methods in the Data class?"

Regards, Andrew
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alberto,

I'm not having much luck finding a simple tutorial that will help explain the concepts of locking a record for you (unless, of course, you want to buy a book on the subject) . I will see if I can give some hints without giving away the entire solution.

Looking at your interface:


A simple way to handle this at a high level is to have a collection of locks - you could have a map between every record that exists and its current status (locked or not), or you could simply have a set of records that are locked. Each of the methods could then interact with that collection in a thread safe way.

See if you that makes sense, and come back with more questions if needed.

Once you are past that (and only when you are past that), you might want to consider the instruction "Locks a record so that it can only be updated or deleted by this client" (check if you have that in your instructions - I would be surprised if you didn't). How are you going to ensure that only a particular client can perform a particular operation? You might want to avoid the entire challenge, and declare that you are only providing business logic methods to the client software - this is a valid design decision, but one I don't like. The alternative though would require a change to the type of collection used above and how your final implementation will work. But that must wait until after your initial concepts work.

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic