【问题标题】:Android SQLite closed exceptionAndroid SQLite 关闭异常
【发布时间】:2012-12-09 17:38:40
【问题描述】:

我没有扎实的同步背景。我认为当我使用 synchronized 方法时,这个问题将得到解决。但是谁能帮我解决这个问题?代码如下:

public HashMap<String, FriendInfo> getAllRecordsInList_HashMap() {

        MySQLiteHelper dbHelper = MySQLiteHelper.getInstance(mActivity);
        HashMap<String, FriendInfo> list_map = new HashMap<String, FriendInfo>();

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        // Cursor cursor = db.rawQuery("SELECT * from " +
        // dbHelper.tbl_friendlist, new String[] {});
        String[] cols = { dbHelper.id, dbHelper.name, dbHelper.picture, dbHelper.birthday, dbHelper.livein, dbHelper.gender, dbHelper.is_online, dbHelper.is_vip };
        Cursor cursor = db.query(dbHelper.tbl_friendlist, cols, null, null, null, null, dbHelper.name);

        if (cursor != null && cursor.getCount() > 0) {

            // some code here....
        }
        cursor.close();
        return list_map;
    }

不同的线程可以调用这个方法getAllRecordsInList_HashMap()。如果我使用同步方法,请告知如何解决此问题。会修复吗?

java.lang.IllegalStateException: Cannot perform this operation 
    because the connection pool has been closed. 

at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962) 
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599) 
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348) 
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894) 
at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834) 
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) 
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143) 
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133) 
at com.platinumapps.friendlist.BLL_Friendlist.getAllRecordsInList_HashMap(BLL_Friendlist.java:290) 
at com.platinumapps.activities.Messages_Activity.addViewTOLayout(Messages_Activity.java:753) 
at com.platinumapps.activities.Messages_Activity.access$3(Messages_Activity.java:744) 
at com.platinumapps.activities.Messages_Activity$1$3.run(Messages_Activity.java:141) 
at ndroid.app.Activity.runOnUiThread(Activity.java:4644) 
at com.platinumapps.activities.Messages_Activity$1.onReceive(Messages_Activity.java:136) 
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758) 
at android.os.Handler.handleCallback(Handler.java:725) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5039) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: android android-sqlite


    【解决方案1】:

    根据Dianne Hackborn(Android 框架工程师)的说法,不需要在内容提供者中关闭数据库

    在创建托管进程时创建内容提供程序, 并且只要进程运行就会一直存在,因此无需关闭数据库,它会在进程被杀死时作为内核清理进程资源的一部分而关闭。

    所以没有必要关闭数据库。不需要时会自动关闭。

    【讨论】:

    • 如果我不是内容提供者的一部分怎么办?
    • 如果你不使用 ContentProvider,那么最好使用单例模式 SqlHandlerClass,它一次只能有一个实例。
    【解决方案2】:

    您不应关闭数据库,因为它将在下一次调用中再次使用。所以尝试删除

    db.close();
    

    来自您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2012-10-28
      相关资源
      最近更新 更多