23 #include "CibernateConnectionPool.h"
32 CibernateConnectionPool::CibernateConnectionPool(
int size,
string dbName,
string uname,
string pass)
34 logger = Logger::getLogger(
"CibernateConnectionPool");
35 createPool(size,dbName,uname,pass);
38 CibernateConnectionPool::~CibernateConnectionPool()
40 for (
int var = 0; var < (int)readConnections.size(); ++var) {
41 delete readConnections.at(var);
43 for (
int var = 0; var < (int)writeConnections.size(); ++var) {
44 delete writeConnections.at(var);
46 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
47 logger <<
"\nDestructed CibernateConnectionPool" << flush;
50 void CibernateConnectionPool::closeConnection(
Connection *conn)
55 void CibernateConnectionPool::newConnection(
bool read)
58 SQLCHAR V_OD_msg[200],V_OD_stat[10];
59 SQLSMALLINT V_OD_mlen;
60 SQLINTEGER V_OD_err_s;
63 V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &(connection->conn));
64 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
66 logger <<
"Error AllocHDB " << V_OD_erg << endl;
67 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
68 this->initialized =
false;
72 SQLSetConnectAttr(connection->conn, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
74 V_OD_erg = SQLConnect(connection->conn, (SQLCHAR*) this->dbName.c_str(), this->dbName.length(),
75 (SQLCHAR*) this->uname.c_str(), this->uname.length(),
76 (SQLCHAR*) this->pass.c_str(), this->pass.length());
77 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
79 logger <<
"Error SQLConnect " << V_OD_erg << endl;
80 SQLGetDiagRec(SQL_HANDLE_DBC, connection->conn,1,
81 V_OD_stat, &V_OD_err_s,V_OD_msg,100,&V_OD_mlen);
82 logger << V_OD_msg <<
" (" << (int)V_OD_err_s <<
")" << endl;
83 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
84 this->initialized =
false;
88 connection->busy =
false;
89 connection->type = read;
91 this->readConnections.push_back(connection);
93 this->writeConnections.push_back(connection);
97 void CibernateConnectionPool::createPool(
int size,
string dbName,
string uname,
string pass)
99 this->dbName = dbName;
102 this->readNumber = 0;
103 int reads = round(size/5);
108 this->initialized =
true;
109 V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);
110 if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
112 logger <<
"Error AllocHandle" << endl;
113 this->initialized =
false;
115 V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (
void*)SQL_OV_ODBC3, 0);
116 if((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
118 logger <<
"Error SetEnv" << endl;
119 SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
120 this->initialized =
false;
122 for(
int i=0;i<reads;i++)
124 this->newConnection(
true);
125 if(!this->initialized)
128 for(
int i=0;i<size-reads;i++)
130 this->newConnection(
false);
131 if(!this->initialized)
134 if(!this->initialized)
136 logger <<
"Failed to create pool !" << endl;
140 logger <<
"Created pool successfully !" << endl;
144 Connection* CibernateConnectionPool::getReadConnection()
146 if(this->readNumber==(
int)this->readConnections.size())
147 this->readNumber = 0;
148 return this->readConnections.at(this->readNumber++);
150 Connection* CibernateConnectionPool::getWriteConnection()
156 for(
unsigned int i=0;i<this->writeConnections.size();i++)
158 if(!this->writeConnections.at(i)->busy)
159 return this->writeConnections.at(i);
161 if(t.elapsedMilliSeconds()>500)
163 newConnection(
false);
164 return this->writeConnections.at(this->writeConnections.size()-1);
170 Connection::Connection()
172 logger = Logger::getLogger(
"Connection");
173 logger <<
"\nCreated Connection" << flush;
176 Connection::~Connection()
179 SQLFreeHandle(SQL_HANDLE_DBC,conn);
180 logger <<
"\nDestructed Connection" << flush;