0
04-11 05:05:17.837: W/SQLiteConnectionPool(6454): A SQLiteConnection object for database Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

this is an error return by my logcat ive searched for the possible solution for this error in here andhere saying you should close database but i have this code

@Override
     protected void onPause() {
      super.onPause();

      if (db.isOpen()) {
          db.close();
      }
     }

on all my ativity so im wondering why i get this error

Community
  • 1
  • 1
  • are you sure Each Cursor is closed when you're finished with it? – Sree Apr 11 '14 at 09:27
  • http://www.devlog.en.alt-area.org/?p=1019 – Sree Apr 11 '14 at 09:29
  • is there a possibility that a cursor is open even with the code i put above?do i need to put db.close()everytime i open the db for rawquery,insert and update? –  Apr 11 '14 at 09:30
  • no need of this, you can try the same in onDestroy also – Sree Apr 11 '14 at 09:37
  • http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html – Sree Apr 11 '14 at 09:38
  • what do you mean i dont need?the code above i put is not neede?i just use your code? –  Apr 11 '14 at 09:50
  • sorry i mean no need to call db.close()everytime – Sree Apr 11 '14 at 09:51
  • im wondering why my code is not working i still get the error even if i have that close database on all my java file..am i missing something vital? –  Apr 11 '14 at 10:07
  • ho no, can you able to debug the app and find where it is going to error? – Sree Apr 11 '14 at 10:11
  • yes found it it is happening in one of my activities..i tried running all activity and i only get that error when i run that specific activity..but i dont know excatly where in the activity im still looking for it as of now –  Apr 11 '14 at 10:11
  • is there any thread inside that ? – Sree Apr 11 '14 at 10:13
  • what do you mean thread? –  Apr 11 '14 at 10:13
  • android thread or AsyncTask – Sree Apr 11 '14 at 10:14
  • no no no...i dont even have any of that anywhere in my entire project.i dont know how to make that kind of code so i didnt put it in..what i have is simple rawquery update and insert –  Apr 11 '14 at 10:15
  • 1
    and one thing - protected void onDestroy() { super.onDestroy(); if (db.isOpen()) { db.close(); } } try this too – Sree Apr 11 '14 at 10:15
  • ok will try this one and be back on you –  Apr 11 '14 at 10:16
  • the error is so persistent lol...do you think i should remove onPause and just onDestroy i used them both but nothing happened –  Apr 11 '14 at 10:19
  • hmm... nothing make change on that. you can try and post that code if you can – Sree Apr 11 '14 at 10:20
  • where you are oping your db in that i cant find – Sree Apr 11 '14 at 10:24
  • your error is on the second on you are giving try by close there and reopen here – Sree Apr 11 '14 at 10:26
  • what?i dont understand what you mean –  Apr 11 '14 at 10:27
  • close the db in the second link it self and reopen in this if you want \ – Sree Apr 11 '14 at 10:27
  • i have close db on all my activity that is why im wondering why it wont close on this one activity –  Apr 11 '14 at 10:28
  • OMG yes you want to close all your activty,please google to know more about that – Sree Apr 11 '14 at 10:29
  • no what im saying is i have that code in all of my activity that is wy im wondering why this specific activity is not closing and the rest are doing ok –  Apr 11 '14 at 10:30
  • are you sure you debug the app and find the place where the error is happen ? – Sree Apr 11 '14 at 10:31
  • im debugging now mate.. –  Apr 11 '14 at 10:34

1 Answers1

4
public class DatabaseHelper extends SQLiteOpenHelper { 

  private static DatabaseHelper sInstance;

  private static final String DATABASE_NAME = "database_name";
  private static final String DATABASE_TABLE = "table_name";
  private static final int DATABASE_VERSION = 1;

  public static DatabaseHelper getInstance(Context context) {

    // Use the application context, which will ensure that you 
    // don't accidentally leak an Activity's context.
    // See this article for more information: http://bit.ly/6LRzfx
    if (sInstance == null) {
      sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
  }

  /**
   * Constructor should be private to prevent direct instantiation.
   * make call to static factory method "getInstance()" instead.
   */
  private DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

im currently doing the same thing im doing this tutorial if you want to try it you can you can find it here

Giant
  • 1,619
  • 7
  • 33
  • 67
  • Could you please help to solve this [issue](https://stackoverflow.com/questions/62646206/unable-to-prevent-sqliteconnection-object-leakage) – MrinmoyMk Jun 29 '20 at 22:27