大致意图:启动两个线程并发执行insert 一张表, 看看是否存在插入失败的情况,
最终测试结果sqlLite 自己内部有保护, 应用程序可以不要再加锁保护,但是应用层需要保护自己的临界资源,
比如键值信息, 可以根据下面的程序演变其他的测试项, 包括对sqlLite 操性能的考察。
#include<...>
class Task_test : public ACE_Task <ACE_MT_SYNCH>
{
public:
Task_test(): m_pdb(NULL),m_nUnique(0)
{
}
~Task_test()
{
sqlite3_close(m_pdb);//关闭sqllite 对象
}
virtual int svc()
{
int nThreadID = ACE_OS::thr_self();//获取当前线程ID
char *perrmsg = NULL;
for (int i=0; i<10; i++)
{
char szch[50]={0};
ACE_OS::snprintf(szch,99,"insert into tbl_test values (%d,%d,'xcp')",getunique(),nThreadID);
//执行插入操作
int nResult = sqlite3_exec(m_pdb,szch,&perrmsg);
if(nResult != sqlITE_OK )
{
ACE_DEBUG((LM_INFO,"%T <%t> insert Failed. /n"));
}
else
{
ACE_DEBUG((LM_INFO,"%T <%t> insert success. /n"));
}
}
return 0;
}
void init()
{
int nResult = 0;
char *perrmsg = NULL;
//打开db,创建sqllite 对象
nResult = sqlite3_open("./db/test.db",&m_pdb);
if( nResult != sqlITE_OK )
{
ACE_DEBUG((LM_INFO,"%T <%t> open db Failed. /n"));
return;
}
else
{
ACE_DEBUG((LM_INFO,"%T <%t> open db success. /n"));
}
//创建数据表
nResult = sqlite3_exec(m_pdb,"create table tbl_test (id integer primary key,threadID integer,name varchar(32))",&perrmsg);
if(nResult != sqlITE_OK )
{
ACE_DEBUG((LM_INFO,"%T <%t> create table Failed. /n"));
return;
}
else
{
ACE_DEBUG((LM_INFO,"%T <%t> create db success. /n"));
}
}
//显示数据表信息
void display()
{
int nResult = 0;
char *perrmsg = NULL;
int nRow = 0;
int nCol = 0;
char **ppResult = NULL;
nResult = sqlite3_get_table(m_pdb,"select * from tbl_test",&ppResult,&nRow,&nCol,&perrmsg);
if(nResult != sqlITE_OK )
{
std::cout<<"exec sql Failed."<<std::endl;
}
for(int k=0; k<=nRow; k++)
{
std::cout<<k<<": ";
int t=0;
while (t < nCol)
{
std::cout<<ppResult[k*nCol+t]<<"/t";
t++;
}
std::cout<<std::endl<<"------------------------------------"<<std::endl;
}
sqlite3_free_table(ppResult); //释放结果信息
}
protected:
private:
sqlite3 *m_pdb;
ACE_Recursive_Thread_Mutex m_lock;
int m_nUnique;
int getunique()
{
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(m_lock);
return ++m_nUnique;
}
};
//主程序
int main(int argc,char* argv[])
{
Task_test oTask_test;
oTask_test.init();
oTask_test.activate((THR_NEW_LWP | THR_JOINABLE),2);
oTask_test.wait();
oTask_test.display();
return 0;
}
------------------------------------------
输出如下:
17:03:56.839000 <2836> open db success.
17:03:56.999000 <2836> create db success.
17:03:59.112000 <2828> insert success.
17:03:59.212000 <4020> insert success.
17:03:59.333000 <2828> insert success.
17:03:59.433000 <4020> insert success.
17:03:59.533000 <2828> insert success.
17:03:59.643000 <4020> insert success.
17:03:59.743000 <2828> insert success.
17:03:59.843000 <4020> insert success.
17:03:59.943000 <2828> insert success.
17:04:00.044000 <4020> insert success.
17:04:00.144000 <2828> insert success.
17:04:00.254000 <4020> insert success.
17:04:00.374000 <2828> insert success.
17:04:00.474000 <4020> insert success.
17:04:00.584000 <2828> insert success.
17:04:00.695000 <4020> insert success.
17:04:00.795000 <2828> insert success.
17:04:00.895000 <4020> insert success.
17:04:01.015000 <2828> insert success.
17:04:01.125000 <4020> insert success.
0: id threadID name------------------------------------1: 1 2828 xcp------------------------------------2: 2 4020 xcp------------------------------------3: 3 2828 xcp------------------------------------4: 4 4020 xcp------------------------------------5: 5 2828 xcp------------------------------------6: 6 4020 xcp------------------------------------7: 7 2828 xcp------------------------------------8: 8 4020 xcp------------------------------------9: 9 2828 xcp------------------------------------10: 10 4020 xcp------------------------------------11: 11 2828 xcp------------------------------------12: 12 4020 xcp------------------------------------13: 13 2828 xcp------------------------------------14: 14 4020 xcp------------------------------------15: 15 2828 xcp------------------------------------16: 16 4020 xcp------------------------------------17: 17 2828 xcp------------------------------------18: 18 4020 xcp------------------------------------19: 19 2828 xcp------------------------------------20: 20 4020 xcp------------------------------------