Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CursorWindowAllocationException in standard ORMLite method

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?

like image 474
Wishmaster Avatar asked Sep 03 '14 08:09

Wishmaster


1 Answers

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;
    }
});
like image 174
Paul Burke Avatar answered Oct 18 '22 04:10

Paul Burke