本文整理汇总了C++中IDatabase::connect方法的典型用法代码示例。如果您正苦于以下问题:C++ IDatabase::connect方法的具体用法?C++ IDatabase::connect怎么用?C++ IDatabase::connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDatabase
的用法示例。
在下文中一共展示了IDatabase::connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getDatabase
IDatabase* DatabaseFactory::getDatabase(EDataTypes dataType, EDataActions dataAction)
{
FUNCTION_ENTRY("getDatabase");
// Set up a local variable to point to the database that is either created or found
IDatabase* theDatabase = NULL;
ThreadGuard guard( m_getDatabaseLock );
// get the connection string for this datatype/action
std::string dbConnection;
// forever loop will exit by exception or when a good db is found
while(1)
{
// If this call fails it will throw something.
try
{
DbConnection::getInstance().getConnectionString(dataType, dataAction, dbConnection);
}
catch(DbConnectionFailed&)
{
// we exit the forever loop with this throw
TA_THROW(DatabaseException("Unable to find a working database"));
}
// Get the ID of this thread - we want one database connection per thread
int threadId = Thread::getCurrentThreadId();
// Look to see if there is already a database connection for this thread
//<ThreadID: Map<ConnectionString, SimpleDBDatabase>>
ThreadMap::iterator threadIter ( m_databaseMap.find( threadId ));
if ( threadIter == m_databaseMap.end() )
{
// add new ConnectionMap. Set db* to null untill it's defined
ConnectionMap cmap;
cmap.insert( ConnectionMap::value_type( dbConnection, 0 ) );
threadIter = m_databaseMap.insert(ThreadMap::value_type(threadId, cmap) ).first;
}
ConnectionMap::iterator connIter ( threadIter->second.find( dbConnection ) );
if (connIter == threadIter->second.end())
{
connIter = threadIter->second.insert(ConnectionMap::value_type( dbConnection, 0 )).first;
}
if (connIter->second != 0 )
{
// Then the connection already exists.
theDatabase = connIter->second; //TODO: need to set dataType and DataAction
theDatabase->setDataTypeAction(dataType, dataAction);
}
else // Database doesn't exist for this thread, so need to create it.
{
LOG(SourceInfo, DebugUtil::GenericLog, DebugUtil::DebugInfo, "New SimpleDbDatabase object created for thread %lu", threadId);
theDatabase = new TA_Base_Core::SimpleDbDatabase(dataType, dataAction);
// add to map
connIter->second = theDatabase;
}
// Now that we have a database object, we need to connect it - do this everytime
// 'cause if the database is alerady connected, nothing will happen, but if it has
// been inadvertandtly disconnected, then it will be reconnected
try // The connect line can generate a DatabaseException
{
theDatabase->connect( dbConnection );
// we can also exit the forever loop this (preferred) way
FUNCTION_EXIT;
return theDatabase;
}
catch (DatabaseException&)
{
// A DatabaseException was generated. Need to delete the database object for now.
delete theDatabase;
threadIter->second.erase(connIter);
TA_Base_Core::Thread::sleep(100);
// do not rethrow, just sleep for a while
// there may be another database we can talk to.
}
} // end forever loop
}