File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Bodgitt and Scarper : Data class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Bodgitt and Scarper : Data class" Watch "Bodgitt and Scarper : Data class" New topic
Author

Bodgitt and Scarper : Data class

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi,

I have few questions regarding Data class:

1) My Data class must implement given interface. But can I have extra methods in Data class (all my extra methods are private. only one method is public - which is used to display all records)? I read a post by Andrew Monkhouse (to a candidate who failed):

your unlock method is calling an openFile() method and a setUserId() method that are not in any interface I have seen


I've written another class to handle locking mechanism, and I'm calling those methods via lock and unlock method from give interface. Is it OK?

2) Update method does not throw DuplicateKeyException, so theoretically, there is possibility of 2 exactly same records. But as per requirements, I only need to 'book' a record, which mean only one field of that record would change (fortunately, I'm not using that field as part of primary key). So can I ignore this issue? (because practically, no user can change name, location etc. right?)

3) What is purpose of isLocked method? If client A locks record 1 and client B tries to lock it, B will wait till A releases the record. This all can be achieved simply by await/signal in lock/unlock and I don't even have to call isLocked method. On the other hand, If I call isLocked before attempting a lock and isLocked returns false, what should I do?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi,
Going further, how to treat exceptions from Data class? e.g. lock throws only RNFE. However, my code inside lock might throw InterruptedException. How should I handle those? Should I create a child class of RNFE and wrap InterruptedException inside it? Or should I swallow it (its a bad idea though)?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

1/ Why would it not be allowed to have private methods in the Data class. The must requirement is to implement the given interface and having private methods will not violate that must requirement.

2/ In a normal application you can only have a DuplicateKeyException when you create new records, because you would not allow to update some primary key fields.

3/ The purpose of the isLocked method is to check if a record is locked. So in my application when the record is locked, the method returns true; otherwise false.

4/ There are many threads about how to handle the InterruptedException, so I would suggest using the search engine of this forum and I'm certain you'll find a satisfactory answer


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Thanks Roel.

As now my data code is about to finish, one of few things bugging me is:

How should I restrict a client from locking more than 1 record?

Since I'm having a thin client, client itself will not lock the record. Only server can lock/unlock records.
Also, when client makes an 'update' request, it won't be able to send another request before this request completes.
Thus, theoretically, client cannot have more than one locks (at any given time), and I don't have to worry about this issue. Am I right here?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

But your Data class could also be used without your business service, via another application. Don't forget about that scenario. So your Data class should be thread-safe on its own, so it can function flawlessly without your business service.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Don't forget about that scenario.


Thanks Roel.

But the question:
How should I restrict a client from locking more than 1 record?

still remains.

Basically, what should happen in such case (same client trying to obtain another lock)? Since its the decision made by us (i.e. by candidate - not stated in requirement), what about request from same client for locking other record? Should I show error message etc.?

Since I'm using RMI factory, my Data class instance (object) would work as client identifyer. How about keeping a boolean variable at object level (non-static) to check if current client can obtain a successful lock? That way, I can identify if same client is requesting for another lock(flipping the value in lock and unlock). But as I asked above, what should be my response to such request?

Thanks again.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

I throw an IllegalStateException when that happens
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Roel De Nijs wrote:But your Data class could also be used without your business service, via another application. Don't forget about that scenario. So your Data class should be thread-safe on its own, so it can function flawlessly without your business service.

Hi Roel! I have Bodgitt and Scarper assignment too but I didn't find the requirement you're talking about in the instructions. Can you please share your thoughts about using the Data class with another application?? Did I miss something?

Thanks,
Dmitry


OCMJEA, OCJP, OCJD, OCE SQL, OCP Forms Developer, OCP PL/SQL Developer
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

It's not mentioned in the instructions directly, but there is some requirement about reusablity and extensibility. So I adopted these design principles in my application in my GUI (easily add new functions or new search criteria), but also in my Data class. You can re-use the Data class to manage a (similar) database file containing hotel or customer information. And because my Data class is thread-safe it can also easily be used (without any change required) in other applications where they don't want to use RMI, but prefer sockets or a web-based application.

But I'm convinced that your Data class should be completely thread-safe and so your class should not fail when using Roberto's Data class test.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Hi Roel! Thanks for your time!
Correct me please if I got you wrong. You've made two things:
1) You've implemented your database server with multiuser support and the Data class as a mediator between the GUI and this multiuser database server
2) You've made your Data class thread safe because it can be used by some application, but your Data class doesn't contain any server logic (I mean handling user connecttions, locking mechanism implementation).

Am I right?

Thanks,
Dmitry
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

My Data class implements the given interface, so all methods are implemented according to the comments in the interface. The Data class is thread-safe, so it can be used in a multi-threaded environment. For the assignment I took a thin client approach, so I also have a business service (and its implementation). This business service is exposed to the client through RMI (so clients invoke find, book,... methods) and uses the Data class (to invoke methods like lock, update, unlock, read, find,...). In the business service implementation I don't have to take care about multiple threads, because my Data class is thread-safe.
For another application the Data class can be used with a web front-end or another client application where sockets is chosen as network protocol. You just can write the web front end (and maybe a few controllers) or the sockets protocol and you don't have to worry about the Data class, you can use it as-is and you are guaranteed it's thread-safe.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Hi Roel!
My solution is close to its logical end. I understood you but I went another way and my Data class implements the interface given by Oracle, but it only invokes corresponding methods of my business service and all the logic and thread safety are implemented in the business layer. The difference of my approach and yours is location of the Data class. In my approach the Data class is located at the client side and in yours your Data class is at the server part of the application (RMI is the line dividing the application). Is my approach wrong?

Thanks,
Dmitry
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Your approach is very weird! You are the 1st one with such an approach. So it would be interesting to have a view on why you decided Data class is front-end class and develop a business layer. Because every other developer implemented Data class as the DAO (data access object, interacting with the database file). In front of the DAO they have a business service (for a thin client it's on the server-side, for a thick client it's located at the client). And finally a GUI with interacts with your business service.

I'll guess you have opted for a thick client approach? Because otherwise having Data class client side would make no sense at all. It's also weird to have a class that's in the db-package at your client. Mostly you would expect it server side. I'll guess your Data class is nothing more but delegating the methods to the business layer?

And because the section about the Data class is located in the Server section of the instructions, I'm inclined to say that your approach is wrong and will result in failure, but that's just my thought.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Hi Roel!
So it would be interesting to have a view on why you decided Data class is front-end class and develop a business layer.

I see only two reasons there are rush and inadvertence. I'll do my best to avoid them in future.

I'll guess you have opted for a thick client approach?

Yes, you right.

I'll guess your Data class is nothing more but delegating the methods to the business layer?

Yes, you guessed

And because the section about the Data class is located in the Server section of the instructions, I'm inclined to say that your approach is wrong and will result in failure, but that's just my thought.

Thanks Roel, now I need some time to fix it according your comments.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Hi Roel!
I've spent some time thinking about your comments and I've made a coarse plan of my solution. I'm going to:
1) In the suncertify.db package implement the DAO (thread safe Data class).
2) In the suncertify.remote package implement remote database with logical lock/unlock operations. I'm going to use the Decorator Design Pattern to decorate
the DAO object.
3) Implement Business Service over database(local or remote). Certainly I will use the Factory Design Pattern to create a unique remote database instances and
also the Facade Design Pattern to define a higher level interface which will be used by the GUI (I've decided to opt the "thin" client approach). At this layer I will
use the Value Object which will transfer itself into the array of strings which is acceptable at the lower layers. I'm going to put all of this stuff in
the suncertify.business package. Also I'm going to prohibit write access to the record number through Value Object interface.
4) Make the Business Service callable through the RMI.
5) Implement the GUI using the MVC Design Pattern and put it in suncertify.gui
package. The GUI will only use methods of the Business Service.

Is it ok?

Thanks,
Dmitry
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

What's the added value of the class(es) you'll create in 2? I would think your business service implementation invokes the methods of the Data class and you'll make sure that each client has a unique instance of the business service, so you can identify your clients uniquely. But maybe I'm missing something in your design.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Roel De Nijs wrote:What's the added value of the class(es) you'll create in 2? I would think your business service implementation invokes the methods of the Data class and you'll make sure that each client has a unique instance of the business service, so you can identify your clients uniquely. But maybe I'm missing something in your design.

Did you mean 3 instead 2? Because in 2 I create the LockManager class which is responsible for handling multiple users and for lock/unlock functionality.
So do you believe that the Factory Pattern in 3 is an excessive complication of things?
And all the rest is ok?

Thanks,
Dmitry
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Dmitry Kotlov wrote:Did you mean 3 instead 2?

No, I definitely meant 2. In 1 you create your Data class which is a thread-safe implementation of the given interface. How you implement the interface is up to you: you could have 1 class (Data) which contains all logic, you could create 2 helper classes (one for file operations, the other takes care of the locking mechanism) and Data class simply delegates the methods to these helper classes.
So I still don't see why you need a seperate LockManager for handling multiple users and lock/unlock functionality, because that's already what the lock/unlock methods from the Data class should do (and you'll use these methods too for standalone mode, because to update a record the client must have locked the record, according to the comments in the interface)
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Hi Roel!
According the instructions
You may assume that at any moment, at most one program is accessing the database file;therefore your locking system only needs to be concerned with multiple concurrent clients of your server.
I inderstand it this way: when application was started as a network server (by means of command line parameter) it must handle multiple users requests, but when it started in alone mode and there is only one client of the database it may but not must handle multiple users requests. Of course interface is the same for all modes but in local mode lock and unlock methods are empty, and in server mode they provide neccessary functionality. Actually I create two classes one is for local mode and the other one is for server mode. The second class decorates the first one by adding multiple users support.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Ok, I understand what you are trying to do in step 2. But don't you think it would simplify your design if you have 1 Data class which can be used in both modes than having seperate implementations based on the mode your application will be running in.

instructions.html wrote:// Locks a record so that it can only be updated or deleted by this client.

If you leave your lock/unlock methods empty in local mode I'm wondering how you will do this one, because that means you can't update/delete a record if it's not locked.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Roel De Nijs wrote:Ok, I understand what you are trying to do in step 2. But don't you think it would simplify your design if you have 1 Data class which can be used in both modes than having seperate implementations based on the mode your application will be running in.

instructions.html wrote:// Locks a record so that it can only be updated or deleted by this client.

If you leave your lock/unlock methods empty in local mode I'm wondering how you will do this one, because that means you can't update/delete a record if it's not locked.


If the specified record is already locked, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked

There is function that determines whether a record is locked or not. Its name is isLocked(int recNo). In delete of update functions we need first to check whether the specified record is locked or not. What if this isLocked function will always return false in the local mode? Is it a contradiction to the instructions or not?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Again my question: if you always will return false from isLocked-method, how will you be able to update/delete a record if a lock on the record (you want to update/delete) is required prior to updating/deleting the record.
Dmitry Kotlov
Greenhorn

Joined: Sep 09, 2011
Posts: 23

Roel De Nijs wrote:Again my question: if you always will return false from isLocked-method, how will you be able to update/delete a record if a lock on the record (you want to update/delete) is required prior to updating/deleting the record.

You right. My approach doesn't assume locking prior to update/delete in local mode. I missed it
Thanks, Roel!
And what about figures 3,4,5?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Dmitry Kotlov wrote:And what about figures 3,4,5?

I had my doubts about figure 2, so I commented on it. So if I had doubts/questions about 3, 4 or 5 I would have commented on them as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bodgitt and Scarper : Data class