连接池已关闭,数据库未打开

时间:2018-03-26 20:13:24

标签: android dagger-2 android-room

我知道这通常是由多线程问题引起的,或者是在数据库关闭时尝试与数据库进行交互但是在尝试对这两个问题进行数千次查询IllegalStateException时在某些设备上被抛出:

The database '/data/user/0/.../databases/database.sqlite' is not open.

Cannot perform this operation because the connection pool has been closed.

我遇到了一个问题,当Entity被添加到数据库中的主键与Entity已经存在的主数据库相同时,它就不会替换原来的Entity OnConflictStrategy 1}}即使有' sa替换myRoomDatabase.dbDao().insertAllRecords(*it.records)

没有抛出这些错误的原始代码:

for (record in it.records)
{
    try
    {
        myRoomDatabase.dbDao().deleteRecordsById(record.id)
    } catch (e: Exception)
    {
        e.printStackTrace()
    }
    myRoomDatabase.dbDao().insertSingleRecord(record)
}

新代码,因为替换策略不起作用:

.close()

我不会在应用中的任何地方拨打@Module open class RoomModule(val application: Application) { @Volatile private var INSTANCE: MyRoomDatabase? = null fun getInstance(context: Context): MyRoomDatabase = INSTANCE ?: synchronized(this) { INSTANCE ?: buildDatabase(context).also { INSTANCE = it } } private fun buildDatabase(context: Context): MyRoomDatabase { return Room.databaseBuilder(context, MyRoomDatabase::class.java, "db.sqlite") .openHelperFactory(AssetSQLiteOpenHelperFactory()) // Prepopulated SQLite file .fallbackToDestructiveMigration() // User never modifies database .build() } @Provides @Singleton open fun provideMyRoomDatabase(context: Context): MyRoomDatabase = getInstance(context) }

我的数据库是通过Dagger提供的:

Context

我的AppModule是使用旧版Dagger通过@Module public class AppModule { private Context context; public AppModule(Context context) { this.context = context; } @Provides Context provideContext() { return context; } etc. } 提供的:

var root = this;
var a = [];

page.open('https://www.thegioididong.com/dtdd/iphone-x-256gb', function (status) {
page.evaluateAsync(function () {
    console.log("click now")
    document.getElementsByClassName("viewparameterfull")[0].click()
}, 3000)

setTimeout(function (a, b) {
    root.a = page.evaluate(function (el, i) {
        console.log("get data now")
        var temp = document.getElementsByClassName('parameterfull')[0].childNodes
        for (i = 0; i < temp.length; i++) {
             console.log(temp[i].innerHTML)
        }
       //Output html of all childNodes as intended

        return temp
    })}
    , 10000)
})

setTimeout(function () {
    console.log("log now");

    for (i = 0; i < a.length; i++) {
        console.log(a[i].innerHTML)
    } 

       //Output only the first child of a, but a.length still shows 51 (enough number of child nodes)
    console.log(a)
}, 20000)

知道如何阻止这种情况发生吗?我不正确地使用Dagger吗?

0 个答案:

没有答案