Android SQLite 关闭异常 [英] Android SQLite closed exception

查看:31
本文介绍了Android SQLite 关闭异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有扎实的同步背景.我认为当我使用 synchronized 方法时,这个问题会得到解决.但是有人可以帮我解决这个问题吗?代码如下:

I have no solid background of synchronization. I think this issue will be fixed when I use synchronized method. But can anyone help me to fix this issue? Code is below:

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().如果我使用同步方法,请告知如何解决此问题.会修复吗?

Different threads can call this method getAllRecordsInList_HashMap(). Please advise how to fix this issue if I make synchronized method. Will it be fixed?

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)

推荐答案

根据 Dianne Hackborn(Android 框架工程师)在内容提供者中无需关闭数据库

According to Dianne Hackborn (Android framework engineer) there is no need to close the database in a content provider

内容提供者在其托管进程创建时创建,并在进程执行时一直存在,因此无需关闭数据库,它会在进程被终止时作为内核清理进程资源的一部分而关闭.

A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database it will get closed as part of the kernel cleaning up the process's resources when the process is killed.

因此无需关闭数据库.不需要的时候会自动关闭.

So there is no need to close the database . it will automatically close when it is not needed.

这篇关于Android SQLite 关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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