This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Bodgitt and Scarper and design/synchronization questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Bodgitt and Scarper and design/synchronization questions" Watch "NX: Bodgitt and Scarper and design/synchronization questions" New topic
Author

NX: Bodgitt and Scarper and design/synchronization questions

S Perreault
Ranch Hand

Joined: Oct 29, 2003
Posts: 37
Thanks to all who read this post.
I have been assigned the Bodgitt and Scarper Contractor assignment.
(Please excuse my terminology. I am trying to explain it as simply as I can =D)
So far I have created a Data class that implements the DBAccess. Inside the Data class, I have a DataSchema class that contains the 'file header' information.
For my model, I have Contractor a class that contains all of the information to take a record from the database and create a Contractor. To connect the Contractor and the methods to access the database, I have a class called DBConnector that basically allows the Contractor class to use read/create/update/delete database methods.
What is my question? Well I have a few.
1) Is this a valid design?
2) Can we change the 'footprint' of the methods in the DBAccess class to include synchronized?
3) If no to Q2, would it be a good idea to synchronize the DBConnector class, or should I keep the synchronization inside the Data class?
Thanks for any help or ideas!
Perogi.
EDIT added NX: to the beginning. I am not sure if this is the newest exam or not.
[ October 29, 2003: Message edited by: S Perreault ]
[ October 29, 2003: Message edited by: S Perreault ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Perogi,
Originally posted by S Perreault:
1) Is this a valid design?

I think this is OK.
I am a bit confused where your Contractor class is. Is it a class that you are using to send data between the database and the client GUI, or is it a class on either the GUI or the database side?
Will you be using DBConnector to abstract your connection so that most of your client GUI is unaware whether it has a local or networked connection?
Originally posted by S Perreault:
2) Can we change the 'footprint' of the methods in the DBAccess class to include synchronized?

You should not change any of the method signatures or add new method signatures to the interface. However this should not be a problem for what you want to do ...
(Sorry, I am going to be pendatic here - hopefully you will see what I am trying to say.)
DBAccess is an Interface, not a Class.
Interfaces may never have the keyword "synchronized". In fact, the only modifiers allowed in an interface are "public, protected, private, abstract, static, strictfp". (refer to Java Language Specification). The JLS also states "Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.")
Which is a long way of saying that your methods in your Data class may have the "synchronized" modifier without you needing to change your DBAccess interface.
So what you really want to do is achievable.
Originally posted by S Perreault:
3) If no to Q2, would it be a good idea to synchronize the DBConnector class, or should I keep the synchronization inside the Data class?

See above
Originally posted by S Perreault:
EDIT added NX: to the beginning. I am not sure if this is the newest exam or not.

Yes, you do have an NX assignment. You may be interested in reading the JavaRanch SCJD FAQ, particularly the question How may assignments are there? (What is an NX assignment?)
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In fact, the only modifiers allowed in an interface are "public, protected, private, abstract, static, strictfp". (refer to Java Language Specification).
Um, this is somewhat tangential to the point of the discussion, but I think you're confusing modifiers of an interface with modifiers in an interface.
Anyway though - as Andrew says, an interface can never declare one of its methods to be synchronizized.
[ October 30, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
S Perreault
Ranch Hand

Joined: Oct 29, 2003
Posts: 37
Thanks sooo much!!
In response to your question

I am a bit confused where your Contractor class is. Is it a class that you are using to send data between the database and the client GUI, or is it a class on either the GUI or the database side?
Will you be using DBConnector to abstract your connection so that most of your client GUI is unaware whether it has a local or networked connection?

The Contractor class is in essense, solely on the Client 'GUI' side. The methods given to me in the DBAccess interface (it's only been a month since I passed the SCJP 1.4, and I'm already forgetting the basics ) return a String array. My DBConnector class will receive 'records' (which are individual instances of a Contractor) convert them to the necessary String array and use these String arrays on the database side. On the return trip, the String arrays will be converted into a Contractor 'record' and used on the Client GUI. More work than necessary? Probably, if not definitely.
Q: Should I create an Abstract Contractor class and take my current class and change it to a concrete "HomeImprovementContractor" class that derives from the Abstract Contractor class? That way, in the future, if someone has another type of Contractor, he/she could derive a new class and still use the common functionality of the Contractor class? Thanks for any insight.
And yes, the GUI will not know if the application is being networked.
This board has been an incredible help!
Perogi.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Shawn
Originally posted by S Perreault:
My DBConnector class will receive 'records' (which are individual instances of a Contractor) convert them to the necessary String array and use these String arrays on the database side. On the return trip, the String arrays will be converted into a Contractor 'record' and used on the Client GUI. More work than necessary? Probably, if not definitely.

It is probably more work than is necessary for this assignment.
But it is nice to have, and might make it easier for you later to learn how to deal with object oriented databases which will pass around objects like Contractors rather than database primitives.
Note that I said this would be easier for you, not that it would be easier for modification of your assignment to use such a database - there are probably going to be far more changes in going to an OO-RDB than could be hidden just by your use of the Contractors class here
Originally posted by S Perreault:
Q: Should I create an Abstract Contractor class and take my current class and change it to a concrete "HomeImprovementContractor" class that derives from the Abstract Contractor class? That way, in the future, if someone has another type of Contractor, he/she could derive a new class and still use the common functionality of the Contractor class? Thanks for any insight.

I would have expected the Contractor class to be fairly generic anyway, so I don't think that this is going to gain you much. If you want to do it so that you get more experience in doing these sorts of interfaces and coding to interfaces, then go for it (but I would think you have plenty of oportunity in the assignment anyway ). But I wouldn't bother doing it to try and meet some unknown abstract future change to the system.
Originally posted by S Perreault:
And yes, the GUI will not know if the application is being networked.


Regards, Andrew
S Perreault
Ranch Hand

Joined: Oct 29, 2003
Posts: 37
Although I haven't begun the GUI part, I thought that it would be easier to do getters/setters on one object than to call data[i] all of the time.
On the abstract part, yes, I would agree, the basic data for a HomeImprovementContractor could be a good base class for a Contractor. All of the data would be pertinent to a Contractor. You are also correct in assuming that I was trying to do it to help in any future changes/additions ;-)
Now for another question: Right now, my Data class can talk to the database as a whole but not as an individual record. For instance, let's say that I want to perform something like this:
data1.readRecord(2);
data2.readRecord(5);
data3.deleteRecord(4);
Currently, in my testing, these three method calls could take place in any order (3 different threads) but will take care of each method one at a time. Example:
Let's say that I have some sys outs dispersed to show me when a lock is set, unset, and queried. The above code would do something like this.
"data1 is querying record 2 to see if its locked"
"record 2 is not locked"
"data1 is locking record 2"
"data1 is reading record 2"
"data1 is querying record 2 to see if its locked"
"record 2 is locked by data1"
"data1 is unlocking record 2"
"data3 is querying record 4 to see if its locked"
"record 4 is not locked"
"data3 is locking record 4"
"data3 is deleting record 4"
"data3 is querying record 4 to see if its locked"
"record 4 is locked by data3"
"data3 is unlocking record 4"
"data2 is querying record 5 to see if its locked"
"record 5 is not locked"
"data2 is locking record 5"
"data2 is reading record 5"
"data2 is querying record 5 to see if its locked"
"record 5 is locked by data2"
"data2 is unlocking record 5"
Each read/delete (etc) is totally seperate from the other methods.
I am wondering if this is correct or if we need something more like this:
"data1 is querying record 2 to see if its locked"
"record 2 is not locked"
"data1 is locking record 2"
"data3 is querying record 4 to see if its locked"
"record 4 is not locked"
"data2 is querying record 5 to see if its locked"
"data1 is reading record 2"
"data3 is locking record 4"
"record 5 is not locked"
"data2 is locking record 5"
"data2 is reading record 5"
"data2 is querying record 5 to see if its locked"
"record 5 is locked by data2"
"data2 is unlocking record 5"
"data1 is querying record 2 to see if its locked"
"record 2 is locked by data1"
"data1 is unlocking record 2"
"data3 is deleting record 4"
"data3 is querying record 4 to see if its locked"
"record 4 is locked by data3"
"data3 is unlocking record 4"
Of course if data1 was accessing the same record as data2, then data1 (or data2) would need to wait for the other object to be finished with that record.
Any ideas or help here would be most welcome. Threading was by far the worst part of my Programmer's exam :-/
Thanks once again!
Perogi.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Shawn,
The problem with your first example is that it is relying on your clients to finish their unit of work very quickly. All other threads will be blocked until that unit of work is completed. This also produces a bottleneck that will not be alleviated by adding extra CPUs or adding a striped raid system - only one thread can be doing anything at any given time, no matter how much of the hardware is idle.
The second example allows other threads to continue working even if one thread has not released it's lock. It has better performance options.
The first example is closer to a system that stops dirty reads, but it still does not stop a client having dated data. If you were looking at isolation levels in configuring your database, you would be meeting the first level:

Note:
  • the Yes/No options indicate whether the "bad thing" can happen, not whether it meets the isolation level.
  • Dirty read in the database sense is a record that has been updated but not committed. For example, if you locked a record, updated it with the client ID, then rolled the transaction back (updated it again to have no client ID), then unlocked it - the client reading the client ID is said to have dirty data, since it has not been committed. This is different from having dated data, where it has read the data, and the data gets updated before the client does it's next read.

  • Now since your first option will not meet the "repeatable read" criteria, does it matter that you can read uncommitted data? (not that we really have a concept of commit / rollback in this assignment). I don't think that it makes any difference to us.
    In which case, my opinion is that we should allow the slightly better perforamance granted to us by using the second option.
    Regards, Andrew
    S Perreault
    Ranch Hand

    Joined: Oct 29, 2003
    Posts: 37
    Whoa!
    That is a crazy diagram. Thanks for taking out the time to create it.
    I totally understand that having a more multithreaded application is a better solution. I am wondering if the first example is valid.
    I would like to create a threading system like example two but all of my attempts have come to nothing.
    Back to the drawing board!
    Pele.
    p.s. Why do I keep reading that people are using a WeakHashMap? What are the advantages of using a WeakHashMap instead of a regular HashMap? I have read the Sun API and I still don't understand the benefits.
    [ October 30, 2003: Message edited by: S Perreault ]
    Adrian Muscalu
    Ranch Hand

    Joined: May 08, 2000
    Posts: 73
    Perogi,
    WeakHashMap is supposed to help you if you decide that its components should be removed with no mandatory action from outside. Basically, if you store (key) a reference to an "active" object (that is, has a "strong reference") than the WeakHashMap will keep it as long as the reference does not become "weak".
    You might want to consider it if you decide to implement a mechanism where clients (and their locks) get removed in an elegant way. Imagine a client that lost the connection with the server or just hanged. You can release the locks this client holds.
    This is my understanding of the class. As a personal note, I think its implementation is not an easy job.
    Hope I got it right from the API.
    Adrian


    SCJP2, SCWCD
    S Perreault
    Ranch Hand

    Joined: Oct 29, 2003
    Posts: 37
    Adrian,
    Cool. That sounds similar to what I thought(at least from what I got from the API).
    Thank you!
    Perogi.
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Hi Adrian,
    Originally posted by Adrian Muscalu:
    You might want to consider it if you decide to implement a mechanism where clients (and their locks) get removed in an elegant way. Imagine a client that lost the connection with the server or just hanged. You can release the locks this client holds.

    Well technically the JVM will release the locks that the crashed client held for you - you don't have to release them yourself (and that is the benefit of the WeakHashMap).
    Originally posted by Adrian Muscalu:
    As a personal note, I think its implementation is not an easy job.

    Implementing a WeakHashMap (that is creating your own WeakHashMap from scratch) or using the WeakHashMap within your application?
    Actually implementing your own version of a WeakHashMap is not too difficult - you can look at the source code to the existing WeakHashMap and see how it is implemented, and then you can easily build your own (say if you wanted a WeakHashList, or if you wanted the keys to be WeakReferences).
    As for using it in your application - that depends on whether your instructions require you to use cookies or not.
    If you don't need to use cookies, then whatever you use to identify the client within your lock(), update(), delete() and unlock() methods can be used as the key in the WeakHashMap. Very simple. You could also go a step further and check periodically whether any locks have been removed from that WeakHashMap to make it a more rounded application.
    But if your instructions require you to use cookies, then using a WeakHashMap is a little more troublesome.
    Regards, Andrew
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11404
        
      81

    Hi Shawn,
    Originally posted by S Perreault:
    Whoa!
    That is a crazy diagram. Thanks for taking out the time to create it.

    No problems - just hope it was understandable
    Originally posted by S Perreault:
    I totally understand that having a more multithreaded application is a better solution. I am wondering if the first example is valid.

    Yes, the first example is valid. And you could justify having that in your design decisions document if you wanted to.
    But if you can't get example two working at the moment, then you might want to persevere with trying to get it working, just for your own knowledge. Later you can make a design decision about which you want to use.
    Originally posted by S Perreault:
    I would like to create a threading system like example two but all of my attempts have come to nothing.

    Why? What sorts of problems are you experiencing?
    By the way, are you using Sockets or RMI?
    Regards, Andrew
    PS. Do you prefer Shawn or Perogi or Pele?
    S Perreault
    Ranch Hand

    Joined: Oct 29, 2003
    Posts: 37
    Hahahaha,
    I never realized that I signed off as Pele. (It's my avatar for NWN and EQ Pelemele Malef'carum.)
    My friends call me Perogi. My girlfriend calls me Shawn. (or our secret 'love' name ) The people who I've married call me Reverend. (I became one in order to marry people, not for any religious sense. http://www.ulc.org/ ) I guess Perogi would be the best for this forum. I will try to answer all posts with it
    I plan on using Sockets. IMHO, I believe that a junior programmer would believe that sockets is easier. (design decision validation) I was trying to figure out what a benefit (besides learning a new skill) RMI would give the project. Maybe someone out there could give me their thoughts on it.
    Last night I used a few 'sleep' calls to slow things down and it appears that my threading may be using diagram 2. The threads started to become intermixed once I slowed things down. Not enough to make two of the records be locked at the same time, however. I will need to perform more testing to figure out what is truly going on.
    Thanks Andrew!
    Perogi.
    Adrian Muscalu
    Ranch Hand

    Joined: May 08, 2000
    Posts: 73
    Hi Perogi,
    I wonder how you pronounce that.
    Threads vs RMI deserves another thread so if you are so kind to start it I promise to give my 2 cents.
    A+
    Adrian
    S Perreault
    Ranch Hand

    Joined: Oct 29, 2003
    Posts: 37
    Will do!
    Perogi.
     
    Don't get me started about those stupid light bulbs.
     
    subject: NX: Bodgitt and Scarper and design/synchronization questions
     
    Similar Threads
    Bodgitt and Scarper : Data class
    Bodgitt and Scarper : locking and unlocking
    NX: Bodgitt and Scarper
    GUI Part - Bodgitt and Scarper
    recNo in Bodgitt and Scarper