|
@@ -1,374 +1,387 @@
|
|
|
-#include "CDBConnPool.h"
|
|
|
-#include <boost/bind.hpp>
|
|
|
-#include <boost/lockfree/queue.hpp>
|
|
|
-
|
|
|
-namespace YADB
|
|
|
-{
|
|
|
- boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;
|
|
|
-
|
|
|
- CDBConnPool::CDBConnPool()
|
|
|
- {
|
|
|
- __pAsyncDBConn = 0;
|
|
|
- }
|
|
|
-
|
|
|
- CDBConnPool::~CDBConnPool()
|
|
|
- {
|
|
|
- Close();
|
|
|
- }
|
|
|
-
|
|
|
- CDBConnect * CDBConnPool::__CreateIdleConn( std::string& Error, bool IsTemp )
|
|
|
- {
|
|
|
- std::string ConnErr;
|
|
|
-
|
|
|
- _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
- CDBConnect* pConn = new CDBConnect( IsTemp );
|
|
|
- if ( !pConn->Connect( ConnSetting, ConnErr ) )
|
|
|
- {
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if ( !ConnSetting.stmtSQL.empty() )
|
|
|
- {
|
|
|
- if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), Error ) )
|
|
|
- {
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- return 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- __IdleConnList.push_back( pConn );
|
|
|
- return pConn;
|
|
|
- }
|
|
|
-
|
|
|
- bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& Error )
|
|
|
- {
|
|
|
- std::unique_lock<std::mutex> lock( __mtx );
|
|
|
-
|
|
|
- if ( Setting.PoolSize < DCC_MIN_COUNT )
|
|
|
- {
|
|
|
- Error = "PoolSize is too small!";
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if ( Setting.PoolSize > DCC_MAX_COUNT )
|
|
|
- {
|
|
|
- Error = "PoolSize is too big!";
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- __Setting = Setting;
|
|
|
- std::string ConnErr;
|
|
|
- for ( int i = 0; i < __Setting.PoolSize; i++ )
|
|
|
- {
|
|
|
- CDBConnect* pConn = __CreateIdleConn( Error );
|
|
|
- if ( !pConn )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- __CreateAsyncThrdConn();
|
|
|
-
|
|
|
-
|
|
|
- __StartAsyncThrd();
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::Close()
|
|
|
- {
|
|
|
- std::unique_lock<std::mutex> lock( __mtx );
|
|
|
-
|
|
|
-
|
|
|
- __StopAsyncThrd();
|
|
|
-
|
|
|
-
|
|
|
- __DestroyAsyncThrdConn();
|
|
|
-
|
|
|
-
|
|
|
- CDBConnect* pConn = 0;
|
|
|
- std::list<CDBConnect*>::iterator lit_conn;
|
|
|
- for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
|
|
|
- {
|
|
|
- pConn = *lit_conn;
|
|
|
- pConn->Close();
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- }
|
|
|
- __BusyConnList.clear();
|
|
|
-
|
|
|
- for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
|
|
|
- {
|
|
|
- pConn = *lit_conn;
|
|
|
- pConn->Close();
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- }
|
|
|
- __IdleConnList.clear();
|
|
|
- }
|
|
|
-
|
|
|
- CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
|
|
|
- {
|
|
|
- std::unique_lock<std::mutex> lock( __mtx );
|
|
|
-
|
|
|
- CDBConnect* pConn = 0;
|
|
|
-
|
|
|
- if ( __IdleConnList.size() > 0 )
|
|
|
- {
|
|
|
- pConn = *(__IdleConnList.begin());
|
|
|
- __IdleConnList.pop_front();
|
|
|
- __BusyConnList.push_back( pConn );
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- if ( __IdleConnList.size() < DCC_MAX_COUNT )
|
|
|
- {
|
|
|
- pConn = __CreateIdleConn( Error, true );
|
|
|
- if ( !pConn )
|
|
|
- {
|
|
|
- Error = "Error,failed connect to database!";
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- __IdleConnList.pop_front();
|
|
|
- __BusyConnList.push_back( pConn );
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Error = "Error,db connect count beyond the max connect count!";
|
|
|
- return 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if ( pConn )
|
|
|
- {
|
|
|
- if ( pConn->ConnctionTest( Error ) != 0 )
|
|
|
- {
|
|
|
-
|
|
|
- _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
- pConn->Close();
|
|
|
- int nRet = pConn->Connect( ConnSetting, Error );
|
|
|
- if ( nRet < 0 )
|
|
|
- {
|
|
|
- GiveBack( pConn );
|
|
|
- Error = "Error,failed connect to database!";
|
|
|
- return 0;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return pConn;
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::GiveBack( CDBConnect * pConn )
|
|
|
- {
|
|
|
- std::unique_lock<std::mutex> lock( __mtx );
|
|
|
-
|
|
|
- if ( 0 == pConn )
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- __BusyConnList.remove( pConn );
|
|
|
-
|
|
|
-
|
|
|
- if ( pConn->IsTemp() )
|
|
|
- {
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- __IdleConnList.push_back( pConn );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
|
|
|
- {
|
|
|
- std::string Error;
|
|
|
- while( pOwner->__Running )
|
|
|
- {
|
|
|
- _ASYNC_SQL_* pData = 0;
|
|
|
- while ( __AsyncQueue.pop( pData ) )
|
|
|
- {
|
|
|
- if ( pData )
|
|
|
- {
|
|
|
- if ( __pAsyncDBConn )
|
|
|
- {
|
|
|
- my_ulonglong llRes = 0;
|
|
|
- llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
|
|
|
- if ( -1 == llRes )
|
|
|
- {
|
|
|
-
|
|
|
- printf( "Error,璋冪敤ExcuteRealSql澶辫触,Err=%s\n", Error.c_str() );
|
|
|
-
|
|
|
- if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
|
|
|
- {
|
|
|
- _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
- __pAsyncDBConn->Close();
|
|
|
- int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
|
|
|
- if ( nRet < 0 )
|
|
|
- {
|
|
|
- Error = "Error,failed connect to database!";
|
|
|
-
|
|
|
- printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
|
|
|
-
|
|
|
- boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- pData->FailedCount++;
|
|
|
- if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
|
|
|
- {
|
|
|
- _ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
|
|
|
- pNewData->FailedCount = pData->FailedCount;
|
|
|
- pNewData->SQL = pData->SQL;
|
|
|
- __AsyncQueue.push( pNewData );
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- delete pData;
|
|
|
- pData = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- __IsExited = true;
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::__StopAsyncThrd()
|
|
|
- {
|
|
|
- if ( !__Running )
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- __Running = false;
|
|
|
-
|
|
|
- while ( !__IsExited )
|
|
|
- {
|
|
|
- boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- _ASYNC_SQL_* pData = 0;
|
|
|
- while ( __AsyncQueue.pop( pData ) )
|
|
|
- {
|
|
|
- if (pData)
|
|
|
- {
|
|
|
- delete pData;
|
|
|
- pData = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::__StartAsyncThrd()
|
|
|
- {
|
|
|
- boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
|
|
|
- thrd.detach();
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::__CreateAsyncThrdConn()
|
|
|
- {
|
|
|
- std::string ConnErr;
|
|
|
-
|
|
|
- _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
- CDBConnect* pConn = new CDBConnect();
|
|
|
- if ( !pConn->Connect( ConnSetting, ConnErr ) )
|
|
|
- {
|
|
|
- delete pConn;
|
|
|
- pConn = 0;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- __pAsyncDBConn = pConn;
|
|
|
- }
|
|
|
-
|
|
|
- void CDBConnPool::__DestroyAsyncThrdConn()
|
|
|
- {
|
|
|
- if ( __pAsyncDBConn )
|
|
|
- {
|
|
|
- __pAsyncDBConn->Close();
|
|
|
- delete __pAsyncDBConn;
|
|
|
- __pAsyncDBConn = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- bool CDBConnPool::PushAsync( const std::string& strSQL )
|
|
|
- {
|
|
|
- _ASYNC_SQL_* pData = new _ASYNC_SQL_;
|
|
|
- if ( !pData )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- pData->SQL = strSQL;
|
|
|
- return __AsyncQueue.push( pData );
|
|
|
- }
|
|
|
-
|
|
|
- bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes, std::string& Error )
|
|
|
- {
|
|
|
- CDBConnect *pConn = GetDBConnect( Error );
|
|
|
- if ( 0 == pConn )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- MYSQL_RES* pRes = pConn->Query( szSql, Error );
|
|
|
- GiveBack( pConn );
|
|
|
- return DBRes.Bind( pRes, Error );
|
|
|
- }
|
|
|
-
|
|
|
- MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error )
|
|
|
- {
|
|
|
- CDBConnect *pConn = GetDBConnect( Error );
|
|
|
- if ( 0 == pConn )
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- MYSQL_RES* pRes = pConn->Query( szSql, Error );
|
|
|
- GiveBack( pConn );
|
|
|
- return pRes;
|
|
|
- }
|
|
|
-
|
|
|
- my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
|
|
|
- {
|
|
|
- CDBConnect *pConn = GetDBConnect( Error );
|
|
|
- if ( 0 == pConn )
|
|
|
- {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
|
|
|
- GiveBack( pConn );
|
|
|
- return nRet;
|
|
|
- }
|
|
|
-
|
|
|
- my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
|
|
|
- {
|
|
|
- CDBConnect *pConn = GetDBConnect( Error );
|
|
|
- if ( 0 == pConn )
|
|
|
- {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
|
|
|
-
|
|
|
- GiveBack( pConn );
|
|
|
- return nRet;
|
|
|
- }
|
|
|
-}
|
|
|
+#include "CDBConnPool.h"
|
|
|
+#include <boost/bind.hpp>
|
|
|
+
|
|
|
+namespace YADB
|
|
|
+{
|
|
|
+ boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;
|
|
|
+
|
|
|
+ CDBConnPool::CDBConnPool()
|
|
|
+ {
|
|
|
+ __pAsyncDBConn = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ CDBConnPool::~CDBConnPool()
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ CDBConnect * CDBConnPool::__CreateIdleConn( std::string& ConnErr, bool IsTemp )
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
+ CDBConnect* pConn = new CDBConnect( IsTemp );
|
|
|
+ if ( !pConn->Connect( ConnSetting, ConnErr ) )
|
|
|
+ {
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if ( !ConnSetting.stmtSQL.empty() )
|
|
|
+ {
|
|
|
+ if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), ConnErr ) )
|
|
|
+ {
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ __IdleConnList.push_back( pConn );
|
|
|
+ return pConn;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& szError )
|
|
|
+ {
|
|
|
+ return Create(Setting,true,szError);
|
|
|
+ }
|
|
|
+
|
|
|
+ bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError )
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock( __mtx );
|
|
|
+
|
|
|
+ if ( Setting.PoolSize < DCC_MIN_COUNT )
|
|
|
+ {
|
|
|
+ szError = "PoolSize is too small!";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( Setting.PoolSize > DCC_MAX_COUNT )
|
|
|
+ {
|
|
|
+ szError = "PoolSize is too big!";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ __Setting = Setting;
|
|
|
+
|
|
|
+ if ((int)__IdleConnList.size() < __Setting.PoolSize)
|
|
|
+ {
|
|
|
+ for ( int i = 0; i < __Setting.PoolSize; i++ )
|
|
|
+ {
|
|
|
+ CDBConnect* pConn = __CreateIdleConn( szError );
|
|
|
+ if ( !pConn )
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bAsync || !__Running)
|
|
|
+ {
|
|
|
+
|
|
|
+ __CreateAsyncThrdConn();
|
|
|
+
|
|
|
+
|
|
|
+ __StartAsyncThrd();
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::Close()
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock( __mtx );
|
|
|
+
|
|
|
+
|
|
|
+ __StopAsyncThrd();
|
|
|
+
|
|
|
+
|
|
|
+ __DestroyAsyncThrdConn();
|
|
|
+
|
|
|
+
|
|
|
+ CDBConnect* pConn = 0;
|
|
|
+ std::list<CDBConnect*>::iterator lit_conn;
|
|
|
+ for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
|
|
|
+ {
|
|
|
+ pConn = *lit_conn;
|
|
|
+ pConn->Close();
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ }
|
|
|
+ __BusyConnList.clear();
|
|
|
+
|
|
|
+ for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
|
|
|
+ {
|
|
|
+ pConn = *lit_conn;
|
|
|
+ pConn->Close();
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ }
|
|
|
+ __IdleConnList.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock( __mtx );
|
|
|
+
|
|
|
+ CDBConnect* pConn = 0;
|
|
|
+
|
|
|
+ if ( __IdleConnList.size() > 0 )
|
|
|
+ {
|
|
|
+ pConn = *(__IdleConnList.begin());
|
|
|
+ __IdleConnList.pop_front();
|
|
|
+ __BusyConnList.push_back( pConn );
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ if ( __IdleConnList.size() < DCC_MAX_COUNT )
|
|
|
+ {
|
|
|
+ pConn = __CreateIdleConn( Error, true );
|
|
|
+ if ( !pConn )
|
|
|
+ {
|
|
|
+ Error = "Error,failed connect to database!";
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ __IdleConnList.pop_front();
|
|
|
+ __BusyConnList.push_back( pConn );
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Error = "Error,db connect count beyond the max connect count!";
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if ( pConn )
|
|
|
+ {
|
|
|
+ if ( pConn->ConnctionTest( Error ) != 0 )
|
|
|
+ {
|
|
|
+
|
|
|
+ _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
+ pConn->Close();
|
|
|
+ int nRet = pConn->Connect( ConnSetting, Error );
|
|
|
+ if ( nRet < 0 )
|
|
|
+ {
|
|
|
+ GiveBack( pConn );
|
|
|
+ Error = "Error,failed connect to database!";
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return pConn;
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::GiveBack( CDBConnect * pConn )
|
|
|
+ {
|
|
|
+ std::unique_lock<std::mutex> lock( __mtx );
|
|
|
+
|
|
|
+ if ( 0 == pConn )
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ __BusyConnList.remove( pConn );
|
|
|
+
|
|
|
+
|
|
|
+ if ( pConn->IsTemp() )
|
|
|
+ {
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ __IdleConnList.push_back( pConn );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
|
|
|
+ {
|
|
|
+ std::string Error;
|
|
|
+ while( pOwner->__Running )
|
|
|
+ {
|
|
|
+ _ASYNC_SQL_* pData = 0;
|
|
|
+
|
|
|
+ while ( __AsyncQueue.pop( pData ) )
|
|
|
+ {
|
|
|
+ if ( pData )
|
|
|
+ {
|
|
|
+ if ( __pAsyncDBConn )
|
|
|
+ {
|
|
|
+ my_ulonglong llRes = 0;
|
|
|
+ llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
|
|
|
+ if ( (my_ulonglong)-1 == llRes )
|
|
|
+ {
|
|
|
+
|
|
|
+ printf( "Error,调用ExcuteRealSql失败,Err=%s\n", Error.c_str() );
|
|
|
+
|
|
|
+ if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
|
|
|
+ {
|
|
|
+ _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
+ __pAsyncDBConn->Close();
|
|
|
+ int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
|
|
|
+ if ( nRet < 0 )
|
|
|
+ {
|
|
|
+ Error = "Error,failed connect to database!";
|
|
|
+
|
|
|
+ printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
|
|
|
+
|
|
|
+ boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ pData->FailedCount++;
|
|
|
+ if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
|
|
|
+ {
|
|
|
+ _ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
|
|
|
+ pNewData->FailedCount = pData->FailedCount;
|
|
|
+ pNewData->SQL = pData->SQL;
|
|
|
+ __AsyncQueue.push( pNewData );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ delete pData;
|
|
|
+ pData = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ __IsExited = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::__StopAsyncThrd()
|
|
|
+ {
|
|
|
+ if ( !__Running )
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ __Running = false;
|
|
|
+
|
|
|
+ while ( !__IsExited )
|
|
|
+ {
|
|
|
+ boost::this_thread::sleep(boost::posix_time::millisec(1));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ _ASYNC_SQL_* pData = 0;
|
|
|
+ while ( __AsyncQueue.pop( pData ) )
|
|
|
+ {
|
|
|
+ if (pData)
|
|
|
+ {
|
|
|
+ delete pData;
|
|
|
+ pData = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::__StartAsyncThrd()
|
|
|
+ {
|
|
|
+ __Running = true;
|
|
|
+ boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
|
|
|
+ thrd.detach();
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::__CreateAsyncThrdConn()
|
|
|
+ {
|
|
|
+ std::string ConnErr;
|
|
|
+
|
|
|
+ this->__DestroyAsyncThrdConn();
|
|
|
+
|
|
|
+ _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
|
|
|
+ CDBConnect* pConn = new CDBConnect();
|
|
|
+ if ( !pConn->Connect( ConnSetting, ConnErr ) )
|
|
|
+ {
|
|
|
+ delete pConn;
|
|
|
+ pConn = 0;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ __pAsyncDBConn = pConn;
|
|
|
+ }
|
|
|
+
|
|
|
+ void CDBConnPool::__DestroyAsyncThrdConn()
|
|
|
+ {
|
|
|
+ if ( __pAsyncDBConn )
|
|
|
+ {
|
|
|
+ __pAsyncDBConn->Close();
|
|
|
+ delete __pAsyncDBConn;
|
|
|
+ __pAsyncDBConn = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bool CDBConnPool::PushAsync( const std::string& strSQL )
|
|
|
+ {
|
|
|
+ _ASYNC_SQL_* pData = new _ASYNC_SQL_;
|
|
|
+ if ( !pData )
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ pData->SQL = strSQL;
|
|
|
+ return __AsyncQueue.push( pData );
|
|
|
+ }
|
|
|
+
|
|
|
+ bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes,std::string& Error )
|
|
|
+ {
|
|
|
+ CDBConnect *pConn = GetDBConnect( Error );
|
|
|
+ if ( 0 == pConn )
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ MYSQL_RES* pRes = pConn->Query( szSql, Error );
|
|
|
+ GiveBack( pConn );
|
|
|
+ return DBRes.Bind( pRes, Error );
|
|
|
+ }
|
|
|
+
|
|
|
+ MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error)
|
|
|
+ {
|
|
|
+ CDBConnect *pConn = GetDBConnect(Error);
|
|
|
+ if( 0 == pConn){
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ MYSQL_RES* pRes = pConn->Query(szSql,Error);
|
|
|
+ GiveBack(pConn);
|
|
|
+ return pRes;
|
|
|
+ }
|
|
|
+ my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
|
|
|
+ {
|
|
|
+ CDBConnect *pConn = GetDBConnect( Error );
|
|
|
+ if ( 0 == pConn )
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
|
|
|
+ GiveBack( pConn );
|
|
|
+ return nRet;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
|
|
|
+ {
|
|
|
+ CDBConnect *pConn = GetDBConnect( Error );
|
|
|
+ if ( 0 == pConn )
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
|
|
|
+
|
|
|
+ GiveBack( pConn );
|
|
|
+ return nRet;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|