CursorWindowAllocationException in standard ORMLite method
Asked Answered
L

2

7

I need save some objects in DB. I'm using this code in my Dao class.

  public void saveActions(List<Action> actionList) throws SQLException {
    for (Action action : actionList) {
        createOrUpdate(action);
      }
  }

And sometimes I have CursorWindowAllocationException in createOrUpdate() function.

Does anyone have solution of this problem?

Liriodendron answered 3/9, 2014 at 8:2 Comment(0)
S
2

If you look up the source of CursorWindowAllocationException it reads:

This exception is thrown when a CursorWindow couldn't be allocated, most probably due to memory not being available.

If you follow the stack, you'll see that the call com.j256.ormlite.android.AndroidDatabaseConnection.queryForLong is creating a cursor for every createOrUpdate call.

So what's likely happening here is that there are too many Cursors being created before the memory is freed.

You should execute these calls in a transaction, or better yet, use batch tasks. E.g.

actionDao.callBatchTasks(new Callable<Void>() {
        public Void call() throws SQLException {
            for (Action action : actionList) {
                actionDao.createOrUpdate(action);
            }
        return null;
    }
});
Sorrento answered 16/9, 2014 at 5:33 Comment(0)
G
-1

You must call cursor.close(); in finally { } block when you do not need cursor anymore. Cursor allocation will fail without calling close() on cursor after some time when system resources for cursor allocation will be not available.

Godbeare answered 14/9, 2014 at 11:12 Comment(2)
But I don't use cursor manually. I just use createOrUpdate(); and all other logic runs inside orm library. Well how I can see no one knows why ormlite fails.Liriodendron
Are not you loading some big data e.g. images from DB? Could you provide more code? What do you mean by "sometimes" you get CursorWindowAllocationException please?Godbeare

© 2022 - 2024 — McMap. All rights reserved.