Android- java.lang.IllegalStateException:

时间:2014-10-24 05:04:43

标签: java android sqlite synchronization

我有一个在后台运行的服务,可以访问(读取和写入)sqlite数据库。这将执行应用程序的同步过程。相应的前台进程(主应用程序)也可以在我的android应用程序中访问(读取和写入)sqlite数据库。

当正在进行同步过程并且用户尝试使用前台进程访问数据库时,会出现此错误。我使用synchronized来锁定sqlite db,但仍然会出现此错误。

synchronized (obj) {
    blogDbAdapter.open();
    todayBlogs = blogDbAdapter.getBlogsByDate(today, today, me, activity);
    blogDbAdapter.close();
}

这是错误:

    10-24 00:41:11.134: E/AndroidRuntime(1627): FATAL EXCEPTION: main
 Process: com.eyepax.horseapp, PID: 1627
 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:144)
    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
    at com.eyx.hor.data.dbadapters.BlogDbAdapter.getBlogsByDate(BlogDbAdapter.java:172)
    at com.eyx.hor.data.DataManager.getTodayBlogs(DataManager.java:2693)
    at com.eyx.hor.ui.activities.BlogListView.setDataAdapter(BlogListView.java:239)
    at com.eyx.hor.ui.activities.BlogListView.onCreateView(BlogListView.java:92)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

这是getBlogsByDate(),

public ArrayList<Blog> getBlogsByDate(java.util.Date fromDateU, java.util.Date toDateU, boolean me, Activity activity) {
    Date fromDate = new java.sql.Date(fromDateU.getTime());
    Date toDate = new java.sql.Date(toDateU.getTime());

    this.open();
    Cursor cursor = null;
    if (me) {
        String user = CommonUtils.getFromSharedPreference("USER_ID", "0", activity);
        cursor = database.rawQuery("select * from " + TABLE_NAME + " where date(" + DATE + ") BETWEEN '" + fromDate + "' AND '" + toDate + "' AND " + USER
                + " = " + user, null);
    } else {
        cursor = database.rawQuery("select * from " + TABLE_NAME + " where date(" + DATE + ") BETWEEN '" + fromDate + "' AND '" + toDate + "'", null);
    }

    ArrayList<Blog> list = null;
    if (cursor != null && cursor.moveToFirst()) {
        list = new ArrayList<Blog>();
        do {
            Blog blog = new Blog();
            blog.setBlogId(cursor.getString(cursor.getColumnIndex(BLOG_ID)));
            blog.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
            blog.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
            blog.setImageUrl(cursor.getString(cursor.getColumnIndex(IMAGE_URL)));
            blog.setDate(cursor.getString(cursor.getColumnIndex(DATE)));
            blog.setUser(cursor.getString(cursor.getColumnIndex(USER)));
            blog.setUserImageUrl(cursor.getString(cursor.getColumnIndex(USER_IMAGE_URL)));
            blog.setGroupId(cursor.getString(cursor.getColumnIndex(GROUP_ID)));

            list.add(blog);
        } while (cursor.moveToNext());
    }

    if (cursor != null)
        cursor.close();
    return list;
}

1 个答案:

答案 0 :(得分:0)

错误说:

  

java.lang.IllegalStateException:无法执行此操作,因为   连接池已关闭。

然后,您需要打开连接。

尝试使用:

synchronized (obj) {
  blogDbAdapter.open();
  todayBlogs = blogDbAdapter.getBlogsByDate(today, today, me, activity);
  blogDbAdapter.close();
}