Greetings,
i am having alot of problems making a connection pool. I have made a QHash with a Qstring and a QSqlDatabase pointer.
QHash<QString, QSqlDatabase* > openConnections;
To copy to clipboard, switch view to plain text mode
I try to open a connection with this:
for(int i=0; i<number;i++){
DatabaseConnectionName
=dbNameX.
append(QString::number(i
));
db->addDatabase("QODBC",DatabaseConnectionName);
db->setDatabaseName(dbName);
db->setPassword(password);
db->setUserName(userName);
if(!db->open()){
errDB = db->lastError();
std::cout << qPrintable(errDB.text()) << std::endl;
}
openConnections.insert(dbName, db);
}
for(int i=0; i<number;i++){
QSqlDatabase *db;
db = new QSqlDatabase;
QString DatabaseConnectionName;
QString dbNameX=dbName;
DatabaseConnectionName=dbNameX.append(QString::number(i));
db->addDatabase("QODBC",DatabaseConnectionName);
db->setDatabaseName(dbName);
db->setPassword(password);
db->setUserName(userName);
if(!db->open()){
QSqlError errDB;
errDB = db->lastError();
std::cout << qPrintable(errDB.text()) << std::endl;
}
openConnections.insert(dbName, db);
}
To copy to clipboard, switch view to plain text mode
I get a driver not loaded error. If i do this for a single connection everthing works:
db.setDatabaseName("TESTDB");
db.setUserName("USER");
db.setPassword("PASS");
if( !db.open())
{
errDBRev = db.lastError();
std::cout << qPrintable(errDBRev.text()) << std::endl;
}else{
std::cout << "Connect worked" << std::endl;
}
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QODBC","TESTDB");
db.setDatabaseName("TESTDB");
db.setUserName("USER");
db.setPassword("PASS");
if( !db.open())
{
QSqlError errDBRev;
errDBRev = db.lastError();
std::cout << qPrintable(errDBRev.text()) << std::endl;
}else{
std::cout << "Connect worked" << std::endl;
}
To copy to clipboard, switch view to plain text mode
Can someone point me in the right direction.
I need a connection pool since my program makes alot of querrys on multiple databases ( monitoring sistem ). Mostly it holds about 500 active threads that need to be connected to 2 DBs and execute specific SQLs every X seconds/minutes/hours and i know opening and closing connections is a costly procedure.
With regards,
Marko
Bookmarks