Reputation: 2034
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)?
Upvotes: 1
Views: 1404
Reputation: 6748
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
Upvotes: 1