问题描述
我在Android项目中将greendao用作ORM库,并且在理解事务处理行为时遇到了问题.
I am using greendao as ORM library in my Android project and have problems understanding the behaviour of transactions.
greendao提供了一个API runInTx(Runnable r)
.我的问题是,如何处理来自不同线程的两次此方法调用?
greendao offers an API runInTx(Runnable r)
.My question is, how will two calls of this method from different Threads will be handled?
线程1
public void thread1() {
DaoManager.INSTANCE.getDaoSession().runInTx(new Runnable()
{
doQueries();
doInsertsAndUpdates();
}
}
线程2
public void thread2() {
DaoManager.INSTANCE.getDaoSession().runInTx(new Runnable()
{
doQueries();
doInsertsAndUpdates();
}
}
我已经看到,runInTx
正在以独占模式调用SQLiteDatabase.beginTransaction
,因此在打开该事务时,没有其他线程可以对数据库进行读/写操作.但是我不知道的是,当线程1做他的工作(即doQueries
)时,另一个线程(即线程2)将被阻塞,因此将不会执行线程2的 doQueries
和doInsertsAndUpdates
>直到线程1完成交易为止?
I have seen, that runInTx
is calling SQLiteDatabase.beginTransaction
in exclusive mode, so no other thread can read/write to database while this transaction is open.But what i cannot figure out is, when thread1 is doing his job (i.e. doQueries
) will the other thread (i.e thread2) blocked, so doQueries
and doInsertsAndUpdates
of thread2 will not be performed until thread1 has finished the transaction?
感谢您的帮助.
推荐答案
SQLiteDatabase.beginTransaction
是阻止调用.
这篇关于使用greendao的SQLite事务行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!