Reputation: 32321
I have read about Serialization theory part , where it says it is required when an object state need to be persisted . I have written a Web Service Application , Where it will run on different JVMs
I am in the process of improving the performance of my Web Service so I have decided to use transient
keyword for some of my Variables inside my Webservice
class
I have some questions related to it as what object need to be serialized and what should not be
1.First to start with for my Logger , I will use the keyword transient
For example :
private transient static final Logger logger = Logger.getLogger(Tata.class);
2.But what about the instance variables inside the class ?? do we need to use transient for them or not ?? For example :
String strategyData = null;
String errorText = null;
Properties prop = null;
Please share your inputs .
Upvotes: 2
Views: 1133
Reputation: 4093
Serialization is used incases where you need to persist state of your object. Or need to trasfer the object state between different machine (or virtual machine, if you are using more than one vm on a single machine) etc.
For ex. If you what the state of the object even after restarting the system, You can use serialization.
Also, You may not required to save all the state in the object. For that you can declare that variable as transient.
If you need only strategyData
to be persisted, you can declare the other two variables as transient.
Note. If you need to serialization an object. All the object referred inside must also be Serializable, or you need to declare them as transient.
Upvotes: 0
Reputation: 21
visit the link which might be useful to you..
http://javarevisited.blogspot.com/2011/09/transient-keyword-variable-in-java.html
Upvotes: 0
Reputation: 7792
What do you mean by 'run on different JVMs' ?
If it would run simultaneously on several JVMs (a distributed server for instance) than no the variables that represent the state of your object must not be transient.
Otherwise the other servers instances would get your service in an inconsistent state.
Upvotes: 0
Reputation: 691635
The web service class itself is never serialized. The data objects that are returned by the web service methods, as well as their arguments, are serialized. They should contain information needed by the recipient of the object.
If some information is not needed by the recipient, and if the object will not cause exceptions with this information set to null, then you can mark it as transient. But if it's not needed by the recipient, it should probably not be part of the object in the first place.
If you're using a logger inside the DTO, then this logger should definitely not be serialized. But make sure to check that the logger is not null each time you're using it, then.
Upvotes: 0
Reputation: 30638
if the variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.
so all those variables which you do not need to store in the persisted state of the object can be declared as transient.
refer http://www.javabeat.net/tips/168-what-is-transient-keyword-in-java.html for more details
Upvotes: 1
Reputation: 115328
You should mark as transient all fields that cannot or should not be serialized together with other object fields.
Field that is not serializable itself cannot be serialized and therefore must be marked as transient if it belongs to serializable class. The example is class that contains several fields and one of them is Thread. Obviously thread cannot be serialized. Therefore mark it as transient and implement mechanism that creates new thread when object is restored after serialization.
Other example is when field can be but should not be serialized. For example process ID. Assume that your program holds process ID of other process. The process ID itself is int
, so there is not problem to serialze it. But it does not have any sense in other environment or in the same environment in several minutes (because that process probably already does not exist).
Upvotes: 0