realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.

Open dotgc opened this issue 11 years ago • 36 comments

java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.
            at io.realm.internal.ImplicitTransaction.assertNotClosed(ImplicitTransaction.java:66)
            at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:29)
            at io.realm.Realm$RealmCallback.handleMessage(Realm.java:194)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:176)

I'm getting the above error while adding/updating realm objects in a background handler thread. What is the reason behind this?

dotgc avatar Jan 08 '15 10:01 dotgc

You have called close() on Realm and are probably trying to access a RealmResult or RealmObject afterwards.

cmelchior avatar Jan 08 '15 10:01 cmelchior

Is this the only possibility? Because I don't see this happening anywhere in my code.

dotgc avatar Jan 09 '15 00:01 dotgc

It is by far the most likely. We will need more information to be more specific, can you share your code or try to reduce it to a testcase?

cmelchior avatar Jan 09 '15 06:01 cmelchior

Wait, encountering it again. Below is an explanation of what I'm trying to do.

I need to constantly update the app with data from the server. So for polling data, I created a handler thread with a Looper. On app start, in the run() method of the Runnable, I get a Realm instance, perform a transaction, close the Realm instance, and then post the Runnable again to the same thread with a delay of say, 60 seconds.

Below is some pseudo code:

Runnable r = new Runnable() {
     @Override
     public void run() {
         httpRequest.get(uri, new DataCallback() {
             public void onSuccess(JSONObject result) throws JSONException {
                 Realm realm = Realm.getInstance(context);
                 realm.setAutoRefresh(true);
                 try {
                     realm.beginTransaction();
                     <Some write with data from the server>
                     realm.commitTransation();
                 } catch (Exception e) {
                     realm.cancelTransaction();
                 } 
                 realm.close();
             }
        }
        handlerThread.postDelayed(r, 60 * 1000);
    } 
}  

handlerThread.post(r)  // executed at app start.

I post some other Runnables too on the same handler thread. Some of them involve a network call, some of them just work with local data.

dotgc avatar Jan 10 '15 04:01 dotgc

That pattern looks pretty solid. You just have to be careful about catching Exception, as you might catch OutOfMemory which could potentially cause corruption on your database. Catching RuntimeException is much safer.

Is it inside that exact runnable you get the ImplicitTransaction exception?

cmelchior avatar Jan 10 '15 10:01 cmelchior

@ImGauravC What kind of writes are doing. Only writes on new objects or overriding objects that came from somewhere else?

cmelchior avatar Jan 10 '15 10:01 cmelchior

@cmelchior : I sync current client state with the server response. So it involves either: a) using setters to change values of RealmObjects. b) creating new RealmObjects c) Since some data might be deleted on the server, so in order to keep the data in sync on the client, I delete all realm objects using realm.clear() and then recreate them with the server response.

dotgc avatar Jan 10 '15 16:01 dotgc

Oh, and the objects might be in use in activity/adapter while this is being done.

dotgc avatar Jan 10 '15 16:01 dotgc

You a) might be causing the problems. Realm instances are cached on the same thread and reference counted, but if you reference count reaches 0, it will close the Realm and all objects are no longer valid.

So if you access RealmObjects you have not queried for in the success handler you might risk they have been closed.

cmelchior avatar Jan 10 '15 16:01 cmelchior

@cmelchior : I do query for the objects in the success handler. Still facing the issue.

dotgc avatar Jan 13 '15 11:01 dotgc

In that case I would be very interested to know exactly what your code does as you might have stumbled upon a bug somewhere. Are you in a position to share it or can you reduce it to a minimal case?

Just a note. I just found a bug in our our Realm.close() code that could cause it to behave wrong if you call close to many times, so I would check for that.

cmelchior avatar Jan 13 '15 12:01 cmelchior

@ImGauravC I found another bug in how our Realm.close() method works on background threads which could explain the behaviour you are seeing. See the above PR for a fix.

cmelchior avatar Jan 13 '15 20:01 cmelchior

Great! Can we have the 0.77 soon?

dotgc avatar Jan 14 '15 19:01 dotgc

Sure - we hope to cut it tomorrow all going well :-)

bmunkholm avatar Jan 14 '15 19:01 bmunkholm

Fix has been merged into master and will be part of 0.77.0

cmelchior avatar Jan 15 '15 15:01 cmelchior

Hi @cmelchior I'm still seeing this.

java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.
       at io.realm.internal.ImplicitTransaction.assertNotClosed(ImplicitTransaction.java:66)
       at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:29)
       at io.realm.Realm$RealmCallback.handleMessage(Realm.java:212)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:137)

I've multiple background threads fetching data from the server for different parts of the application (Just in case this might help.)

dotgc avatar Jan 27 '15 16:01 dotgc

Also with the newest 0.78 release?

cmelchior avatar Jan 27 '15 18:01 cmelchior

Didn't check this one

dotgc avatar Jan 27 '15 20:01 dotgc

Yes, even with the new version.

dotgc avatar Feb 02 '15 19:02 dotgc

Hi @ImGauravC Are you still seeing this issue with the latest version of Realm?

cmelchior avatar Apr 16 '15 08:04 cmelchior