Serial ID during Java serialization (in combination with actual scenarios)

Source: Internet
Author: User

To put it simply, Java's serialization mechanism verifies version consistency by judging the serialversionuid of the class at runtime. During deserialization, the JVM compares the serialversionuid In the byte stream with the serialversionuid of the corresponding local entity (class). If they are the same, they can be deserialized, otherwise, the serialization version is inconsistent.

When the entity (class) implementing the java. Io. serializable interface does not explicitly define a name named serialversionuid, the type isLongJava serialization mechanism will automatically generate a serialversionuid Based on the compiled class for comparison. In this case, only the same compiled class will generate the same serialversionuid.

If we do not want to forcibly divide the software version by compiling, that is, the entity implementing the serialization interface can be compatible with the previous version. If the class is not changed, we need to explicitly define a class named serialversionuid, type:LongSerialized entities that do not modify the value of this variable can be serialized and deserialized to each other.

The above two paragraphs are taken from NLP. I was lucky to see it yesterday. Now I recall the scenario as follows:

Environment:

1) distributed: Multiple resin servers;

2) The serialized Java class does not display the definition serialversionuid;


3) redis is used as the cache (serialized objects are stored in the redis cache );

During usage, the version is incompatible during serialization, that is, the serialversionuid is abnormal. Because serialversionuid is not displayed and defined, the serialversionuid of classes in different resin classes is different in the distributed environment, deserialization is easy to report this exception.

 
Therefore, it is best to display the serial number definition during serialization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.