问题
In my android app I have some Database transaction using SQLite but I am getting SQLiteConnection object leakage despite trying many ways to prevent the leak. I have almost tried each and every thing in the internet like closing the db, closing the cursor, or ending the transaction. Below is the warning in android studio.
A SQLiteConnection object for database '/data/user/0/com.example.myapp/databases/myapp.dbnotes.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
Sometimes by closing the db or closing the cursor I used to get error - 'Attempt to reopen an already-closed object ....'. I am not pro in Android, I am trying to learn by my own self , so could you help me. I have posted the codes below-
Inside the DBHelper class
public class DBHelper extends SQLiteOpenHelper { ... public static DBHelper getInstance(Context ctx) { if (mInstance == null) { mInstance = new DBHelper(ctx.getApplicationContext()); } return mInstance; } private DBHelper(Context context) { super(context, DB_NAME, null, 1); this.context = context; DB_PATH = context.getDatabasePath(DB_NAME).getPath(); } ... private Cursor getData(String Query) { String myPath = DB_PATH + DB_NAME; Cursor c = null; try { db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); c = db.rawQuery(Query, null); } catch (Exception e) { e.printStackTrace(); } return c; } private void dml(String Query) { String myPath = DB_PATH + DB_NAME; if (db == null) db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); try { db.execSQL(Query); } catch (Exception e) { e.printStackTrace(); } } //Methods to perform different db transaction public void addToSubject(ItemSubject itemSubject) { if (checkSuggested(itemSubject.getId())) { dml("delete from " + TABLE_SUBJECT + " where id = '" + itemSubject.getId() + "'"); } String insert = "insert into TABLE_SUBJECT ....."; dml(insert); } public void cleartable_subject() { String delete = "delete from " + TABLE_SUBJECT; dml(delete); } public long subject_size() { if (db == null) { db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE); } try { long count = DatabaseUtils.queryNumEntries(db, TABLE_SUBJECT); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } private Boolean checkSubject(String id) { String select = "select * from " + TABLE_SUBJECT + " where id = '" + id + "'"; Cursor cursor = getData(select); return cursor != null && cursor.getCount() > 0; } public ArrayList<ItemSubject> loadDataSubject() { ArrayList<ItemSubject> arrayList = new ArrayList<>(); String select = "select * from " + TABLE_SUBJECT; Cursor cursor = getData(select); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { String id = cursor.getString(cursor.getColumnIndex(TAG_ID)); String course = cursor.getString(cursor.getColumnIndex(TAG_COURSE_NAME)); .... ItemSubject objItem = new ItemSubject(id, courseId,...); arrayList.add(objItem); cursor.moveToNext(); } cursor.close(); } return arrayList; } //There are more similar methods for other tables
I am accessing these methods inside fragments and activities like
dbHelper = DBHelper.getInstance(getActivity());
if ((dbHelper.subject_size() >= 1){
dbHelper.cleartable_subject();
for (int i = 0; i < arrayListSubject.size(); i++) {
dbHelper.addToSubject(arrayListSubject.get(i));
}
arrayListSubject = dbHelper.loadDataSubject();
}
Sorry for the long section of codes, but I thought everything will be necessary. Anyways could you please help me
来源:https://stackoverflow.com/questions/62646206/unable-to-prevent-sqliteconnection-object-leakage