2

I am trying to make multithread use of one sqlite database on Android. I have the following situation :

Activity A : it has 10 threads which use DB ,but uses only one SQLiteDatabase

Activity B : has 10 threads which use DB ,but uses only one SQLiteDatabase

(the two SQLiteDatabase objects of the Activities are different)

The logic execution is the following :

  1. A calls B
  2. B inserts into the database and returns to A
  3. A display the result
  4. A try to erase what B inserted but fails .

A can erase old things ,but the new objects that B just inserted fail,if I restart the application ,A is able to erase the objects that previously couldn't. Should I use the same SQLiteDatabase for the two Activities ,and if is this the case how to pass this object to Activity B ?

Activity A thread code :

private class Delete_Task extends AsyncTask {

    private Context mContext;

    private DatabaseManager mDB;

    public DeleteEvent_Task(Context mContext,DatabaseManager DB) {
        this.mContext = mContext;
        this.mDB = DB;
    }

    @Override
    protected Void doInBackground(Long... params) {
        long id = params[0];

        this.mDB.delete(id);

        return null;
    }
}

Activity B thread code :

private class Update_Task extends AsyncTask< Object, Void, Void > {

    private Context mContext;


    public Update_Task(Context mContext)
    {
        this.mContext = mContext;
    }

    @Override
    protected Void doInBackground(Event... params) {
        Object e = params[0];

        DatabaseManager DB = new DatabaseManager(this.mContext);

        if (e.getId() <= 0)
        {           
            DB.add(e);
        }
        else
        {

            DB.update(e.getId(), e);
        }
        DB.close();

        return null;
    }       
}
5
  • And with only 1 thread per activity it all works fine? Commented Nov 30, 2011 at 22:35
  • Actually Activity B is currently using only one thread to store the results to the database ,and Activity A is using one thread too to query them and then eliminate them . Commented Nov 30, 2011 at 22:36
  • Perhaps you fail to requery somehow? A piece of your code could help Commented Nov 30, 2011 at 22:39
  • I just posted the code that fails Commented Nov 30, 2011 at 22:49
  • I am so sorry guys ... the error was totally my folt and nothing to do with Android . I just try the solutions of Graham Borland and Herb and they work great !!! I just mark the answer of Graham Borland because at the end was the way I chose to go. I'm a little disappointed of myself not seeing this bug before posting a quiestion here. Commented Dec 1, 2011 at 12:19

4 Answers 4

3

By far the simplest thing to do is open the database connection once, on application startup, and share this connection throughout your code without ever closing it. Provided you use a single connection, all reads and writes will be correctly serialized so you don't need to worry about it.

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

2 Comments

Thanks.But what is the best way to do that,making a variable in Application class ?
Yes, or make your database helper a singleton.
1

if you use the same database and open and close the db connection before and after each executed statement, it should cause no conflict

4 Comments

should you be updating params after the insert?
no,in the Activity A ,the object is just displayed and if selected it is erased from the database ,or at least this is how it must be .
This is a bad idea: opening a db connection is expensive, and doing so for reach single operation will kill performance. Do follow @Graham Borland's advice and make your DB a singleton, opening the connection only once.
Ok you convince me .Thank you very much :-)
1

I would imagine it may be easier for you to initialize a new SQLiteDatabase for each thread that needs it than to be passing it around. Make sure you close it when you are done with it.

Have you tried running in serialized mode?

3 Comments

For thread I use AsynkTask ,and I was doing it like you say ,one connection per thread but it didn't work ,so reading in SO I found that some people prefer the less connections the better and I tried it but with the same result.For the serialized mode in the docs says "The default mode is serialized." But how can i set it implicitly ?
using sqlite3_open_v2() specifying the SQLITE_OPEN_FULLMUTEX runtime flag will it will run in serialized mode.
Thanks ,but I am running the application on Android and I can't use that function ,it is only avaible in the C interface.
0

A tip: Verify the possibility of use OO database Berkeley DB for Android is complete java implementation, secure, multithreaded, complete documented and free. Your processing is better that native because not execute native access but complete your tasks on dalvik vm. I have developing new application with that and I have no complaints.

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.