Why does the connection pool close before displaying Sqlite data?

Asked

Viewed 44 times

1

I am trying to display BD data in a Recycleview but get the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.emerson.drawer, PID: 8316
              java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closFed.

I did some tests and found that this occurs when there are more than 50 results. Does anyone know anything about this error?

My consultation:

from2 = new String[]{
                DataBaseHandler.TABLE_OS + "." + DataBaseHandler.KEY_OS_ID + " AS _id",
                DataBaseHandler.KEY_OS_EQUIPAMENTO,
                DataBaseHandler.KEY_CLIENTES_NAME,
                DataBaseHandler.KEY_OS_ACESSORIO,
                DataBaseHandler.KEY_OS_ABERTURA,
                DataBaseHandler.KEY_SITUACAO_NAME,
                DataBaseHandler.KEY_SITUACAO_COR_R,
                DataBaseHandler.KEY_SITUACAO_COR_G,
                DataBaseHandler.KEY_SITUACAO_COR_B,
                DataBaseHandler.KEY_OS_ID_AMIGAVEL,
                DataBaseHandler.KEY_OS_MARCA,
                DataBaseHandler.KEY_OS_FOTO

        };       
 Cursor cursor = db.Select(DataBaseHandler.TABLE_OS + " LEFT OUTER JOIN " + DataBaseHandler.TABLE_SITUACAO + " ON " + DataBaseHandler.TABLE_SITUACAO + "." + DataBaseHandler.KEY_SITUACAO_ID + "=" + DataBaseHandler.TABLE_OS + "." + DataBaseHandler.KEY_OS_SITUACAO +
                                        " LEFT OUTER JOIN " + DataBaseHandler.TABLE_CLIENTES + " ON " + DataBaseHandler.TABLE_CLIENTES + "." + DataBaseHandler.KEY_CLIENTES_ID + "=" + DataBaseHandler.TABLE_OS + "." + DataBaseHandler.KEY_OS_CLIENTE, from2, null, null, null, null, null, null);

     int index0 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_ID);
            int index = cursor.getColumnIndex(DataBaseHandler.KEY_OS_ID_AMIGAVEL);
            int index2 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_EQUIPAMENTO);
            int index3 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_MARCA);
            int index4 = cursor.getColumnIndex(DataBaseHandler.KEY_SITUACAO_NAME);
            int index5 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_ACESSORIO);
            int index6 = cursor.getColumnIndex(DataBaseHandler.KEY_CLIENTES_NAME);
            int index7 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_ABERTURA);
            int index8 = cursor.getColumnIndex(DataBaseHandler.KEY_SITUACAO_COR_R);
            int index9 = cursor.getColumnIndex(DataBaseHandler.KEY_SITUACAO_COR_G);
            int index10 = cursor.getColumnIndex(DataBaseHandler.KEY_SITUACAO_COR_B);
            int index11 = cursor.getColumnIndex(DataBaseHandler.KEY_OS_FOTO);

            if (cursor.moveToFirst()) {
                do {
                    int id = cursor.getInt(index0);
                    String numero = cursor.getString(index);
                    String equipamento = cursor.getString(index2);
                    String marca = cursor.getString(index3);
                    String situacao = cursor.getString(index4);
                    String acessorio = cursor.getString(index5);
                    String cliente = cursor.getString(index6);
                    String data = cursor.getString(index7);
                    int corR = cursor.getInt(index8);
                    int corG = cursor.getInt(index9);
                    int corB = cursor.getInt(index10);
                    byte[] ft = cursor.getBlob(index11);

                    Bitmap foto = null;
                    if (ft != null) {
                        foto = BitmapFactory.decodeByteArray(ft, 0, ft.length);
                    }

                    OsList lista = new OsList(id, numero, equipamento, marca, situacao, acessorio, cliente, data, corR, corG, corB, foto);
                    osList.add(lista);

                }
                while (cursor.moveToNext());
            }

Databasehandler.class

...
public Cursor Select(String tabela, String campos[], String where,
                         String[] whereArgs, String groupBy, String having, String orderBy, String limit) {
        Cursor c = null;
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            Log.i("response", "Iniciando consulta");
            c = db.query(tabela, campos, where, whereArgs, groupBy, having, orderBy, limit);


        } finally {
            if (c != null)
                Log.i("teste", String.valueOf(c.getCount()));
            db.close();
        }
        return c;
    }
...
  • Make sure you are using the db.close() or even cursor.close()... it is interesting to close the bank every time it is used.

  • @Acklay The query method contains 
 } finally {
 if (c != null)
 Log.i("teste", String.valueOf(c.getCount()));
 db.close();
 }
 return c;
 }

  • You can debug and see where you’re stopping?

  • db is a Sqliteopenhelper class? If so, is it Singleton? Enter the full code of this query.

  • @ramaral yes is type Sqliteopenhelper, I edited the question, thank you.

1 answer

1


The problem is in the method Select(). In it is done the db close and consequently the cursor.

Change the method like this:

...
public Cursor Select(String tabela, String campos[], String where,
                     String[] whereArgs, String groupBy, String having, String orderBy, String limit) {
    Cursor c = null;
    SQLiteDatabase db = this.getWritableDatabase();
    Log.i("response", "Iniciando consulta");
    c = db.query(tabela, campos, where, whereArgs, groupBy, having, orderBy, limit);
    return c;
}
...

In the code that calls the method Select() I close the cursor after using it.
Doing the db close is not required and may not even be desired. If you want, when you no longer need db, call the method close() of the Sqliteopenhelper class, usually this is done in the onDestroy().

  • Amigo @ramaral worked perfectly. Thank you very much. o close() should be done in the query or in the main Activity?

  • Whether you mean the close of db depends on whether Sqliteopenhelper is Singleton or not. If you are, you must do so when you are sure that you will no longer use it, if you are not, do so at the place where you requested it. However, as I said in the reply, it is not mandatory to close the db

Browser other questions tagged

You are not signed in. Login or sign up in order to post.