• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can final and transient be used in a variable declaraion?

 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Some of the classes in my application is stopping the application to be clustered due to NotSerializationException occured due to java.io.CharArrayWriter class.

I found some of the classes in the application are using this class instance of which it was not serialized. And the object is declared as :



Can I modify it as


or




Please guide me with some good suggestions:
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of the transient keyword will ensure that the CharArrayWriter variable is not serialised, so yes, that should get you around your problem.
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, declaring it as transient does prevent the error from occuring, but don't expect the object to work correctly upon deserialization -- as it will now have an invalid char array writer.

You also need to modify the class too. You need to either (1) have the class check the instance before every usage and fix it, or (2) define a readObject method on deserialization to initialize a new char array writer. And of course, clean up any mess caused by the disappearance of the writer.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two more points...

The fact that the variable is declared as "final" definitely means that the code that uses it doesn't expect the variable to disappear.

I forgot that there may be data in the writer, which will be lost upon serialization too. Another issue to take care of.

Henry
[ September 08, 2008: Message edited by: Henry Wong ]
 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Thank you for your response. But can you explain me in detail how to
define a readObject method on deserialization to initialize a new char array writer. And of course, clean up any mess caused by the disappearance of the writer.


Currently I ve made the CharArraWriter instance availiable in some classes of application be transient. And the NotSerializationException is no more pointing to that class instance now. But it is still giving the NotSerializableException to other class instances. I am keeping on turning to serializing those instances after verifying. But let me know if a class containing LIST interface reference can be serialized??(LIST---> COLLECTION are not Serialized in the java source code!!)
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response. But can you explain me in detail how to


Unfortunately, it is *your* code. And you broke it. I can't explain in detail on how to fix it. I can't even explain in high-level how to fix it. If you read what I said again, I basically said you need to fix the "mess".

I called it a "mess" because I don't know your code, and hence, don't know what broke when you made the change.


Now... you can try to add readObject() and writeObject() methods to help save and reconstitute the char writer, but even then... you need to examine your class completely to confirm if even that will work.

Henry
[ September 08, 2008: Message edited by: Henry Wong ]
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a Sun article on serialization...

http://java.sun.com/developer/technicalArticles/Programming/serialization/

It doesn't explain the readObject() and writeObject() in too much details, but it is a good start.

Henry
 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats fine Henry!!

But, I ve a small query here:

Currently I am trying to cluster a big application in java in which many different kind of classes are involved. I am getting java.io.NotSerializableException in many situations. But making those identified classes Serialized could not solve my issue, as It contains many different classes. Please have a look about the following example which is restricting me to be Serialized, due to the presence of interfaces like LIST(COLLECTION is not a SErialized interface.)




How can I avoid these situations?? Can I make the class serialized, only after making the LIST as TRANSIENT???
 
Henry Wong
author
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look about the following example which is restricting me to be Serialized, due to the presence of interfaces like LIST(COLLECTION is not a SErialized interface.)


This statement is completely *not* true. All implementations of collections in the current runtime are serializable. What may not be serializable are the items in the collections. To get the collection serializable, you will need to get the items in the collections serializable -- items which you have not shown.

How can I avoid these situations?? Can I make the class serialized, only after making the LIST as TRANSIENT???


Unfortunately, you have completely missed the whole point of this topic. I will try to explain again... pretend you need to get a teleport CAR (yes, a real car from one planet to another using teleportation). But you can't because the engine is not serializable -- hence, the car isn't serializable.

You are arguing for ways to get the engine transient, but it is really too complex. My argument is, even if you are able to do it, all you going to get is to teleport a car without the engine on the other side!!


So, to answer your question... yes, if you can get all the items which are not serializable transient, then the whole class can be serializable. But... will the class work on the other side?

Thats fine Henry!!


Really?!? You really need to read the article that I linked to. Then you need to followup to get up to speed on serialization first. Because transient is not enough !! You can probably get that car teleported, and working, but you probably need to use readObject() and writeObject() methods, to send the engine specs over, and have mechanics on the other side rebuilt them. (hmmm, so I butchered this analogy, sorry... )

Henry
[ September 09, 2008: Message edited by: Henry Wong ]
 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have realised your outerline."The object to be persisted must implement the Serializable interface or inherit that implementation from its object hierarchy" But, in the article, coming to the point of filter stream, i.e.,
The next step is to actually persist the object. That is done with the java.io.ObjectOutputStream class.


If I am not bugging you up, I want to know a bit detail about this how the ObjectOutputStream is persisting the object. and what is the significace of
200 out.writeObject(time);
. I think I am missing some basic knowledge and could not realize the facts.


Thank you in advance.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a philosophical level, using both final and transient is most strange.
Final means it won't change.
Transient means you will not serialize it.

They are not literally inconsistant, as 'final' and 'volatile' are. But I think they represent poor design.

That it may work is an accident. You need to design and implement your class to be robust and reusable by some maintainer years from now who is not as smart as you are.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand where the serialization part fits in to your particular setup.

At what time in the life cycle of application does this problem occur?

During server startup? At deployment time? When the application is in operation?

What is the application server or container you are using and what is the clustering technology being used?
 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya,

Its the problem I ve faced during clustering. That too, particularly when the active node goes down and on continuing the application, the other node is generating the NotSerializableException.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I know something about the cluster architecture you are using?

Aside from this I guess there are some RMI calls being made when the active node fails and it could be that you need to re-engineer your application to use only serializable business objects to be used in RMI calls..

Just a shot in the dark..
 
Srikanth Adapa
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to cluster an application involved with different frameworks like echo2. It was also involved with the RMI as you have guessed. It was a huge web application to be run as a platform with different features like dynamic database maintainence, Reach engines, google maps etc with a high GUI provided using ECHO2.
Coming about my clustering architecture, I want to run multiple jboss instances load balanced by apache server. httpd 2.0.63.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic