Rella
Rella

Reputation: 66945

One C++ application can not open 2 SQLite databases in one thread on linux?

I actually seem to have problem with this (only on linux, described here with stacktrace like

==11682==    at 0x0: ??? // Here goes the error=(
==11682==    by 0x4D49BE: sqlite3_free (sqlite3.c:18155)
==11682==    by 0x102242D5: sqlite3OsInit (sqlite3.c:14162)
==11682==    by 0x1029EB28: sqlite3_initialize (sqlite3.c:107299)
==11682==    by 0x102A159F: openDatabase (sqlite3.c:108909)
==11682==    by 0x102A1B29: sqlite3_open (sqlite3.c:109156)
==11682==    by 0x1021CAB0: sqlite3pp::database::connect(char const*) (sqlite3pp.cpp:89)
==11682==    by 0x1021C6E3: sqlite3pp::database::database(char const*) (sqlite3pp.cpp:74)

) so I have one thread that calls 2 similar functions of 2 difrent classes using sqlitecpp

Functions look like this

void user_control::start_work_with_db( std::string db_name )
{
    if (!is_db_set) // TODO: find out how to detach from one db and connect to another.
    {
        boost::shared_ptr<sqlite3pp::database> db_( new sqlite3pp::database(db_name.c_str())); //I could not get `db = new sqlite3pp::database(DB_name.c_str());` to compile
        db = db_;
        *lu << "Connected to "<< db_name << " database and created a table with SQLite return code: " << db->execute(command_create_users_table.c_str()) << log_util::endl;
    }
}

and

void users_files_service::create_files_table( std::string db_name )
{
    if(!is_db_set) // TODO: find out how to detach from one db and connect to another.
    {
        boost::shared_ptr<sqlite3pp::database> db_( new sqlite3pp::database(db_name.c_str())); //I could not get `db = new sqlite3pp::database(DB_name.c_str());` to compile
        db = db_;
        *lu << "Connected to "<< db_name << " database and created a table with SQLite return code: " << db->execute(command_create_files_table.c_str()) << log_util::endl;
        is_db_set = true;
    }
}

and command_create_files_table looks like

command_create_files_table = "CREATE TABLE IF NOT EXISTS files (encoded_url varchar(300) UNIQUE NOT NULL primary key, file_name varchar(150) NOT NULL, user_name varchar(65) NOT NULL, is_public BOOLEAN NOT NULL, modified DATETIME NOT NULL default CURRENT_TIMESTAMP )";

and

command_create_users_table = "CREATE TABLE IF NOT EXISTS users (email varchar(100) UNIQUE NOT NULL primary key, pass varchar(100))";

and db is private member of each of classes.

I wonder - do I have an error somewhere in presented code or SQLite does not suppor operating on 2 DB files from one thread?

Upvotes: 1

Views: 449

Answers (1)

thkala
thkala

Reputation: 86333

The base SQLite library most certainly supports opening multiple DB files from the same thread - I have repeatedly done so on both Linux and Windows. It is both possible and useful, as long as you do not twist yourself up by opening the same DB file twice and trying to interleave transactions, in which case your thread may just deadlock - not crash.

This line is indicative of stack corruption:

==11682==    at 0x0: ???

You might want to use Valgrind to further track this issue down. There are a few tips for using Valgrind in this past answer of mine.

Upvotes: 5

Related Questions