例外:尝试获取在收盘SQLiteClosable参考 [英] Exception: attempt to acquire a reference on a close SQLiteClosable

查看:127
本文介绍了例外:尝试获取在收盘SQLiteClosable参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布<一个href="http://groups.google.com/group/android-developers/browse%5Fthread/thread/1678a8b9bc7b6a8a">this早在五月的[Android的开发者]谷歌集团。我从来没有听说过回,却不能重现该问题,直到做了上周我的一个学生。我想我会在这里发表一下,看看它是否响钟声任何的人。

I posted this back in May on the [android-developers] Google Group. I never heard back and was not able to reproduce the problem until one of my students did last week. I figured I'd post it here and see if it rang any bells for anyone.

在我的code样品之一,我有以下方式:

In one of my code samples, I have the following method:

static Cursor getAll(SQLiteDatabase db, String orderBy) {
        return(db.rawQuery("SELECT * FROM restaurants "+orderBy, null));

}

当我运行它,偶尔,我得到这样的:

When I run it, sporadically, I get this:

05-01 14:45:05.849: ERROR/AndroidRuntime(1145):
java.lang.IllegalStateException: attempt to acquire a reference on a
close SQLiteClosable
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:31)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:56)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1118)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1092)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
apt.tutorial.Restaurant.getAll(Restaurant.java:14)

这是没有意义的我。该数据库无疑是开放的。该 SQLiteClosable SQLiteQuery SQLiteQueryDriver 创建,我 看到没有证据表明有一个对象池或某事怎么回事 这可能解释了为什么一个新 SQLiteClosable 已关闭。该 事实上,它是分散的(意思是相同的UI操作有时 触发异常,但不总是)表明某种池,赛 条件,或东西...但我不知道在哪里。

This makes no sense to me. The database is definitely open. The SQLiteClosable is the SQLiteQuery created by SQLiteQueryDriver, and I see no evidence that there is an object pool or something going on here that might explain how a "new" SQLiteClosable is already closed. The fact that it is sporadic (meaning the same UI operations sometimes trigger the exception, but not always) suggests some sort of pool, race condition, or something...but I'm not sure where.

思考?

谢谢!

更新:在code的问题是从LunchList教程我的的Andr​​oid编程教程的书。这是一个位S $ P $垫出来,不是非常适合直接在SO发布。你可以从上面的链接,如果你想看看它下载code的那本书。我不记得到底是哪的学生正在上当时的教程版,虽然它是在教程12教程16范围内。我主要是希望能发现别人谁之前就绊倒了这个问题,并有可能是罪魁祸首运行。我敢肯定我的数据库是开放的。再次感谢!

UPDATE: The code in question is from the LunchList tutorials out of my Android Programming Tutorials book. It's a bit spread out and not terribly suitable for posting directly in SO. You can download the code for that book from the above link if you want to take a look at it. I do not recall exactly which edition of the tutorial the student was working on at the time, though it was in the Tutorial 12-Tutorial 16 range. I was mostly hoping to run across somebody who had tripped over this problem before and had a likely culprit. I'm fairly certain my database is open. Thanks again!

推荐答案

这一次开车送我疯狂的时间最长。我已经找到解决的办法很简单:不保持引用 SQLiteDatabase 的对象。相反,使用 SQLiteOpenHelper 和呼叫 getWritableDatabase()每次需要一次。从文档:

This one drove me insane for the longest time. The solution I have found is fairly simple: don't keep references to SQLiteDatabase objects. Instead, use a SQLiteOpenHelper and call getWritableDatabase() every time you need one. From the docs:

公共同步SQLiteDatabase getWritableDatabase()

public synchronized SQLiteDatabase getWritableDatabase()

创建和/或打开,这将被用于读取和写入的数据库。 一旦成功打开,数据库高速缓存,这样你就可以每次你需要编写到数据库时调用此方法。

Create and/or open a database that will be used for reading and writing. Once opened successfully, the database is cached, so you can call this method every time you need to write to the database.

,答案就在那里的全部时间。

The answer was right there the whole time.

这篇关于例外:尝试获取在收盘SQLiteClosable参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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