NPE calling method against detached POJO after DB is closed
OrientDB 2.1.4
I'm getting an NPE when calling a getter method on a detached POJO object. Previously in 2.0.8 this was working. The issue is that checkClassRegistration is returning a null because the database that was used to retrieve the POJO has been closed by the time we call the getter.
Order sort of looks like this:
open database connection
assign db to thread local
register entity class
add some objects via db.save(
then later ( different thread, maybe days later...whatever) open database assign db to thread local run java api query to get a list of objects detach all the objects in the list, and replace list items with return from db.detch() close database connection return list of detached objects loop over the list and call getters
It falls calling the getters against the detached, proxied objects
I notice there is a db.detach(obj, boolean) which will return the non-proxied instance...but that seems like quite a bit of overhead with all the reflection that is required to use this.
What would actually be best is a way to be able to run the query and get back NON proxied objects to start with, and avoid the whole overhead of the proxy. This would save the detach and all subsequent checks inside the getters to verify state. This is a scenario where the POJO is simply read only and we're not saving any data back to the database so we don't care about resaving the object.
The alternative is to leave the database connection open until after all the data has been pulled from the POJOs...but then I have to change lots of code.
The object is detached AND its proxied already, but the checks in checkClassRegistration don't short cut this. Instead checkClassRegistration chokes because getCurrentSerializedSchema() is returning null if there is no database assigned to the thread local. The thread local was cleaned out when the current connection was closed. And there is no null check in checkClassRegistration().
Maybe flip this check around to avoid the NPE:
if ((!(getCurrentSerializedSchema().classes.contains(iClass))) && (!(Proxy.class.isAssignableFrom(iClass)))) { ... }
to this:
if (!Proxy.class.isAssignableFrom(iClass && !getCurrentSerializedSchema().classes.contains(iClass) { ... }
java.lang.NullPointerException at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.checkClassRegistration(OObjectEntitySerializer.java:350) at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.isTransientField(OObjectEntitySerializer.java:291) at com.orientechnologies.orient.object.enhancement.OObjectMethodFilter.isGetterMethod(OObjectMethodFilter.java:114) at com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler.invoke(OObjectProxyMethodHandler.java:114) at com.progress.isq.ipqos.activity.system.CPUActivity_$$javassist_31.getBusyPercent(CPUActivity$$_javassist_31.java) at com.progress.isq.ipqos.resources.system.CpuData.getLastDataSample(CpuData.java:66)
I'm having the same issue, I would have thought that once detachAll() was called on a POJO you should be able to access all its fields and getters and setters after the DB connection was closed.
+1 I am having the same issue here. This seems to be the same issue as #4953
Well, same here it seems...
java.lang.NullPointerException
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.checkClassRegistration(OObjectEntitySerializer.java:350)
at com.orientechnologies.orient.object.enhancement.OObjectEntitySerializer.isTransientField(OObjectEntitySerializer.java:291)
at com.orientechnologies.orient.object.enhancement.OObjectMethodFilter.isGetterMethod(OObjectMethodFilter.java:114)
at com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler.invoke(OObjectProxyMethodHandler.java:114)