📄 pgsqlconnection.cpp
字号:
const char** argv): BaseConnection(PGSQL_DRIVERNAME), _numHandles(0), _handles(NULL){ // Store any arguments in a easy to use structure. (Must be even number) // All argument names will be in lowercase. if (argc % 2 == 0) for (int i=0; i<argc; i+=2) { // Check for SSL been required if (strcasecmp(argv[i], "requireSSL") == 0) if (strcasecmp(argv[i+1], "yes") == 0) _pgsqlOptions.requireSSL = true; else _pgsqlOptions.requireSSL = false; // Check for connection timeout if (strcasecmp(argv[i], "connectionTimeout") == 0) _pgsqlOptions.connectionTimeout = argv[i+1]; // Check for port if (strcasecmp(argv[i], "port") == 0) _pgsqlOptions.port = argv[i+1]; } } // PostgresqlConnection::PostgresqlConnection// -----------------------------------------------------------------------------// PostgresqlConnection::~PostgresqlConnection// -----------------------------------------------------------------------------PostgresqlConnection::~PostgresqlConnection(){ // Make sure we disconnect from the database. if (isConnected) { disconnect(120); // 2 Minute timeout. } // Free any handles. _freeCollection(CONNECTION_HANDLES);} // PostgresqlConnection::~PostgresqlConnection// -----------------------------------------------------------------------------// PostgresqlConnection::connect// -----------------------------------------------------------------------------void PostgresqlConnection::connect( const string &username, const string &password, const string &databaseName, const string &host, int maxConnections, int minConnections, const string &optParam1, const string &optParam2){ // Synchronize the function. SimpleThread_Synchronize sync(classMutex); // Make sure we are not already connected. if (isConnected) throw ErrorConnecting("connect(): Already connected to the database."); // Lower the database name: Postgresql always forces the database name to lowercase when it is created string lDatabaseName = databaseName; for (int i=0; i<lDatabaseName.length(); i++) lDatabaseName[i] = tolower(lDatabaseName[i]); // Check and set the parameters. (Use the base class for this) BaseConnection::connect(username, password, lDatabaseName, host, maxConnections, minConnections, optParam1, optParam2); // Free any handles used before. _freeCollection(CONNECTION_HANDLES); // Create the collection to hold the needed handle information. _numHandles = maxConnections; _handles = (PgsqlHandle**)malloc(_numHandles * sizeof(PgsqlHandle*)); for (int i=0; i<_numHandles; i++) { _handles[i] = new PgsqlHandle(); _handles[i]->status = BaseHandle::DISCONNECTED; } // Connect the required specified minimum connections. The rest will be on demand. for (int j=0; j<minConnections; j++) _pgsqlConnect(j); isConnected = true; } // PostgresqlConnection::connect// -----------------------------------------------------------------------------// PostgresqlConnection::disconnect// -----------------------------------------------------------------------------voidPostgresqlConnection::disconnect( time_t timeout){ // Synchronize the function. SimpleThread_Synchronize sync(classMutex); bool doneTimeout = false; // Only disconnect if we are connected. if (!isConnected) throw NotConnected("disconnect(): Not connected to the database."); // Disconnect all the open connections. for (int i=0; i<_numHandles; i++) { // Free the query object if it is still attached. // TODO if (_handles[i]->status == BaseHandle::CONNECTED || _handles[i]->status == BaseHandle::CONNECTED_USED) { // If the handle is USED wait for the timeout. // Only wait once for the timeout for all connections. if (_handles[i]->status == BaseHandle::CONNECTED_USED && !doneTimeout) { SimpleThread::sleep(timeout * 1000); doneTimeout = true; } _pgsqlDisconnect(i); } } isConnected = false;} // PostgresqlConnection::disconnect// -----------------------------------------------------------------------------// PostgresqlConnection::requestQueryConnection// -----------------------------------------------------------------------------void*PostgresqlConnection::requestQueryConnection(){ // This function must act as a fifo stack. The first thread in must get the first // available connection. // Synchronize the function. SimpleThread_Synchronize sync(classMutex); // Make sure we are connected. if (!isConnected) throw NotConnected("requestQueryConnection(): Not connected to the database."); // Flag that a request is occuring. isRequestQueryConnectionOccuring = true; // Loop until we have a valid connection or an error. // Built in timeout of 1 min per thread to retrieve a connection. int i; time_t now = time(NULL); while (time(NULL) <= now + 60) { // Try and obtain a connection for (i=0; i<_numHandles; i++) { // We have an available handle thats already connected. if (_handles[i]->status == BaseHandle::CONNECTED) { _handles[i]->queryObject = new PostgresqlQuery(this, i); _handles[i]->status = BaseHandle::CONNECTED_USED; isRequestQueryConnectionOccuring = false; return _handles[i]->queryObject; } // We have an available handle that needs to be connected. if (_handles[i]->status == BaseHandle::DISCONNECTED) { _pgsqlConnect(i); _handles[i]->queryObject = new PostgresqlQuery(this, i); _handles[i]->status = BaseHandle::CONNECTED_USED; isRequestQueryConnectionOccuring = false; return _handles[i]->queryObject; } } // Sleep for a second to conserve resources SimpleThread::sleep(1000); } // If we got this far a timeout occured. isRequestQueryConnectionOccuring = false; throw QueryConnectionTimeout("requestQueryConnection(): A timout occured " "while trying to obtain a query connection.");} // PostgresqlConnection::requestQueryConnection// -----------------------------------------------------------------------------// PostgresqlConnection::releaseQueryConnection// -----------------------------------------------------------------------------voidPostgresqlConnection::releaseQueryConnection( void* queryObject){ // Don't synchronize as we may need to release a connection to allow // requestQueryConnection to get an available one // Find the handle that has the query connection instance int i; for (i=0; i<_numHandles; i++) { if (_handles[i]->queryObject == queryObject) { _handles[i]->queryObject = NULL; _handles[i]->status = BaseHandle::CONNECTED; // Check if we need to release the connection. // We release the connection if the connection is outside of the minimum // connections and there is no current request for a connection waiting. if (i >= minConnections && !isRequestQueryConnectionOccuring) { _pgsqlDisconnect(i); } break; } }} // PostgresqlConnection::releaseQueryConnection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -