Subject | Multiple threads with one connection each |
---|---|
Author | Datatal AB - Gauffin, Jonas |
Post date | 2003-02-26T10:10:36Z |
Hello
I'm writing a small test application for firebird that uses Open
FireBird ODBC Driver.
Everything works nice when I got one connection.
But as soon as I load up more threads, with one connection each,
everything stops in the execution of a sql statement (SELECT
GEN_ID(GEN_USER, 1) FROM RDB$DATABASE).
I've started with a thread that do random reads and a thread that do
random inserts/updates.
This is the read thread:
UINT CFirebirdDDlg::ThreadFuncRead(LPVOID params)
{
ThreadParamsStruct& p = *(ThreadParamsStruct*)params;
AddText(p.hWnd, p.id+10, "ReadThread(%d) STARTED\n\n", p.id);
CDatabase db;
Connect(db);
CRecordset rs(&db);
Sleep(3000);
DWORD dwStart, dwStop;
CString sValue;
int nId;
char szSql[384];
for (int i=0; i < 40000; i++)
{
Execute(rs, "SELECT GEN_ID(GEN_USER, 0) FROM
RDB$DATABASE");
rs.GetFieldValue(0, sValue);
nId = MyRand(1, atoi(sValue));
rs.Close();
dwStart = GetTickCount();
switch (MyRand(0,3))
{
case 0:
sprintf(szSql, "select * from user_info
ui, user_number un where ui.userid=un.userid and un.phoneno=%d",
MyRand(1000,5000));
break;
case 1:
sprintf(szSql, "select * from user_info
ui where userid=%d", MyRand(1,nId));
break;
case 2:
sprintf(szSql, "select * from user_info
where office='%s' and lastname='%s'", p.dlg->GetOffice(),
p.dlg->GetLastName());
break;
}
//odbc.Execute(szSql);
Execute(rs, szSql);
dwStop = GetTickCount();
rs.Close();
AddText(p.hWnd, p.id, "ReadThread(%d) %s took %d ms",
p.id, szSql, dwStop-dwStart);
Sleep(500);
}
return 0;
}
void Connect(CDatabase &db)
{
try
{
db.OpenEx("DSN=firebird;UID=SYSDBA;PWD=masterkey;");
}
catch (CDBException ex)
{
char msg[257];
ex.GetErrorMessage(msg, 256);
AfxMessageBox(msg);
}
}
void Execute(CRecordset &rs, LPCTSTR sql)
{
try
{
rs.Open(CRecordset::forwardOnly, sql);
}
catch (CDBException ex)
{
char msg[257];
ex.GetErrorMessage(msg, 256);
AfxMessageBox(msg);
}
}
Im doing the test just to get a hang of everything..
What can be wrong?
I'm writing a small test application for firebird that uses Open
FireBird ODBC Driver.
Everything works nice when I got one connection.
But as soon as I load up more threads, with one connection each,
everything stops in the execution of a sql statement (SELECT
GEN_ID(GEN_USER, 1) FROM RDB$DATABASE).
I've started with a thread that do random reads and a thread that do
random inserts/updates.
This is the read thread:
UINT CFirebirdDDlg::ThreadFuncRead(LPVOID params)
{
ThreadParamsStruct& p = *(ThreadParamsStruct*)params;
AddText(p.hWnd, p.id+10, "ReadThread(%d) STARTED\n\n", p.id);
CDatabase db;
Connect(db);
CRecordset rs(&db);
Sleep(3000);
DWORD dwStart, dwStop;
CString sValue;
int nId;
char szSql[384];
for (int i=0; i < 40000; i++)
{
Execute(rs, "SELECT GEN_ID(GEN_USER, 0) FROM
RDB$DATABASE");
rs.GetFieldValue(0, sValue);
nId = MyRand(1, atoi(sValue));
rs.Close();
dwStart = GetTickCount();
switch (MyRand(0,3))
{
case 0:
sprintf(szSql, "select * from user_info
ui, user_number un where ui.userid=un.userid and un.phoneno=%d",
MyRand(1000,5000));
break;
case 1:
sprintf(szSql, "select * from user_info
ui where userid=%d", MyRand(1,nId));
break;
case 2:
sprintf(szSql, "select * from user_info
where office='%s' and lastname='%s'", p.dlg->GetOffice(),
p.dlg->GetLastName());
break;
}
//odbc.Execute(szSql);
Execute(rs, szSql);
dwStop = GetTickCount();
rs.Close();
AddText(p.hWnd, p.id, "ReadThread(%d) %s took %d ms",
p.id, szSql, dwStop-dwStart);
Sleep(500);
}
return 0;
}
void Connect(CDatabase &db)
{
try
{
db.OpenEx("DSN=firebird;UID=SYSDBA;PWD=masterkey;");
}
catch (CDBException ex)
{
char msg[257];
ex.GetErrorMessage(msg, 256);
AfxMessageBox(msg);
}
}
void Execute(CRecordset &rs, LPCTSTR sql)
{
try
{
rs.Open(CRecordset::forwardOnly, sql);
}
catch (CDBException ex)
{
char msg[257];
ex.GetErrorMessage(msg, 256);
AfxMessageBox(msg);
}
}
Im doing the test just to get a hang of everything..
What can be wrong?