Search code examples
androidormlite

Does callBatchTasks (from ORMLite) run in a separate thread?


I'm using ORMLite to manage my database on my android app and it has been working pretty well. Occasionally though, I'll get an exception containing about a constraint when I try to update the data. The exception helped me trace it to where I am calling callBatchTasks().

It's actually a series of 3 calls at that point. The first calls 2 update Table1 and Table2. The last call updates a table (Table3) that is used to specify the relationship between Table1 and Table2 (and therefore has constraints). This is the one that gets the exception.

table1Dao.callBatchTasks(table1task);
table2Dao.callBatchTasks(table2task);
table3Dao.callBatchTasks(table3task);  // Exception here

Since the tasks implement callable I'm thinking that each runs on a separate thread. So if the table3task gets ahead of either of the other ones, it runs into a constraint issue. Is this true? If so, what's the recommend workaround so that they execute in order?


Solution

  • Dao.callBatchTasks(...) has nothing to do with threads. The source for all ORMLite classes is available through the source jars or online. Looking at the source for BaseDaoImpl.callBatchTasks(...) which calls through to StatementExecutor.callBatchTasks(...), it:

    1. Turns off auto-commit (using an android transaction)
    2. Calls the Callable.call() method that is passed in as the argument to callbatchTasks(...)
    3. Restored auto-commit (closing the android transaction)

    If you need to run it in another thread then you will have to fork that thread and manage it yourself.