Reputation: 5867
Recently i saw and heard a new concept (atleast to me) with Serialization. What i saw is we have a DAO classes for Hibernate to talk to DB, that class and all implements Serialization. I have asked the developer saying that we need to do serialization when we transfer object. He told me in a multithreaded environment, either we need to implement interface or serialization. But i am not convinced with his statment.
We are backend application and we are providing webservices to the frontend. So we are having frontend faced classes, business classes and DAO classses. In what area this Serialization to be used.
Does the developer is correct on multihreaded and Serialization?
Upvotes: 3
Views: 2024
Reputation: 3364
You have to Implement Serializable
in order to convert your Object to bitStream no matter what you doing with that.
Only thing , you have to consider in multithread environment, ObjectStream
is not locking while writing the object. So if your Object state going be modify by multiple threads at the same time. it is application responsibility to lock and write in safe.
This SO provides more info regarding this.
"He told me in a multithreaded environment, either we need to implement interface or serialization"
What you mean by implement interface
? any interface ? or proxies
?
For DAO layer. As per JPA spec
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the
entity class must implement the Serializable interface
So JPA does not force you to do Serializable
unless you want to serialize external ( like send object in remote / writing into file and so).But it is nothing harm to implement Serializable
, since it is just marker Interface. IMHO It is always good practice to implement in entity class
Upvotes: 1
Reputation: 4129
Serialization is used any time you need to take an object, and convert it to a serialized form, so it can be stored for later use (like saving a document), or transmitted and used on a different JVM.
Serialization has implications for concurrency typically only when used in a distributed computing environment, where different JVM instances may be working on the same problem and need to exchange information.
None of this is anything you will need to worry about for a long, long time.
Upvotes: 0
Reputation: 6051
I have no clue what he meant by telling something about multithreading and serialization. It's 2 different concepts. What is your question ?
http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
Serialization is simply used for saving/restoring state of the object to transfer it over the wire or save/restore from the disc, etc. It is used in RMI (remote procedure call) for example.
Upvotes: 0