|
@@ -65,15 +65,20 @@ namespace YADB
|
|
|
}
|
|
|
|
|
|
__Setting = Setting;
|
|
|
- for ( int i = 0; i < __Setting.PoolSize; i++ )
|
|
|
+ //检查连接池中是否已有连接数量
|
|
|
+ if ((int)__IdleConnList.size() < __Setting.PoolSize)
|
|
|
{
|
|
|
- CDBConnect* pConn = __CreateIdleConn( szError );
|
|
|
- if ( !pConn )
|
|
|
+ for ( int i = 0; i < __Setting.PoolSize; i++ )
|
|
|
{
|
|
|
- return false;
|
|
|
- }
|
|
|
+ CDBConnect* pConn = __CreateIdleConn( szError );
|
|
|
+ if ( !pConn )
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- if (bAsync)
|
|
|
+ // 是否已创建异步线程
|
|
|
+ if (bAsync || !__Running)
|
|
|
{
|
|
|
//创建异步执行线程
|
|
|
__CreateAsyncThrdConn();
|
|
@@ -141,7 +146,7 @@ namespace YADB
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- __IdleConnList.pop_front(); //zzj, ERROR!!,应该是pop_back吧
|
|
|
+ __IdleConnList.pop_front();
|
|
|
__BusyConnList.push_back( pConn );
|
|
|
}
|
|
|
else
|
|
@@ -151,7 +156,7 @@ namespace YADB
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //验证看数据库连接是否还有效 //zzj, 新建的链接不需要测试
|
|
|
+ //验证看数据库连接是否还有效
|
|
|
if ( pConn )
|
|
|
{
|
|
|
if ( pConn->ConnctionTest( Error ) != 0 )
|
|
@@ -172,7 +177,6 @@ namespace YADB
|
|
|
return pConn;
|
|
|
}
|
|
|
|
|
|
- //zzj, 建议使用栈展开的方式 自动回收
|
|
|
void CDBConnPool::GiveBack( CDBConnect * pConn )
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock( __mtx );
|
|
@@ -284,6 +288,7 @@ namespace YADB
|
|
|
|
|
|
void CDBConnPool::__StartAsyncThrd()
|
|
|
{
|
|
|
+ __Running = true;
|
|
|
boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
|
|
|
thrd.detach();
|
|
|
}
|
|
@@ -291,6 +296,8 @@ namespace YADB
|
|
|
void CDBConnPool::__CreateAsyncThrdConn()
|
|
|
{
|
|
|
std::string ConnErr;
|
|
|
+ //先断开之前的连接
|
|
|
+ this->__DestroyAsyncThrdConn();
|
|
|
|
|
|
_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
CDBConnect* pConn = new CDBConnect();
|
|
@@ -327,7 +334,7 @@ namespace YADB
|
|
|
}
|
|
|
|
|
|
bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes,std::string& Error )
|
|
|
- {
|
|
|
+ {
|
|
|
CDBConnect *pConn = GetDBConnect( Error );
|
|
|
if ( 0 == pConn )
|
|
|
{
|
|
@@ -335,9 +342,9 @@ namespace YADB
|
|
|
}
|
|
|
|
|
|
MYSQL_RES* pRes = pConn->Query( szSql, Error );
|
|
|
- GiveBack( pConn );
|
|
|
- return DBRes.Bind( pRes, Error );
|
|
|
- }
|
|
|
+ GiveBack( pConn );
|
|
|
+ return DBRes.Bind( pRes, Error );
|
|
|
+ }
|
|
|
|
|
|
MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error)
|
|
|
{
|
|
@@ -351,15 +358,15 @@ namespace YADB
|
|
|
return pRes;
|
|
|
}
|
|
|
my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
|
|
|
- {
|
|
|
- CDBConnect *pConn = GetDBConnect( Error );
|
|
|
+ {
|
|
|
+ CDBConnect *pConn = GetDBConnect( Error );
|
|
|
if ( 0 == pConn )
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
- my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
|
|
|
+ my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
|
|
|
GiveBack( pConn );
|
|
|
return nRet;
|
|
|
//return pConn->ExecuteSql( szSql, Error );
|
|
|
- }
|
|
|
+ }
|
|
|
}
|