ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a application that I'm making in C++ using wxWidgets for managing audio samples, and using SQLite for the database. When I insert a couple of files it handles them fine, but if I try to insert a lot of files at once, the database eventually throws SQLITE_BUSY error, and stops inserting anything to database. How do I handle this error and fix it, so the database can handle such load.
I add files to by either double clicking items in the left browser panel, or dragging and dropping directories/files on to it from external file browser for example.
This is the insert function that inserts those files to the SQLite database,
Those if (rc == SQLITE_BUSY) and all checks below it I added after I encountered the problem with items not getting inserted in the database, to see what is happening, that's when I discovered it throws SQLITE_BUSY.
I've used SQLite with wxWidgets and never had a problem with SQLITE_BUSY until I started multithreading. It was an easy fix, basically set up a mutex to block the next call until the previous one unlocks the mutex again.
If multithreading isn't your problem. SQLite is known for not being the fastest SQL engine. Would it be possible for you to run several statements each time you open the DB? This would eliminate the constantly opening and closing the DB which take time.
I've used SQLite with wxWidgets and never had a problem with SQLITE_BUSY until I started multithreading. It was an easy fix, basically set up a mutex to block the next call until the previous one unlocks the mutex again.
If multithreading isn't your problem. SQLite is known for not being the fastest SQL engine. Would it be possible for you to run several statements each time you open the DB? This would eliminate the constantly opening and closing the DB which take time.
No I haven't explicitly made any part of my application multi-threaded, Though for my every update, delete, or select sql query function, I am opening and closing the database. Could that be the issue, for example as you can see in the insert function I shared in my original post there is favorite column, I have a function to get the value of that column like this,
Code:
int Database::GetFavoriteColumnValueByFilename(std::string filename)
{
int value = 0;
try
{
rc = sqlite3_open("sample.hive", &m_Database);
std::string select = "SELECT FAVORITE FROM SAMPLES WHERE FILENAME = ?;";
rc = sqlite3_prepare_v2(m_Database, select.c_str(), select.size(), &m_Stmt, NULL);
rc = sqlite3_bind_text(m_Stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC);
if (sqlite3_step(m_Stmt) == SQLITE_ROW)
{
wxLogInfo("Record found, fetching..");
value = sqlite3_column_int(m_Stmt, 0);
}
rc = sqlite3_finalize(m_Stmt);
if (rc != SQLITE_OK)
{
wxMessageDialog msgDialog(NULL, "Error! Cannot get favorite column value from table.",
"Error", wxOK | wxICON_ERROR);
msgDialog.ShowModal();
sqlite3_free(m_ErrMsg);
}
else
{
wxLogInfo("Selected data from table successfully.");
}
sqlite3_close(m_Database);
}
catch (const std::exception &exception)
{
wxLogDebug(exception.what());
}
return value;
}
Every other function does open and close the database, but when inserting items to it, the application is in disabled state, so I don't think any other function can be called at that time.
Originally Posted by https://sqlite.org/rescode.html
The SQLITE_BUSY result code indicates that the database file could not be written (or in some cases read) because of concurrent activity by some other database connection, usually a database connection in a separate process.
For example, if process A is in the middle of a large write transaction and at the same time process B attempts to start a new write transaction, process B will get back an SQLITE_BUSY result because SQLite only supports one writer at a time. Process B will need to wait for process A to finish its transaction before starting a new transaction. The sqlite3_busy_timeout() and sqlite3_busy_handler() interfaces and the busy_timeout pragma are available to process B to help it deal with SQLITE_BUSY errors.
An SQLITE_BUSY error can occur at any point in a transaction: when the transaction is first started, during any write or update operations, or when the transaction commits. To avoid encountering SQLITE_BUSY errors in the middle of a transaction, the application can use BEGIN IMMEDIATE instead of just BEGIN to start a transaction. The BEGIN IMMEDIATE command might itself return SQLITE_BUSY, but if it succeeds, then SQLite guarantees that no subsequent operations on the same database through the next COMMIT will return SQLITE_BUSY.
See also: SQLITE_BUSY_RECOVERY and SQLITE_BUSY_SNAPSHOT.
The SQLITE_BUSY result code differs from SQLITE_LOCKED in that SQLITE_BUSY indicates a conflict with a separate database connection, probably in a separate process, whereas SQLITE_LOCKED indicates a conflict within the same database connection (or sometimes a database connection with a shared cache).
Maybe you could further debug where you are getting the error by adding a private temp bool variable that would be set true before opening the DB and false after closing the DB. Each function that opens the database could then check to see if the bool variable is true and display an alert. Once you find out where the concurrent writes are coming from, it should be easy to fix.
Maybe you could further debug where you are getting the error by adding a private temp bool variable that would be set true before opening the DB and false after closing the DB. Each function that opens the database could then check to see if the bool variable is true and display an alert. Once you find out where the concurrent writes are coming from, it should be easy to fix.
I changed some things, first I moved the creation of database and table to a separate method rather than being in the constructor, as the app was for no reason trying to re create the table when its really not necessary, I have IF NOT EXISTS statement but still why do something that is not needed, if I just want to access the methods from the database class, I don't need to re create stuff. Now the constructor is just this,
Not sure where you introduced the segfault, you might need to run through gdb/ddd to figure out which line crashes or revert to a prevoius working state. However this:
Needless to say the isOK needs to be defined as bool isOK = false; to avoid a forever loop. Of course a std::mutex would be ideal for production code since the while loop is not very power efficiant
Not sure where you introduced the segfault, you might need to run through gdb/ddd to figure out which line crashes or revert to a prevoius working state. However this:
Needless to say the isOK needs to be defined as bool isOK = false; to avoid a forever loop. Of course a std::mutex would be ideal for production code since the while loop is not very power efficiant
I have never used std::mutex, but looking around internet, I tried something but it is still the same, also tried adding the while loop you recommended, no luck..
You will need to declare the mutex or isOK as members in the Database class in order for it to work. Declaring them as you have above make them local to the function and each call to the function will create/delete it's own instead of using a shared one for all calls.
Like I said: I don't know where it segfaults and you will need to fix that before continuing, but somthing like this this is what I had in mind:
You will need to declare the mutex or isOK as members in the Database class in order for it to work. Declaring them as you have above make them local to the function and each call to the function will create/delete it's own instead of using a shared one for all calls.
Like I said: I don't know where it segfaults and you will need to fix that before continuing, but somthing like this this is what I had in mind:
Is this code on github, sourceforge, etc by any chance? Or rather will it be open-souce or a personal project?
I tried both while loop and mutex, made them member variables as you said, the while loop, continuously printed "Waiting on previous transaction", I waited for 10-15 seconds but it didn't stop, and the mutex also didn't help, but I got a new error
The segfault started occuring when I moved the database creation from constructor to separate method, before it just threw SQLITE_BUSY but the app didn't crash.
Yes the project is FOSS and available on Gitlab, but the changes I'm doing right now are not included in the latest commit, here is the link to the project - https://gitlab.com/apoorv569/sample-hive.
I tried both while loop and mutex, made them member variables as you said, the while loop, continuously printed "Waiting on previous transaction", I waited for 10-15 seconds but it didn't stop, and the mutex also didn't help, but I got a new error
The segfault started occuring when I moved the database creation from constructor to separate method, before it just threw SQLITE_BUSY but the app didn't crash.
Yes the project is FOSS and available on Gitlab, but the changes I'm doing right now are not included in the latest commit, here is the link to the project - https://gitlab.com/apoorv569/sample-hive.
Neat app. Got it cloned and built on Mint 19.3, Just a note: had to build wxSVG from source since it's not in the Mint repos. I can repoduce the issue you are trying to fix. Maybe you could create and push a branch with your latest code to gitlab that could be used to collaborate on this issue? I don't have a gitlab acount but I should be able to make one. I've just never had a reason to do so before.
Neat app. Got it cloned and built on Mint 19.3, Just a note: had to build wxSVG from source since it's not in the Mint repos. I can repoduce the issue you are trying to fix. Maybe you could create and push a branch with your latest code to gitlab that could be used to collaborate on this issue? I don't have a gitlab acount but I should be able to make one. I've just never had a reason to do so before.
Thanks. I see, I will add a note in README about Mint needing wxSVG to compile from source. I only have one branch ATM, which is called testing, so I don't really have any problem pushing to this branch only. I have pushed the latest commit, it does not have much just a few changes mostly what I shared here in this thread. Appreciate you helping me.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.