1

I need an advice about compatible changes and serialVersionUID (http://docs.oracle.com/javase/6/docs/platform/serialization/spec/version.html#6678).

I work on a system with a 'client' project which communicates with 'server' project using RPC ('server' exports data from DB using DTO classes and 'client' presents that data using the same DTO classes).

When a field is added in a DTO class, the team decided not to change serialversionUID, so when a new version of 'server' is deployed, 'client' doesn't need to be deployed immediately, because older version of that DTO class is compatible with a new one.

But (as we have a few dozen of system instances with both projects), if a new version of 'client' is deployed with new version of DTO class, and 'server' for some reason remains at the old version, then the new field of DTO class will resolve to null, and will be presented as null to customer, which may be incorrect.

Is there any kind of best practice to address this issue? We can change the UID on any kind of changes in DTO classes, but then we'll always have to deploy both projects at the same time, even when changes are not used, and we are trying to avoid that...

Uros
  • 38
  • 4

1 Answers1

0

Changing the UID is not the solution, it is part of the problem. Don't do that.

When you add the new field, you can also add logic into the readObject() method of the same class to give it a sensible default value if it's null.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks, will try that, but I don't know if a default value will help. It would be ideal to throw an exception, but only if serialized class is deserialized into newer version (ie. when there are new fields that don't exist in serialized one). Is there an easy way to detect if a field exists in stream during deserialization, or will it always resolve to null? – Uros Jul 12 '15 at 20:01
  • Why would you want to throw an exception? It's not a dynamic condition. It is conpletely predictable from the state of the deployment whether the new field will be present or not. – user207421 Jul 12 '15 at 23:44
  • there are more than 30 instances with both applications, and sometimes the client application must be deployed before the server application. I know that's a different problem, but it's the way it is for now. I thought it would be better to give some "under maintenance" message, than to have wrong values presented to user (for example, a boolean field will be set to false if the field doesn't exist). – Uros Jul 13 '15 at 00:25