简体   繁体   中英

Does ormLite callInTransaction lock the database?

I was wondering if ormLite locked the database when it executed a transaction. I have table:

key | value
AA  | 1
BB  | 1

I have two database operations running. One modifies multiple entries:

new Callable<Void>() {
    public Void call() throws SQLException {
        pairAA = new DbEntry("AA", 2);
        pairBB = new DbEntry("BB", 2);
        dao.createOrUpdate(pairAA);
        dao.createOrUpdate(pairBB);

        return null;
    }
}

If I try to run the first on different thread, using TransactionManager.callInTransaction() is it possible to get 3 as a result of sumAllValues(dao.queryAll()) ? Running on different threads means the JVM may try to execute queryAll() between createOrUpdate(pairAA) and createOrUpdate(pairBB) . Which would mean AA becomes 2, BB is still 1 when the query reads it. Or would the transaction manager lock that table/database for the whole execution of the callable, making it impossible for the query to happen (only allowing it before or after the callable)?

TransactionManager.callInTransaction() turns off auto commit and commiting your changes after call() method executed or rollback them if call() method throws exception.

So it's not possible to get situation with queryAll() you described. Also Android SQLite allow multiple readers but only one writer at a time details: https://stackoverflow.com/a/7740305/2055854

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM