aspose file tools
The moose likes Distributed Java and the fly likes Getting database data using RMI Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Distributed Java
Reply Bookmark "Getting database data using RMI" Watch "Getting database data using RMI" New topic
Author

Getting database data using RMI

Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Hello, i'm writing a program for a uni project where several clients may interact with a database which resides on a server. To do this i am using RMI. I have been updating tables and submitting queries on the database ok. To do this i have simply been passing string sql statements to the server and then returing an array/vector populated with the database results etc. However is this the best way to pass database data from a server to client?

Although this has been working ok, i'm wondering ahead if there may be performance issues with putting database rows in an array. For instance, what if the database returned 1000's of rows. Would the array be albe to take this and would the performance be acceptable?! Also, where a two dimensional array is not enough, on a single screen several arrays may be needed to populate the various views and data.

Any comments would be greatly appreciated, cheers
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
It is a requirement to return a serializable object, so an array is fine in this respect. I question whether you need Vectors, but that is not the major issue.

If the object that you are returning becomes so huge that it poses a performance problem, then you need to question why it needs to be so big. For instance, why can't the client do some filtering and request only the data which is required. Or why can't the client request a limited amount of data and leave it to the user to request more information.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Thanks Roger you post some interesting points.

I don't particulary like using Vectors....but find them useful for populating a JList or JTable the client end. When i've tried doing this with arrays it goes crazy.....i imagine this has something to do with the default size of the array but thats another issue!

I take on board fully what you said about limiting the amount of data that is sent back to the client. This seems a sensible thing to do! However, i assume from your response that using arrays is a perfactly acceptable way to do what i'm doing....and there isn't perhaps some more superior way of doing it that a novice like me does not know about?!

Cheers
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Yes, I can see what you are doing, you are passing Vectors in the constructors of JList and JTable. It's been a while since I did any Swing programming, what I used to do was to pass in Object arrays as I didn't want to use those old fashioned Vectors. However, it probably isn't a good idea for you to refactor your code right now, especially if it's working.

An array is fine as every array IS-A Serializable. So long as the array contains only what is needed by the client, then it can't be made any smaller. My personal preference is to create a custom Serializable class (known as a transfer object) for shipping the data. If possible, I also make it immutable. But don't go around changing your server-side code yet, it's the client-side code that may need attention.
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Hi Roger,

Thanks for the reply. I'm particulary interested in:

My personal preference is to create a custom Serializable class (known as a transfer object) for shipping the data. If possible, I also make it immutable.


Could you expand on this slightly and explain what you mean and perhaps why this is a preference. I'm quite new to java and especially RMI. It seems i'm shipping everything to and from the server and client in some form of string, or string array and i'm guessing that although this is doing the job it may be not the most sophisticated way of operating!
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
There is a design pattern known as the Transfer Object. Here is some information on it.
http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html

As for immutable objects, there is every reason to use them if you can and no drawbacks as far as I know. Think of security (neither the class nor its subclasses can change the data), thread-safety (no need to synchronize a read-only object). As Transfer Objects are usually not changed after they have been populated, then they are good candidates for being made immutable.
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Thanks Roger you've been a great help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting database data using RMI
 
Similar Threads
passed SCJD with 360/400
Insert multiple rows in database
RMI + Thread: perform searching on different worker PCs
wierd problem while retrieving results from ResultSet
Do we over-use hibernate?