• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how do I return a DBAccess interface back to client?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a factory that returns a DBAccess interface back to the client. I want the client to use this interface to run the methods for either a local or remote database connection. It works fine for a local connection. Now I'm trying to implement the remote connection using RMI.

How do I return a DBAccess interface when I want to establish a remote connection in my fat client?

I have done the following: I have a class called Data.java that implements the DBAccess interface. I have another interface called RemoteDBAccess which lists the same declarations as DBAccess and adds "throws RemoteException" to each of them. This interface also extends Remote.
I have an adapter called RemoteDBAdapter which extends UnicastRemoteObject and implements RemoteDBAccess.

How can transform this RemoteDBAccess interface type into a DBAccess interface type?
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by berry westover:
I have a factory that returns a DBAccess interface back to the client. I want the client to use this interface to run the methods for either a local or remote database connection. It works fine for a local connection. Now I'm trying to implement the remote connection using RMI.

How do I return a DBAccess interface when I want to establish a remote connection in my fat client?

I have done the following: I have a class called Data.java that implements the DBAccess interface. I have another interface called RemoteDBAccess which lists the same declarations as DBAccess and adds "throws RemoteException" to each of them. This interface also extends Remote.
I have an adapter called RemoteDBAdapter which extends UnicastRemoteObject and implements RemoteDBAccess.

How can transform this RemoteDBAccess interface type into a DBAccess interface type?




Please don't keep starting new topics to continue the discussion on a previous topic. I will continue to help, but be warned, if you do this on the Sun Java Forums you will get seriously flamed. This forum is more friendly so that is unlikely to happen here. From now on lets use this topic to discuss your questions about the adapters.

The Data class implements the DBAccess interface. It is capable of providing data base access to a local client on a server.

I define an interface called DataAdapter that implements Remote and adds RemoteException to all the methods in DBAccess.

I define a class called DataAdapterImpl that implements DataAdapter and extends UnicastRemoteObject. The DataAdapterImpl has a Data instance as a member variable. This lets it delegate all its actions to the Data object. This is a case of the Adapter pattern as desribed in GoF.

On the client side I have a class called DataProxy that removes the throws RemoteException from all the methods. DataProxy implements the DBAccess interface exactly. To do this it has a DataAdapter instance that is actually a remote copy of the DataAdapterImpl. Check out the Proxy pattern in GoF, a Proxy is an Adapter that has the same interface as the adapted object, and as such can substitute for it (if you own stocks you may have used a real world proxy to vote at a meeting without actually attending).

Your RemoteDBAccess interface is the same as my DataAdapter interface, it's what you use on the client side of RMI. Your RemoteDataAdapter is the same as my DataAdapterImpl, it's what runs on the server side and has a Data object to delegate to.

You will get an instance of RemoteDBAccess from RMI, probably indirectly from a factory class that is registered in the RMI registry.

Once you have that instance you can build a true Proxy by implementing the DBAccess interface in a class that has a RemoteDBAccess instance as a member variable which it will delegate to. The calls that delegate to this will look like:

In this call all you do is delegate the readRecord to the RemoteDBAccess and catch the RemoteException. You then wrap that exception in a RuntimeException and throw it. I actually use a subclass of RuntimeException to make it easier to catch in the business logic.

As you get further into the database side of this you will need to come to terms with IOException. Whatever you do with IOException is exactly what you will need to do with RemoteException. The choices are to either use an unchecked RuntimeException or use the exceptions defined in the method, such as RecordNotFoundException. I chose to use a subclass of RuntimeException since the findByCriteria method doesn't throw any exceptions at all in the UyB specification.

I hope this helps, I'm off for a couple of days, Merry Christmas and best of luck on your project.

/Peter
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic