2

Here is my problem. My app starts several threads, each for a particular object to be updated. The update of the object happens with a query to a single database. There is a single database and a single OpenHelper. The behavior of my app suggests me that the calls to the database are non simultaneous as well as I would like. How can I access the same database from different threads simultaneously? If the data for each object are in different tables is more efficient to split the database in several databases, one for each object?

    public class SomethingToBeUpdated implements Runnable {

       private SQLiteDatabase db;

       @Override 
       public void run() {
         db.rawQuery( ... bla bla
       }

    }

    public class MainActivity extends Activity {

       private SomethingToBeUpdated[] list = bla bla...

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          for( SomethingToBeUpdated x : list ) {
             new Thread(x).start();
          }
       }

    }
1
  • Be careful with this multi threading on Sqlite. In your case its better to use JOINS with rawQuery Commented Feb 12, 2015 at 22:52

1 Answer 1

5

For the sake of accessing the database in various threads you need to have a Database manager which keeps an object of your database class and pass it to any thread that needs it. In android you cannot access database simultaneously in several threads with different objects. It may just block your UI (the problem i was facing a few days ago).

So to overcome this problem you can use the the database manager i used which is defined as follows:

public class DatabaseManager {
    private AtomicInteger mOpenCounter = new AtomicInteger();

    private static DatabaseManager instance;
    private static SQLiteOpenHelper mDatabaseHelper;
    private SQLiteDatabase mDatabase;

    public static synchronized void initializeInstance(SQLiteOpenHelper helper) {
        if (instance == null) {
            instance = new DatabaseManager();
            mDatabaseHelper = helper;
        }
    }

    public static synchronized DatabaseManager getInstance() {
        if (instance == null) {
            throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }

        return instance;
    }

    public synchronized SQLiteDatabase openDatabase() {
        if(mOpenCounter.incrementAndGet() == 1) {
            mDatabase = mDatabaseHelper.getWritableDatabase();
        }
        return mDatabase;
    }

    public synchronized void closeDatabase() {
        if(mOpenCounter.decrementAndGet() == 0) {
            mDatabase.close();
        }
    }
}

Then you initialize it once like this:

DatabaseManager.initializeInstance(new ChatSQL(c));

And then you can get the database object wherever you want with this syntax:

SQLiteDatabase db = DatabaseManager.getInstance().openDatabase(); //in your methods which are querying the database

With this method your database is now thread safe. Hope this helps.

If the data for each object are in different tables is more efficient to split the database in several databases, one for each object?

No it is not efficient. It has a lot of overheads to define, access, make object and query different databases. And what if you want to join tables? you just cannot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.