📄 ibmdb2connection.cpp
字号:
/* * DB2Connection object defines the needed connection functions for the dbConnect IBM DB2 driver * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US * */#include "ibmDB2Connection.h"#include "ibmDB2Query.h"#include <stdio.h>// -----------------------------------------------------------------------------// PRIVATE:// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------// DB2Connection::_db2Connect// -----------------------------------------------------------------------------void DB2Connection::_db2Connect( int index){ // Make sure the handle is valid and we are not already connected. if (index > _numHandles || index < 0) throw Error("_db2Connect(): Invalid index to database handle."); if (_handles[index]->status != BaseHandle::DISCONNECTED) throw AlreadyConnected("_db2Connect(): " "The database connection is already connected to the database."); SQLRETURN cliRC = SQL_SUCCESS; // Allocate the connection handle cliRC = SQLAllocHandle(SQL_HANDLE_DBC, _henv, &_handles[index]->_hdbc); if (cliRC != SQL_SUCCESS) throw Error("_db2Connect(): " "Unable to allocate the connection handle."); // Connect to the IBM DB2 database. cliRC = SQLConnect(_handles[index]->_hdbc, (SQLCHAR*)databaseName.c_str(), SQL_NTS, (SQLCHAR*)username.c_str(), SQL_NTS, (SQLCHAR*)password.c_str(), SQL_NTS); // Check if an error occured while connecting to the database. if (cliRC != SQL_SUCCESS) { SQLCHAR sqlState[10]; SQLINTEGER sqlCode; SQLCHAR message[255]; SQLSMALLINT length; SQLGetDiagRec(SQL_HANDLE_DBC, _handles[index]->_hdbc, 1, sqlState, &sqlCode, message, 250, &length); string err = "_db2Connect(): "; err += (const char*)message; // Free the connection handle allocated SQLFreeHandle(SQL_HANDLE_DBC, _handles[index]->_hdbc); _handles[index]->_hdbc = SQL_NULL_HDBC; throw ErrorConnecting(err); } // Set the connection autocommit to off cliRC = SQLSetConnectAttr(_handles[index]->_hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_NTS); // Check if an error occured setting the autocommit to false if (cliRC != SQL_SUCCESS) { SQLCHAR sqlState[10]; SQLINTEGER sqlCode; SQLCHAR message[255]; SQLSMALLINT length; SQLGetDiagRec(SQL_HANDLE_DBC, _handles[index]->_hdbc, 1, sqlState, &sqlCode, message, 250, &length); string err = "_db2Connect(): Unable to set autocommit off, "; err += (const char*)message; // Disconnect from the server. SQLDisconnect(_handles[index]->_hdbc); // Free the connection handle allocated SQLFreeHandle(SQL_HANDLE_DBC, _handles[index]->_hdbc); _handles[index]->_hdbc = SQL_NULL_HDBC; throw ErrorConnecting(err); } _handles[index]->status = BaseHandle::CONNECTED; _handles[index]->lastUsed = time(NULL); } // DB2Connection::_db2Connect// -----------------------------------------------------------------------------// DB2Connection::_db2Disconnect// -----------------------------------------------------------------------------void DB2Connection::_db2Disconnect( int index){ // Make sure the handle is valid and we are not already connected. if (index > _numHandles || index < 0) throw Error("_db2Disconnect(): Invalid index to database handle."); if (_handles[index]->status == BaseHandle::DISCONNECTED || _handles[index]->status == BaseHandle::UNKNOWN) throw NotConnected("_db2Disconnect(): " "The database handle does not have a valid connection to the database."); // Disconnect from the server. SQLDisconnect(_handles[index]->_hdbc); // Free the connection handle allocated SQLFreeHandle(SQL_HANDLE_DBC, _handles[index]->_hdbc); _handles[index]->_hdbc = SQL_NULL_HDBC; _handles[index]->status = BaseHandle::DISCONNECTED; _handles[index]->lastUsed = time(NULL);} // DB2Connection::_db2Disconnect// -----------------------------------------------------------------------------// DB2Connection::_db2Ping// -----------------------------------------------------------------------------void DB2Connection::_db2Ping( int index){ // Make sure the handle is valid. if (index > _numHandles || index < 0) throw Error("_db2Ping(): Invalid index to database handle."); // Only ping the connection if the ping interval has expired since the // connection was last used. time_t currentTime = time(NULL); if (currentTime - _handles[index]->lastUsed >= pingInterval) { // Do a dummy query SQLRETURN cliRC = SQL_SUCCESS; SQLHANDLE hstmt; // Directly execute the statement after allocating the statement handle cliRC = SQLAllocHandle(SQL_HANDLE_STMT, _handles[index]->_hdbc, &hstmt); if (cliRC == SQL_SUCCESS) cliRC = SQLExecDirect(hstmt, (SQLCHAR*)"SELECT CURRENT DATE FROM SYSIBM.SYSDUMMY1", SQL_NTS); // Directly execute the statement if (cliRC != SQL_SUCCESS) { SQLFreeHandle(SQL_HANDLE_STMT, hstmt); // Connection is stale, need to reconnect // Synchronize the function as we dont want this connection taken from us SimpleThread_Synchronize sync(classMutex); //1. Disconnect ingnoring errors try { _db2Disconnect(index); } catch(...) { } // Ignore any errors from the disconnect. //2. Try get a new connection try { _db2Connect(index); // Force the connection to be in use again _handles[index]->status = BaseHandle::CONNECTED_USED; } catch(...) { string err = "_db2Ping(): Unable to establish new connection, "; throw ErrorPingingConnection(err); } } else { // Ping was OK SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } }} // DB2Connection::_db2Ping//------------------------------------------------------------------------------// DB2Connection::_freeCollection//------------------------------------------------------------------------------void DB2Connection::_freeCollection( CollectionType type){ int i; switch (type) { case CONNECTION_HANDLES: if (_handles) { for (i=0; i<_numHandles; i++) { if (_handles[i]) { delete _handles[i]; _handles[i] = NULL; } } free(_handles); _handles = NULL; _numHandles = 0; } break; }} // DB2Connection::_freeCollection// -----------------------------------------------------------------------------// PUBLIC:// -----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -