I have an application that uses JBoss Cache to cache some internal objects and the cache data is persisted in a MySQL database. When the application starts, data persisted in the database will be loaded into the cache. Java serialization mechanism is used to persist the data. I assigned a fixed value to serialVersionUID
field for those persistent Java classes.
The problem is that when someone introduces incompatible changes to the serialization format, then the cache data loading from database will fail due to de-serialization errors. After some researches, I got some possible solutions, but not sure which one is the best.
- Change the
serialVersionUID
to use a new version. This approach seems to be easy. But do I need to change all the classes'serialVersionUID
field value to the new version, or just the actually changed classes? Because what's been read is actually an object graph, if different serialization versions are used for different classes, will that be a problem when de-serializing the whole object graph? - Use the same
serialVersionUID
value but create my ownreadObject
and 'writeObject`. - Use
Externalizable
interface.
Which approach may be the best solution?