2011-11-05 24 views
1

其實我似乎有這個問題(僅在Linux上,描述here與堆棧跟蹤像一個C++應用程序無法在Linux上的一個線程中打開2個SQLite數據庫?

==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) 

),所以我有一個線程調用2 difrent類2個類似的功能使用sqlitecpp

功能看這樣

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; 
    } 
} 

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; 
    } 
} 

和command_create_files_table模樣

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)"; 

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

db是每個類的私有成員。

我想知道 - 我在提供的代碼中有某處出現錯誤,或者SQLite不支持從一個線程操作2個DB文件?

回答

5

基地SQLite庫肯定支持從同一個線程打開多個DB文件 - 我在Linux和Windows上都反覆做過。這是可能的和有用的,只要你不打開相同的數據庫文件兩次,並試圖交錯事務,在這種情況下,你的線程可能只是死鎖 - 不會崩潰。

該行表示堆棧損壞:

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

您可能需要使用Valgrind進一步追蹤這個問題了。在this past answer of mine中有幾個使用Valgrind的提示。

相關問題