Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:06:41+00:00 2026-06-11T01:06:41+00:00

I am looking for best approach to handling database connection. I have designed my

  • 0

I am looking for best approach to handling database connection. I have designed my application base on following method and works fine. However, sometimes application crashes especially on old model devices. Please look at my following method.

I have a DatabaseHelper class which all of tables will be created here. This class is like this:

public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        Log.i(TAG, "Object created.");
    }

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CRT_TBL_1);
        db.execSQL(CRT_TBL_2);
        db.execSQL(CRT_TBL_3);
        db.execSQL(CRT_TBL_4);
        db.execSQL(CRT_TBL_5);
        db.execSQL(CRT_TBL_6);
        db.execSQL(CRT_TBL_7);
        db.execSQL(CRT_TBL_8);
    }

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DatabaseHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data.");

        onCreate(db);
    }
}

As you have seen, I have 8 tables which provides data for 8 different activities. Implementation of handler class for one activity is like following code.

public class DatabaseHandler_1 {

    private final String TAG = "DatabaseHandler_1 ";

    private DatabaseHelper dbHelper;
    private SQLiteDatabase database;

    public DatabaseHandler_1 (Context context) {
        dbHelper = new DatabaseHelper(context);

        Log.i(TAG, "Object created.");
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

// ... Other methods insert, update, query, ...
}

Finally, based on required information one or some of them will be used in each activity.

public class Activity_1 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Try to create activity...");

        // Creating Database/Table
        dbHandler_1 = new DatabaseHandler_1 (this);
        dbHandler_1 .open();
        dbHandler_2 = new DatabaseHandler_2 (this);
        dbHandler_2 .open();
        dbHandler_3 = new DatabaseHandler_3 (this);
        dbHandler_3 .open();
    }

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

        // Closing database
        dbRecipesHandler.close();
        dbFavoritsHandler.close();
        dbShoppingHandler.close();
     }

        // Other methods for displaying data...
}

When crashes happens logcat shows the connection is close while my application tries to query in database. I think scenario is like this:
User opens 1’st activity then he goes to second activity and then he goes to third activity. Now, if user clicks on back button third activity will be closed and onDestroy() method of this activity closes database connection. Because, second activity is still in memory, activity assumes that the connection is still open and when it wants to query into database, crash happens.

Logcat shows a message like this: Database connection is close. You wanted to query…

One solution is instead of letting Android OS handles database connection, we manually open connection and use it the manually close it.
Second solution is in Handler class manually open and close connection for each method.

What is you suggestion? did you have any experience about it?
Any suggestion would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:06:42+00:00Added an answer on June 11, 2026 at 1:06 am

    Finally I solved the problem. I thought that I should open database one time in activity. I tried to open database again in onResume() method of each activity and closing it in onPause() of each activity instead of closing in onDestroy() method.

    So far problem solved. Hope to don’t see it again. If i face it again then will update/remove answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im looking for the best approach to takle the following, I have sorted a
We have a legacy web application (not Spring based) and are looking for best
What's the best approach to provide URL handling in an ASP.NET application to produce
I’m looking for best practices or a general approach towards making a server-side application
I'm looking at the best practice approach here. I have a web page that
Looking for the best approach for a non-image CSS Drop Shadow technique. I have
I'm looking for a best practice approach to the following problem. I'd like peoples
I am looking for the best approach to mimic the iPod application on the
Looking for the best approach: I have a tabbar and a navigationbar. under the
I'm looking for the best approach for generating data transfer objects from business objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.