Why does the connection pool close before displaying SQLite data?

1

I'm trying to display DB data in a Recycleview but I 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 testing and found that this happens when there are more than 50 results. Does anyone know anything about this error?

My query:

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;
    }
...
    
asked by anonymous 23.03.2017 / 01:39

1 answer

1

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

Change 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 Select() method, I close the cursor after using it. Close the db is not mandatory and may not be desired. If you want, when you do not need db anymore, call the close() method of the SQLiteOpenHelper class, this is usually done in onDestroy() .

    
23.03.2017 / 17:54