Search code examples
scalatomcat7squeryl

Implementing Externalizable in Scala with SerialVersionUID


I have an app that is deployed to Tomcat 7 and is currently clustered among two nodes. I am running into a small issue that occurs when the container tries to deserialize a class which has changed. The read and write methods should handle this gracefully, so I don't think that is the issue. As best I can tell setting serialVersionUID should solve the issue, but in my code I have it specified as -1 and in the error below that value seems to be ignored.

Exception:

java.io.InvalidClassException: common.user.User; local class incompatible: st ream classdesc serialVersionUID = 1828770465826288626, local class serialVersion UID = 6192552274218063887

Relevant part of Class definition:

case class User(
    var id: Long = 0l) extends SerialVersionUID(-1l) with KeyedEntity[Long] with Externalizable {

  def readExternal(in: ObjectInput) {
    id = in.readLong()
  }

  def writeExternal(out: ObjectOutput) {
    out.writeLong(id)
  }
}

I have also tried using the following instead of extending the abstract class (as per: http://www.scala-lang.org/node/259)

private val serialVersionUID = -1l

I get the same result. Is there something I am missing in how to properly Externalize a class with Scala?


Solution

  • Check the docs for SerialVersionUID, it's an annotation, so you should use @SerialVersionUID(123) right before the class declaration.

    Making it:

    @SerialVersionUID(123)
    case class User(
        var id: Long = 0l) extends KeyedEntity[Long] with Externalizable {
         ...
    }