Hi all,
How can we maintain concurrency in SQlite database(Version-3) which is accessing by multiple threads /Apps.
and if we cannot maintain concurrency in SQlite then which database is useful for my app?
Please Help !!!
Thanks.
Hi all,
How can we maintain concurrency in SQlite database(Version-3) which is accessing by multiple threads /Apps.
and if we cannot maintain concurrency in SQlite then which database is useful for my app?
Please Help !!!
Thanks.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 19, 2013, 4:17 AM EDT
modify your code like this:
--------------------------------------------------
public bool ExecuteANonQuery(string sql, int i = 0)
{
try
{
Open();//for connection open
using (IDbCommand command = _dbCon.CreateCommand())
{
command.CommandText="PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE;"+sql+" COMMIT;";
int ww = command.ExecuteNonQuery();
}
return true;
}
catch (SQLiteException ex)
{
if (ex.Message.Contains("readonly database"))
{
i++;
if (i < 50)
{
Thread.Sleep(100);
return ExecuteANonQuery(sql, i);
}
}
throw;
}
catch { throw; }
finally { Close(); }
---------------------------------------------------
I'm glad I could help.
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 18, 2013, 10:17 PM EDT
you CAN maintain the concurrency in SQLite (Version 3) like you do in other DBMS.
SQLite FAQs page says-"... no other embedded SQL database engine that supports as much concurrency as SQLite. SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business. Other embedded SQL database engines typically only allow a single process to connect to the database at once." (refer to: http://www.sqlite.org/faq.html)
So if you're planning to use embedded database, just go ahead with SQlite, it's quite useful and worthy.
Mark this as answer if it helps.
Cheers!
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 19, 2013, 2:10 AM EDT
Thanks for your reply.
Can u please tell me how can i lock the database file ? i tried to lock database command which is executing while inserting/updating my Query.but i did not get any success.
Please tell me how i will do this process,thus i will sort out concurrency issue in my database.
Thanks
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 19, 2013, 2:15 AM EDT
Execute these commands
PRAGMA locking_mode = EXCLUSIVE;
BEGIN EXCLUSIVE;
This will lock whole database until you execute a:
COMMIT;
Implement above commands in your query and I think the problem will get sorted out.
Looking forward for your response in that.
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 19, 2013, 3:16 AM EDT
Thanks a lott for your reply.Can u give me little more help.
how to use
PRAGMA locking_mode = EXCLUSIVE;
BEGIN EXCLUSIVE;
COMMIT;
using this method i am executing my queries can u please tell me where i put your code inside this.or this is wrong way then can u plz share some demo code for this.
Thanks a lot for your help
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 19, 2013, 4:24 AM EDT
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 19, 2013, 10:02 PM EDT
I used code which u provided in my app but i am getting below error
"SQL logic error or missing database near "COMMIT": syntax error"
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 19, 2013, 10:26 PM EDT
The code will definitely work, just make sure that each of your statement terminates with a semi-colon (the error is most probably because of this only).
------------------------------------------
command.CommandText="PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE;"+sql+" COMMIT;";
-----------------------------------------------------------
Make sure that the "sql" (that denotes your query statement) in above syntax is terminated properly with a semi-colon so that it could generate a single query like:
------------------------------------------
command.CommandText="PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE;"+"insert into myTable values(
------------------------------------------
See, in my above statement, it has four statements combined in one and each one is terminated with a semi-colon.
Hope you got the point now.
Cheers!
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 20, 2013, 8:19 PM EDT
yeah u were right i was missing semicolon in my query. now i added it :). but now again i am facing another issue "SQL logic error or missing database cannot start a transaction within a transaction"
i searched on google but not able to find the solution. :(
Please help me.
Thanks
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 20, 2013, 8:21 PM EDT
Sumit KumawatRank #4,008 · 372 reputationPosted Jun 30, 2013, 7:23 PM EDT
Sorry for delay in reply. actually i found solution myself.
below is solution of my problem.
"After setting locking_mode = EXCLUSIVE; we have to reset locking_mode = NORMAL; "
But now i have another query if i want to access my same SQlite db from two different applications(multi-threaded), Then how i can maintain concurrency at this time.
i tried several times to access database but i got this error "Database lock".
Thanks.