aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ConnectionFactory question 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 "ConnectionFactory question" Watch "ConnectionFactory question" New topic
Author

ConnectionFactory question

John Chien
Ranch Hand

Joined: Feb 06, 2003
Posts: 105
After reading several ConnectionFactory discussions and the Sun's article in http://java.sun.com/j2se/1.3/docs/Guide/rmi/Factory.html. I am a little confused.
According to Sun's article, the ConnectionFactory is treated as a server factory object that will provide the server object of database connection
(RemoteData from what we discussed).
Thus, there should be two ConnectionFactory objects. One for local connection factory, the other for remote connection factory.
These two ConnectionFactory objects seems have to implement a common remote interface called: ConnectionFactoryInterface.
The possible ConnectionFactory interface can be defined as below:
public interface ConnectionFactoryInterface {
DataInterface getConnection() throws RemoteException;
}
Besides that, we have to define a RemoteConnectionFactoryInterface as below:
public RemoteConnectionFactoryInterface extends ConnectionFactoryInterface, Remote {
}
In the server side, we define a RemoteConnectionFactory as :
public RemoteConnectionFacory extends UnicastRemoteObject implements RemoteConnectionFactory {
String myHost;
String myPort;
String myDbname;
public RemoteConnectionFactory(String host, String port, String dbname) {
myHost = host;
myPort = port;
myDbName = dbName;
// Register with the registry
}
public DataInterface getConnection() throws RemoteException {
return RemoteData(myHost, myPort, myDbnmae);
}
public LocalConnectionFactory implements ConnectionFactoryInterface {
String myDbname;
public LocalConnectionfactory(String dbName)
myDbname = dbName;
}
public DataInterface getConnection() {
return Data(myDbname);
}
}
Please comment on the above Connectionfactory (Sun's approach) in comparison with the previously discussed ConnectionFactory that does not implement the factory class as a remote server object.

Thanks,
John chien


SCJP, SCJD, SCWCD, SCBCD<br />IBM Certified Specialist (158)<br />Oracle9i PL/SQL Developer Certified Associate
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Well no and yes.
ConnectionFactory is on the server side and only should have one for remote mode.
Now on your client you can have another Factory, which I called DataAccessFactory, because it returns an instance of DataAccess interface. Both the DataAccessLocal and DataAccessRemote implement this interface.
The Connection that Connection Factory returns implements DataAccess interface and this object is a DataAccessRemote class.


I hope the diagram makes sense.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Perhaps a more intuitive way to understand this is the one that Sun tutorial uses. The public library is the server, the librarian is "bound" to library and serves as a point of contact, and the book that she gives you is the server object that you use. Moreover, since the relationship between you (the client) and the book (the server object) is one to one at any point in time, the book that you got uniquely identifies you. If you don't check the book in time, the library knows who you are and can implement its Unreferenced() interface (drop you from its customers).
Eugene.
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
Originally posted by Mark Spritzler:
Well no and yes.
ConnectionFactory is on the server side and only should have one for remote mode.
Now on your client you can have another Factory, which I called DataAccessFactory, because it returns an instance of DataAccess interface. Both the DataAccessLocal and DataAccessRemote implement this interface.
The Connection that Connection Factory returns implements DataAccess interface and this object is a DataAccessRemote class.


I hope the diagram makes sense.
Mark


Mark -
I would think the DataAccessRemote class needs to implement Remote, or an interface that extends Remote since it is the remote class handed out from the ConnectionFactory bound to the rmi registry which means the methods must throw RemoteException. But it also implements DataAccess which DataAccessLocal implements, which I would imagine does not throw RemoteExceptions. Can you describe this some more to make it more clear?
I did almost the same thing, except that my two classes that implement DataAccess, DataAccessLocal and DataAccessRemote, each wrap their own object. DataAccessLocal wraps a Data and DataAccessRemote wraps a Connection, but each presents the same publc interface.
So the end result in remote mode is:

and in local mode it is:
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Yes.
Actually I had my Interface throw Exception, that way my implementing class can throw whatever they want.
Mark
Brian Blignaut
Ranch Hand

Joined: Jan 08, 2003
Posts: 61
Hi,
My design is as follows :
On the client side I have the following
Connection - Interface which defines all the methods of the data class.
RemoteConnection - Implements Connection and provides remote data access
LocalConenction - Implements Connection and provides local data access.
On the server side I have the following
RemoteConnectionFactory - Interface for a factory that creates connections
RemoteConnectionFactoryImp - Implements RemoteConnectionFactory and creates connections
ClientConnection - Interface which defines all the methods of the data class.
ClientConnectionImp - Implements the ClientConnection interface. Each client that connects will receive it's own instance of this class

My question is, should the ClientConnection interface extend the Connection interface instead of defining it's own methods, this way I could get rid of the RemoteConnection class
Any suggestions would be greatly appreciated
Brian
John Chien
Ranch Hand

Joined: Feb 06, 2003
Posts: 105
Mark, BJ:
Thank you for the answer.
My notebook was broken the last several days. I have to buy a new one.
I am not sure if I really understand your answer.
I think the diagram is not class diagram or object diagram. Am I right ? It is more toward the sequence diagram (the sequence that the objects are called).
If that is the case, I think I understand your answer. Let me try to describe it to see if I really understood.
1) Client calls the client side DataAccessFactory to get a DataAccess object.
2) If it is a local call, the DataAccessFactory returns a DataAccessLocal object (or Data).
3) If the call is for remote server, the DataAccesFactory first get a Connection object.
Then it calls the ConnectionFactory method to get a DataAccessRemote object
Am I right ?
Thanks,
John Chien
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Yes John that sounds right. You call the DataAccessFactory, if local it returns a DataAccessLocal class. If it is remote it looksup the ConnectionFactory and calls getConnection method which returns a DataAccessRemote.
Now in both cases I then take the returning class from the DataAccessFactory class and wrap it into a DataAccessFacade, which hides the implementation from the client, so the client just has to call methods like bookFlight on the DataAccessFacade class.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ConnectionFactory question