This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
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

B&S Locking

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Would somone please provide some suggestions on how to deal with multiple users updating a file? My B&S application displays a list of contractor(s) based on the criteria provided. To reserve a contractor, the user selects a contractor from the table. A popup window displays the selected contractor information along with an input field for the customer number to reserve the contractor. This customer number would then need to be persisted to the file.

When should I lock this record and how should I go about it? It seem logical to me to lock the record once the user has selected the contractor, however this would lock the entire file from being updated? Therefore leaving other users waiting for access to the file. Perhaps I should only lock the record once the user has submitted the customer number? However, this doesn't seem appropriate, or does it?

Is there an option to lock a specific record in a file? My application caches all the contractor in a Hashtable, should I create some record locking on the Hashtable? I'm quite confused and would appreciate some guidance.

Thank you.
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that there are many ways to do this, I am running a
multi-threaded Server using sockets

This server has a static instance of my remote data access class, note the
remote data access class is a bunch of serialized wrapper methods around
my local data access methods

The remote data access access class has static synchronized list in it

// synchronized list to hold an array of RecordLock Objects
private static List recordLockList =
Collections.synchronizedList(new ArrayList());

when I create my sub-instance of the the server I pass in a generated
Sequence Id

// inside run method of server
Socket client = serverSocket.accept();
synchronized (this) {
Connect c = new Connect(client, remoteDataAccess, ++Server.sessionId);
System.out.println("Accepted a connection from: " +
client.getInetAddress());
}

(note: sessionId is a private static int var in Server)

Inside the constructor of Connect which extends Thread I set the
name of this Thread to the passed in sessionId,

client = clientSocket;
this.sessionId = sessionId;
this.setName(new Integer(this.sessionId).toString());
this.remoteDataAccess = remoteDataAccess;

that way I can stuff the list full of RecordLock class
objects defined below below

//-----------------------------------------------------
final class RecordLock {

private int recordNumber;
private String ownerOfLock;

RecordLock(int recordNumber, String ownerOfLock) {
this.recordNumber = recordNumber;
this.ownerOfLock = ownerOfLock;
}

public int getRecordNumber() {
return this.recordNumber;
}

//-----------------------------------------------------
public boolean equals(Object o) {
boolean areLocksEqual = false;

RecordLock rl = (RecordLock) o;

if ( rl instanceof RecordLock ) {
if ( this.ownerOfLock == rl.ownerOfLock &&
this.recordNumber == rl.recordNumber) {
areLocksEqual = true;
}
}
return areLocksEqual;
}
}

//-----------------------------------------------

I can't tell you more because that would be giving the store away,
it took me three days to figure this and the rest out, however this
should give you some food for thought, like I said before, there
are many ways to implement the locking routines, hopefully this helps
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,
Wow! I guess I should have mentioned the fact that I'm barely an intermediate programmer. By doing this certification, I was hoping to really stretch myself and boy have I! With a great deal of apprehension practically every step I take in completing this certification, but the key is that I'm learning a lot.

I was barely able to understand what it was you were doing´┐Żbut this is good, something to look into and understand. This will give me some idea of what I could do.

Thanks for your feedback.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shannon
Originally posted by Shannon Sims:
It seem logical to me to lock the record once the user has selected the contractor, however this would lock the entire file from being updated? [...]Is there an option to lock a specific record in a file?
I think the second quoted question answers the first one - if you can lock a specific record then you do not have to lock the entire file.

Check your instructions - you should have something in the "Server" section along the lines of:

(Note: method signatures may vary, and the "isLocked" method may not even exist)

But the lock and unlock methods do require logical record locking. They do not require physical locking of the file.

Having a collection containing record numbers and the logical owner of those records is almost certainly the way to go.

Note that Thomas Paul Bigbee has a serialized objects over sockets solution, which allows him to identify clients via the thread (or the thread name). However if you are using RMI you cannot use threads for identification.

I have deliberately only given hints here - I also don't like to give too much of the game away .

Regards, Andrew
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint:

Because locking a record will block that particular record for other clients (block them from lockimg it, that is), you will probably want to lock records for the shortest amount of time.

Frans.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew and Fran. I appreciate the hints especially since I decided to take the certification to learn and understand the entire development process. Getting the answer would definitely hinder my learning experience along with the fun of figuring out the solution. Now I will need some time to absorb this information and see what I can come up with.

Thanks again!

This forum is the WONDERFUL!
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My original feedback had
(this.ownerOfLock == rl.ownerOfLock) in the equals method, my original stand-alone design was using objects (Thread.currentThread()), when I had to conform to the interface I had to use the name of the thread (Thread.currentThread().getName())

this.ownerOfLock == rl.ownerOfLock should be repaced with
this.ownerOfLock.equals(rl.ownerOfLock) or equals will always return false, sorry for the incorrect code, I should have checked first.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,
Basically, your code has given me some food for thought and that's what I needed. Thanks for the correction and the code sample.

By the way, in reviewing your code, I noticed you were doing a check that might not be necessary??? I noticed you're checking to see if "rl" is an instance of RecordLock. It seems this check isn't necessary since "rl" is declared as a RecordLock and you are casting "o" as a RecordLock. In addition, have you thought about surrounding this code in a try/catch block as this could throw a CastClassException if "o" isn't of type "RecordLock"? Just thought I would bring this to your attention...in case you didn't notice this.

Originally posted by Thomas Paul Bigbee:

//-----------------------------------------------------
public boolean equals(Object o) {
boolean areLocksEqual = false;

RecordLock rl = (RecordLock) o;

if ( rl instanceof RecordLock ) {
if ( this.ownerOfLock == rl.ownerOfLock &&
this.recordNumber == rl.recordNumber) {
areLocksEqual = true;
}
}
return areLocksEqual;
}
}

//-----------------------------------------------


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