When I open the database in each thread with SQLite Shared-Cache Mode, An accessing error always appears, that shows each member of the codec_ctx object ara all 0xdddddddd.
This error can be reproducted easily. Creating one thread to operate database all the time, in the meanwhile create another thread to open database and then close it.
I have writen a simple project to reproduct the error. The code is:
#define SQLITE_HAS_CODEC
#include "../../bld/sqlite3.h"
const char *g_db_name = "test.db";
const char *g_password = "abc123";
int main()
{
std::cout << "Hello World!\n";
std::thread t1([]() {
sqlite3 *db{};
int ret{};
const char *sql{};
char *errmsg{};
ret = sqlite3_open_v2(g_db_name, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE, nullptr);
sqlite3_key(db, g_password, strlen(g_password));
sql = "drop table if exists tb001; create table tb001(a TEXT, b TEXT, c TEXT);";
ret = sqlite3_exec(db, sql, nullptr, nullptr, nullptr);
for (int i = 0; i < 10000; i++)
{
sql = "insert into tb001 VALUES('a1', 'bb2', 'ccc3')";
ret = sqlite3_exec(db, sql, nullptr, nullptr, &errmsg);
if (ret != SQLITE_OK)
{
std::cout << "err msg:" << errmsg << std::endl;
sqlite3_free(errmsg);
}
}
sqlite3_close_v2(db);
});
std::thread t2([]() {
for (int i = 0; i < 10000; i++)
{
sqlite3 *db{};
int ret{};
ret = sqlite3_open_v2(g_db_name, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE, nullptr);
sqlite3_key(db, g_password, strlen(g_password));
ret = sqlite3_close_v2(db);
}
});
t1.join();
t2.join();
}
If I comment out all ‘sqlite3_key’, the code runs well. Or if I delete all ‘SQLITE_OPEN_SHAREDCACHE’ in sqlite3_open_v2 , the code runs well too.
My computer OS is Windows10, I use Visual Studio 2017, and the sqlcipher version is 4.4.0.
Does sqlcipher not support SQLite Shared-Cache Mode?
If yes, how to use in SQLite Shared-Cache Mode?
Thanks very much.