Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random SQLiteConnectionPool error on Android. How to avoid?

Tags:

android

sqlite

Recently I started to get following error in my application. This is NOT in any specific place and I can reproduce only when loop through all data read/write functionality. It comes up pretty much anywhere.

09-14 08:52:15.089: WARN/SQLiteConnectionPool(19268): The connection pool for database '/data/data/com.nnn/databases/data.db' has been unable to grant a connection to thread 1 (main) with flags 0x5 for 30.000002 seconds.
        Connections: 0 active, 1 idle, 0 available.

Is there any way to avoid this? I understand that somehow I exhause all connections to database? I'm using approach #1: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html

And my database code looks like this:

public class DatabaseHelper extends SQLiteOpenHelper
{
    private final static String LOG_TAG = "com.nnnn.data.DatabaseHelper";
    private static final String DATABASE_NAME = "data.db";
    private static final int DATABASE_VERSION = 260;
    private static SQLiteDatabase databaseInstance;

    public DatabaseHelper()
    {
        super(MyApplication.Me, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static synchronized SQLiteDatabase getDatabase()
    {
        if (databaseInstance == null) databaseInstance = (new DatabaseHelper()).getWritableDatabase();
        return databaseInstance;
    }
like image 596
katit Avatar asked Sep 14 '12 13:09

katit


1 Answers

I have never seen this error. But, I would like to point out that you are not using approach #1 from the page you linked to, and I do believe that your implementation is causing you trouble.

In the design pattern you linked, their approach is to ensure that only one DatabaseHelper instance exists throughout your applications life-cycle. Your approach is different, you are trying to ensure that only one SQLiteDatabase instance exist though-out the applications life-cycle.

So in summery, you are trying to reuse the same database connection for everything(which is not a good idea, and I would strongly suggest you change this approach), but in reality, I believe this approach is causing some issues between SQLiteOpenHelper and your static instance variable.

You are creating new instances of DatabaseHelper in your getDatabase method, sure, your databaseInstance is static, but there is a potential problem with SQLiteOpenHelper instances here. For example, lets say the databaseInstance instance is closed and set to null by a piece of code. Aka, somebody is cleaning up after them-self, then the next time getDatabase() is called, you will create a new database helper, and more importantly, a new SQLiteOpenHelper.

like image 200
JustDanyul Avatar answered Sep 20 '22 01:09

JustDanyul