Mutex logic for shared sqlite database between Native & Android Application

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Mutex logic for shared sqlite database between Native & Android Application

Ajay Gaonkar
Dear Sqlite Team,
Thanks for looking into this mail.

I am using sqlite database in my Android application with some native code.
The database is shared between Android Application & Native code.

Can you please tell me the ideal behavior in below flow,

In my Android application below native function is called through JNI,

Native Layer: (JNI) .  //Pseudocode
jstring
Java_com_example_MainActivity_callFromJavaToJNI()
{
sqlite3_open(“/data/…/MY_DATABASE_NAME”, &db);
sqlite3_mutex* mutex;
mutex = sqlite3_db_mutex(db);

sqlite3_mutex_enter(mutex); //DATABASE LOCKED, So nobody should be able to
access this database until its closed gracefully right???

sqlite3_exec(db, ….)

// Sleep OR Wait for some time for testing the DB in Application Layer
sleep(120); // wait()
sqlite3_mutex_leave(mutex);
sqlite3_close(db);
}


When the Native code is in waiting (or in sleep(120)), if Application tries
to update the same Database. Whats should be the ideal behavior?

I found that even after mutex lock in native, Application is still able to
access the database (which is not released in native layer). Ideally when
the database is locked, it should not be accessed from anywhere right?

Is there something wrong with my native code in mutex lock?
If the above code is fine, then why does the mutex lock initiated at native
level not getting reflected at Application level?

Thanks in advance.

Warm Regards,
Ajay Gaonkar
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Mutex logic for shared sqlite database between Native & Android Application

Jens Alfke-2


> On Dec 14, 2017, at 11:17 PM, Ajay Gaonkar <[hidden email]> wrote:
>
> If the above code is fine, then why does the mutex lock initiated at native
> level not getting reflected at Application level?

Your native code opens its own connection to the database. The mutex governs access to that database _connection_, not to the database _file_, so it has no effect on the connection being used by Java.

If you want to get exclusive access to the database itself, use a transaction.

—Jens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users