Browse Source

db_api 和 项目中使用的libya_db.so统一代码

chensongchao 6 years ago
parent
commit
aa43e7cef7
2 changed files with 25 additions and 18 deletions
  1. 24 17
      db/db_api/CDBConnPool.cpp
  2. 1 1
      db/db_api/CDBConnPool.h

+ 24 - 17
db/db_api/CDBConnPool.cpp

@@ -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 );
-	}
+    }
 }

+ 1 - 1
db/db_api/CDBConnPool.h

@@ -44,7 +44,7 @@ namespace YADB
 		std::list<CDBConnect*> __IdleConnList;//没有使用的数据库连接列表
 		_DB_POOL_SETTING_ __Setting;//数据库连接池设置
 		std::mutex __mtx;//互斥量
-		boost::atomic<bool> __Running{ true };//线程是否运行的标识
+		boost::atomic<bool> __Running{ false };//线程是否运行的标识 默认未开启,等创建线程后开启
 		boost::atomic<bool> __IsExited{ false };//线程是否已退出的标识
 		CDBConnect* __pAsyncDBConn;//异步执行线程用的数据库连接
 	private: