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 Data Serialization 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 "Data Serialization" Watch "Data Serialization" New topic
Author

Data Serialization

Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
I included a reference to Data in my DataServer (which subclassed UnicastRemoteObject).
If I tried using the reference to Data as an instance variable, I get a Serialization error.
How do you overcome this?


Regards,
Jim
SCJP, SCJD, SCWCD, SCEA Part I
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jim,
I replied to this last night but mistakenly addressed it to Samual. I believe that you are going to have to rethink your design on serializing Data. Anyway, here's the post:
Problems with serializing the Data object
Scroll down to the bottom and read the last two posts.
Michael Morris
[ August 13, 2002: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Michael Morris:
Hi Jim,
I replied to this last night but mistakenly addressed it to Samual. I believe that you are going to have to rethink your design on serializing Data. Anyway, here's the post:

Michael,
Thanks for the response. I read your posts and I understand that Data will not serialize. I found out on my own some time ago. What I'm trying to figure out is how to make calls to Data from my DataServer. If I include a reference to Data as an instance variable, I get a serialization error.
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
I have a RemoteData object that has a private ref to Data. RemoteData implements Remote and my data interface, so it has methods to access all public methods in Data through that private reference. I am not getting any errors... do you have a similar settup?


scwcd, scjd, scjp<br /><a href="http://natejohnson.us" target="_blank" rel="nofollow">http://natejohnson.us</a><br /><a href="http://rice.kuali.org" target="_blank" rel="nofollow">http://rice.kuali.org</a>
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jim,
For my server, I just got a connection through the connection factory just like a client would and just like the client, calls were made on my DataAccess interface and not a Data object. Only objects in my db package actually had a reference to Data.
You may want to make calls on the interface through an object that implements your public Data interface for your server as well. That may solve the problem.
Hope this helps,
Michael Morris
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Nate Johnson:
I have a RemoteData object that has a private ref to Data. RemoteData implements Remote and my data interface, so it has methods to access all public methods in Data through that private reference. I am not getting any errors... do you have a similar settup?

I had the same exact setup, except I'm not sure the reference to Data was private - I might have made it public. If so, than that would have been the issue, I'm pretty sure. I'll try and see if it works.
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Michael Morris:
Hi Jim,
For my server, I just got a connection through the connection factory just like a client would and just like the client, calls were made on my DataAccess interface and not a Data object. Only objects in my db package actually had a reference to Data.

Thanks. I'll play around with this some more and see what I can do.
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
I dont know if that would have made a difference... I had the same setup as Michael mentioned (and as I talked about above).
Factory returns my DataAccess (interface to RemoteData object) and I made the calls to Data reference through that interface (called the methods in Data).
I hope that was a better explanation.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Jim,
Try removing any member variables of type Data and have a private method to find/create a Data instance for you in your DataRemote class. This private method in turn will use a factory to get the Data instance.
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Nate (Sai or Michael, feel free to jump in too!),
So would this work (see skeleton code below)?

Then anytime I wanted to get a Data reference, I call getDataReference(filename) in DataFactory.
Question:
Okay, so will RMI serialize the dataReference that has been cast as a DataAccess interface and I can now make dataReference an instance(member) variable?
Or, if RMI will not serialize dataReference, does this imply that every method I have in DataServer that wants access to a Data methods (such as lock, unlock, etc.) will first have to call the geDataReference(filename) method and then make the call to Data?
Either way, the only thing I have to insure is that DataServer knows what the filename is anytime I return a reference.
I think I understand what you, Sai and Michael have been trying patiently to explain, but I'm still not sure I have the right understanding. Sorry if I seem a little obtuse. Thanks for your help and let me know if this will work.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Jim
No need to have a member variable that holds a DataAccess reference as long as you have a method to return that reference like in your example code. Also this way, DataAccess reference will neither be Serialized nor marshalled to the client tier.
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
It is hard for me to see past my implementation without seeing the rest of your code, but I will see what I can do...
Your DataServer class... is that your server? The way I wrote my server was very different, it didnt act as a Data but as setup for my rmi factory...
main
1) create a "local" database on the server (Data)
2) pass that ref to my RemoteDataFactory (constructor) so it can keep a copy when getConn is called on it (which it turn creates a new RemoteData based on that database)
3) bind that RemoteDataFactory to the registry
end main

So, now in the client, get a ref to RemoteDataFactory (from Naming) when a remote connection is requested and call getConn. This will return a RemoteData object that has all of the methods that can access that "local" Data that was created back on the server. Each client can do this when it starts in remote mode and then be talking to the same Data, but with a unique RemoteData (which in turn can be used as the id for locking).
I am not sure if that will help with your server or not...

It looks like Sai may have answered you serialization questions... I know I didnt have to worry about serialization with the approach that I mentioned above.
[ August 14, 2002: Message edited by: Nate Johnson ]
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Nate Johnson:

main
1) create a "local" database on the server (Data)
2) pass that ref to my RemoteDataFactory (constructor) so it can keep a copy when getConn is called on it (which it turn creates a new RemoteData based on that database)
3) bind that RemoteDataFactory to the registry
end main

So, now in the client, get a ref to RemoteDataFactory (from Naming) when a remote connection is requested and call getConn. This will return a RemoteData object that has all of the methods that can access that "local" Data that was created back on the server. Each client can do this when it starts in remote mode and then be talking to the same Data, but with a unique RemoteData (which in turn can be used as the id for locking).

So you end up only having one Data for all the connections? Then synchronizing the calls to Data prevents collisions. . .Okay, I see where you're going.
Thanks. I think I've got it now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Data Serialization