Search code examples
scalaplayframeworkreactivemongoplay-reactivemongo

Why does Play with ReactiveMongo report Disk Write failures due to java.io.NotSerializableException: reactivemongo.bson.BSONObjectID


I am trying to use SecureSocial in combination with reactivemongo. However, I am having problems when it comes to caching the users. During development, the user is logged out each time Play decides to recompile files. I think this is due to the following error:

[error] n.s.e.s.d.DiskStorageFactory - Disk Write of f097d7b2db41b48eb28d73fffe4a0b7af0d5723fd5ea27c3f14c21d559c4f5b7448730955aaa95700df906ac38a4b6833de0dae859d957a368d3e5028208c5ba522ffd779ac8d9e439f2c7ec516dd4ddb0005fda0a4f5cd035c3eedec717807518b4dec5f1e6781ee55125a6ae33db50cba107bb73311078d50ee947bcd92033 failed:
java.io.NotSerializableException: reactivemongo.bson.BSONObjectID
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180) ~[na:1.7.0_21]
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528) ~[na:1.7.0_21]
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493) ~[na:1.7.0_21]
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416) ~[na:1.7.0_21]
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174) ~[na:1.7.0_21]

I don't know how to solve the problem. I was expecting BSONObjectId to be serializable by default since it is a case class. Any hints?


Solution

  • If you check the sources of BSONObjectID it turns out it actually isn't a case class, and wasn't serializable until 2 of october when sgodbillon changed to fix bug #144 (the source)

    Until that is released, I would say you have three options:

    1. run with a snapshot of reactive mongo
    2. run with a patched custom build (that you make yourself)
    3. change what you are serializing somehow (for example using a json Format or something like that) so that you are not really serializing instances of BSONObjectID