Spurious NullPointerException: Cannot invoke "com.objectdb.spi.Tracker.beforeAccess(int)" because "this.__odbTracker" is null

#1

When we use Java serialization for embeddable objects, we spuriously get the following exception:

java.lang.NullPointerException: Cannot invoke "com.objectdb.spi.Tracker.beforeAccess(int)" because "this.__odbTracker" is null
    at quant.phdsc_new.smart_component.label.data.LabelPositionTopRight.writeObject(LabelPositionTopRight.java:1)

We retrieve an object from the database, do a deep copy to ensure any objects with data are fully loaded, and then we enqueue the object to be serialized on another thread. There are some objects which have no data and behave essentially as singletons or custom enums, so we do not copy them, such as LabelPositionTopRight.

ObjectDB Enhancer creates a writeObject method that looks like this:

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        if (this.__odbTracker != null) {
            this.__odbTracker.beforeAccess(-1);
        }
        objectOutputStream.defaultWriteObject();
    }

At some point, maybe when the original thread closes the connection, the __odbTracker field is set to null.

When Java serialization calls writeObject from another thread at the same time this happens, there is a race condition where __odbTracker is set to null between the "if (this.__odbTracker != null)" condition and the "this.__odbTracker.beforeAccess(-1);" call, causing a NullPointerException.

I have investigated this issue on an old version of ObjectDB, but I didn't see mentions of changes in this area in the release notes, so I don't think the enhanced writeObject method was made thread-safe in a newer version, is that correct?

What would you recommend as a solution? Is there any support for ObjectDB to understand these objects as singletons with no data, such that retrieving them from the database would simply look up the singleton instance, and there would be no need for the Enhancer to make changes since there are no fields? Would it be reasonable to ask for the writeObject enhancement to be made thread-safe, by loading __odbTracker into a variable first, or is the scenario of continuing to use an object after closing the database connection completely unsupported and we would need to change our code to deep clone the singleton while the connection is still open?

Thank you.

#2

ObjectDB (following JPA) requires using a separate EntityManager per thread. An EntityManager and all entities in its persistence context are not thread-safe and must not be shared across threads. What you are observing might not even be a race condition, but a classic thread-visibility issue where one thread fails to see recent changes made by another due to CPU/caching behavior.

While you might temporarily work around this by synchronizing access on both ends (wrapping both the EntityManager.close() call and the serialization logic in synchronized blocks to force a memory barrier and prevent race conditions), the only supported solution is to avoid sharing EntityManager instances and entity instances between threads altogether. Enforcing internal thread-safety within ObjectDB would introduce performance overhead without resolving the underlying architectural constraints, so the current design will not be changed.

ObjectDB Support
#3

Well what we're doing is retrieving an object from the database, and passing it off to another thread where it gets serialized. This other thread has no idea where this object came from, the fact it's a database object is irrelevant to it. This object has no data, it's effectively supposed to be a singleton, so it wasn't obvious that simply serializing an enhanced object with no data could cause a data race with ObjectDB's internals, but I guess that *any* object that is retrieved from the database has to be either cloned or the connection has to be closed before the object can be safely used on any other thread?

I believe I worked around this by implementing a writeReplace method, which should completely bypass writeObject.

#4

> so it wasn't obvious that simply serializing an enhanced object with no data could cause a data race with ObjectDB's internals,

As soon as it was enhanced, it got data (the __odbTracker field). There is no special optimized path for dataless entities, as they are not common.

ObjectDB Support
#5

I understand that enhancing gives it the __odbTracker field, and I understand that one of its uses is to lazily load in fields before access, it just wasn't obvious that it would also affect Java serialization like this until I decompiled the enhanced classes to see what was happening.

When thinking about cloning objects after retrieving them from database, I inferred that if __odbTracker is used before field access, and the class has no fields, there would be no reason to clone such objects or replace them with the canonical singleton instance. That inference was wrong, and it makes sense why ObjectDB creates a writeObject method like this, but it was a little surprising.

I think it's all clear now, thank you.

Reply