0

I'm using Realm, a framework used for creating persistent objects on mobile devices. Realm does not support nested transactions. So the problem is: I have a class with a static block that executes a Realm transaction, however, the first time the class is used in my code is by one of its static methods from within another Realm transaction. The error I'm getting is a java.lang.ExceptionInInitializerError, and it is caused by this:

Caused by: java.lang.IllegalStateException: Nested transactions are not allowed. Use commitTransaction() after each beginTransaction().

And the line of code that it brings me to that's causing the error is the line in the static block that is executing the transaction.

Because the first time the class is used is in a Realm transaction, it seems like the static block is not executed until that point, which is why I get the error caused by the nested transactions.

So just to clarify, it doesn't matter what type of variables a class has: static, static final, or whether they're initialized as instance variables or in a static block. Those variables will not be initialized until the class is interacted with for the first time. Correct?

Update:

Here is the code that occurred in the static block:

RealmSingleton.getUserInstance().executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    users.deleteAllFromRealm();
                }
            });
shoe
  • 952
  • 1
  • 20
  • 44
  • Your last statement is correct. – 4castle Sep 16 '16 at 22:23
  • static initialization of a class happens when a class is loaded, and that happens when it's first used. See also http://stackoverflow.com/questions/2007666/in-what-order-do-static-initializer-blocks-in-java-run – zapl Sep 16 '16 at 22:33
  • The last statement is partially correct, they will not be initialized until the class is loaded to be used. – acornagl Sep 16 '16 at 22:34
  • @acornagl and is a class loaded to be used the first time it is interacted with? – shoe Sep 16 '16 at 22:37
  • @shoe In the JVM (Java Virtual Machine) language the *interacted* term has no sense, instead the *loaded* term means that JVM loader has correctly loaded the class. – acornagl Sep 16 '16 at 22:40
  • What does your transaction in the `static` block do? – EpicPandaForce Sep 17 '16 at 10:42
  • @EpicPandaForce it deletes everything from the realm. i posted the code. – shoe Sep 18 '16 at 01:23
  • So based on the error in your question, your classloader loads the class with the first transaction, so possibly in a static block or method, beginTransaction() is called. But in that same block do you have a commitTransaction()? From your error message it looks like that is not the case. Instead, another class is loaded, in which another beginTransaction() is executed without committing the previous one... that's essentially a nested transaction which apparently is not supported by Realm. – code4kix Sep 18 '16 at 01:34

0 Answers0