45

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.

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)

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.

Thoughts?

Thanks!

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!

4
  • Did you make any progress on this? Oct 10, 2009 at 9:34
  • 1
    See the update I just added to the original query. It's such a rare occurrence that I'm not terribly concerned; I was just hoping somebody who had the problem before would see the question. Thanks! Oct 10, 2009 at 12:00
  • Yes, it happens very rarely for me too. Still I would've expected to at least find a bug report for it, but I didn't. Oct 11, 2009 at 6:36
  • thanks for the posting! It really helped my issus too~!
    – sammiwei
    Feb 28, 2012 at 22:44

4 Answers 4

54

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:

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.

3
  • 3
    Hmmmm...that's very interesting. It also looks like you can call close() on the SQLiteOpenHelper to close the database. I'll have to experiment with this. Many thanks! Mar 20, 2010 at 23:16
  • You're very welcome! Thank you for asking the question; it helped me to figure out the solution. Mar 21, 2010 at 2:02
  • Couldn't quite get it. Can you post a more elaborate example?
    – Codevalley
    Jul 26, 2010 at 18:44
9

SQLiteDatabase is closed automatically under some conditions.

http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html

getCount() and onMove() methods of Cursor trigger an actual query using SQLiteQuery. After all required data are obtained, SQLiteQuery decrements the reference count of the SQLiteDatabase instance. When the reference count reaches 0, the database is closed.

Note that the query may be executed asynchronously and in such a case, getCount() may return -1 if it is called before SQLiteQuery finishes preparing data.

1
  • Thanks so much for this info! I had no idea that the db could close by calling getCount().
    – danh32
    May 3, 2013 at 19:26
1

I had same problem for a few days, and my solution was to put open() method just before my query and close() method after database operation. It looks something like this.

open();
Cursor cur=db.query(DATABASE_TABLE_CON, null, null, null, null, null, " name ASC");
close();
return cur;

It works just fine, but I'm concerned for resources cost. I'm not sure am I spending more resources with all this opening and closing database before any action.

1

I've been experiencing a similar problem, despite already following Jarett's advice. In my case the problem is happening fairly regularly on orientation changes. I've discovered that, for some reason I haven't yet got to the bottom of, my code generates two identical, pretty much simultaneous AsyncTasks on an orientation change (as opposed to just one when the activity starts normally). These tasks perform the same database query at the same time from different threads.

This exception (or occasionally some other SQLiteException) is the result. So it seems that this message can be a symptom of concurrency problems, even if it is not necessarily the root of the original problem posted here.

2
  • When the screen orientation changes an entire new Activity is created. If you're starting your AsyncTasks in the Activity's onCreate() method, two will be created. Dec 1, 2010 at 17:03
  • 1
    If you are properly extending SQLiteOpenHelper and closing your database in onDestroy(), then you shouldn't have any problems with concurrent calls to modify the database. Feb 2, 2011 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.