SQLiteDatabase的close()函数导致NullPointerException异常时,多线程 [英] SQLiteDatabase close() function causing NullPointerException when multiple threads

查看:366
本文介绍了SQLiteDatabase的close()函数导致NullPointerException异常时,多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在我的项目运行打开数据库,插入数据,然后关闭数据库中的多个线程时,在Android上SQLiteDatabase实施的close()函数抛出一个NullPointerException异常。一切顺利,除非我让每个线程插入到数据库后关闭()。

I discovered in my project that the close() function in the SQLiteDatabase implementation on Android throws a NullPointerException when running multiple threads that open the database, insert data, and then close the database. Everything runs smoothly unless I allow each thread to close() after inserting into the database.

下面是写线程什么人的例子可能看起来像

Here is an example of what one of the writing threads might look like

        ContentValues values = new ContentValues();
        values.put(CallDetailsStorageConstants.COLUMN_NAME_REMOTE_NAME_IDX, (String) "");
        values.put(CallDetailsStorageConstants.COLUMN_NAME_REMOTE_MEETINGID_IDX, (String) "");

        CalculonDatabase callDetailsOpenHelper = CalculonDatabase.getInstance(mContext);
        SQLiteDatabase dbw = callDetailsOpenHelper.getWritableDatabase();

        long rowid = 0;
        try {
            rowid = dbw.insertWithOnConflict(CallDetailsTable.CALL_DETAILS_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        } catch (SQLiteConstraintException e) {
            e.printStackTrace();
        } finally {
            dbw.close();
        }

您可以看到,在最后一个调用close()制成。当运行多个线程我得到这个例外。

You can see that at the end a call to close() is made. When running multiple threads I get this exception.

11-03 03:39:26.128: E/AndroidRuntime(977): FATAL EXCEPTION: Thread-17
11-03 03:39:26.128: E/AndroidRuntime(977): java.lang.NullPointerException
11-03 03:39:26.128: E/AndroidRuntime(977):  at     android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:283)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:636)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteDatabase.beginTransactionNonExclusive(SQLiteDatabase.java:551)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:240)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
11-03 03:39:26.128: E/AndroidRuntime(977):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1737)
11-03 03:39:26.128: E/AndroidRuntime(977):  at com.smash.DBWritingThread.run(DBWritingThread.java:50)

我的问题就是为什么会发生这种错误只能使用close()函数的时候发生的呢?此外,它是确定使用close()方法在更晚的时间或可能永远?

My question then is why does this error happen only when using the close() function? Also, is it ok to use close() at a much later time or possibly never?

这个问题的任何提示都非常AP preciated。

Any tips with this issue are very much appreciated.

推荐答案

我有你的答案!

千万不要关闭数据库。就是这么简单。

Never close the db. It is that simple.

请确保您只有一个,重复,*的一个的辅助每个应用程序实例。所有线程共享。

Make sure you only have one, repeat, *one Helper instance per application. Share it with all threads.

<一个href="http://www.touchlab.co/blog/single-sqlite-connection/">http://www.touchlab.co/blog/single-sqlite-connection/

下面是对sqlite的和锁定的内部一些更多的信息:

Here's some more info on the internals of sqlite and locking:

<一个href="http://www.touchlab.co/blog/android-sqlite-locking/">http://www.touchlab.co/blog/android-sqlite-locking/

这篇关于SQLiteDatabase的close()函数导致NullPointerException异常时,多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆