public void EndAsyncQuery() { HubbleAsyncConnection asyncConnection = _SqlConnection as HubbleAsyncConnection; if (asyncConnection != null) { asyncConnection.EndAsyncQuery(_ClassId); } }
internal ConnectionPool(HubbleAsyncConnection asyncConnection, string connectionString) { _ConnectionString = connectionString; //Get min pool size from connection string System.Data.SqlClient.SqlConnectionStringBuilder buider = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString); _MinPoolSize = buider.MinPoolSize; if (_MinPoolSize <= 0) { _MinPoolSize = 1; } InitPool(asyncConnection); }
private void InitPool(HubbleAsyncConnection asyncConnection) { lock (_LockObj) { for (int i = 0; i < _MinPoolSize; i++) { Hubble.Framework.Net.TcpClient tcpClient; asyncConnection.InitTcpClient(out tcpClient); TcpItem item = new TcpItem(); item.TcpClient = tcpClient; _TcpItemPool.AddLast(item); } } }
static internal ConnectionPool Get(HubbleAsyncConnection asyncConnection, string connectionString) { lock (_LockObj) { ConnectionPool pool; if (_TcpItemDict.TryGetValue(connectionString, out pool)) { return(pool); } else { pool = new ConnectionPool(asyncConnection, connectionString); _TcpItemDict.Add(connectionString, pool); return(pool); } } }
private Hubble.Framework.Data.DataSet InnerQuery(string orginalSql, int cacheTimeout) { if (!_SqlConnection.Connected) { if (_SqlConnection.ServerBusy) { DateTime expireTime; int hitCount; string tableTicks1; _QueryResult = DataCacheMgr.Get(_SqlConnection, orginalSql, out expireTime, out hitCount, out tableTicks1); if (_QueryResult != null) { return(_QueryResult.DataSet); } else { _SqlConnection.CommandReportNoCache = true; } } try { _SqlConnection.Open(); } catch (Exception e) { throw new System.Data.DataException(string.Format("Sql Connection does not connect!, errMsg:{0}", e.Message)); } } string tableTicks = ""; bool needCache = cacheTimeout >= 0; if (cacheTimeout >= 0) { DateTime expireTime; int hitCount; _QueryResult = DataCacheMgr.Get(_SqlConnection, orginalSql, out expireTime, out hitCount, out tableTicks); if (_QueryResult == null) { tableTicks = ""; } if (DateTime.Now < expireTime && _QueryResult != null) { //not expire return(_QueryResult.DataSet); } else { if (orginalSql.Contains(";")) { //if multi select and not unionselect, disable data cache if (orginalSql.Trim().IndexOf("[UnionSelect]", 0, StringComparison.CurrentCultureIgnoreCase) != 0) { needCache = false; } } } } string sql = ""; if (needCache) { if (ResetDataCacheAfterTimeout) { tableTicks = ""; } if (tableTicks == "") { sql = "exec SP_InnerDataCache;"; } else { sql = "exec SP_InnerDataCache '" + tableTicks.Replace("'", "''") + "';"; } } sql += orginalSql; if (_SqlConnection is HubbleAsyncConnection) { //Async query _AsyncException = null; HubbleAsyncConnection asyncConnection = _SqlConnection as HubbleAsyncConnection; QueryEvent = new System.Threading.ManualResetEvent(false); asyncConnection.BeginAsyncQuerySql(sql, this, ref _ClassId); if (QueryEvent.WaitOne(CommandTimeout * 1000, false)) { if (_AsyncException != null) { throw _AsyncException; } } else { this.EndAsyncQuery(); throw new System.TimeoutException("Query sql timeout"); } } else { _QueryResult = _SqlConnection.QuerySql(sql); } //Data cache if (cacheTimeout > 0 || needCache) { if (_QueryResult.DataSet.Tables.Count >= 0) { QueryResult qr = new QueryResult(); tableTicks = GetTableTicks(_QueryResult); DateTime expireTime; int hitCount; QueryResult qResult = null; int noChangedTables = 0; List <Hubble.Framework.Data.DataTable> sourceTables = new List <Hubble.Framework.Data.DataTable>(); for (int i = 0; i < _QueryResult.DataSet.Tables.Count; i++) { sourceTables.Add(_QueryResult.DataSet.Tables[i]); } for (int i = 0; i < sourceTables.Count; i++) { Hubble.Framework.Data.DataTable table; if (sourceTables[i].MinimumCapacity == int.MaxValue) { if (qResult == null) { qResult = DataCacheMgr.Get(_SqlConnection, orginalSql, out expireTime, out hitCount, out tableTicks); } if (qResult != null) { noChangedTables++; table = sourceTables[i]; MergeDataCache(qResult, table, qr); //table = qResult.DataSet.Tables[i]; //qResult.DataSet.Tables.Remove(table); //qr.AddDataTable(table); } else { table = sourceTables[i]; _QueryResult.DataSet.Tables.Remove(table); qr.AddDataTable(table); } } else { table = sourceTables[i]; _QueryResult.DataSet.Tables.Remove(table); qr.AddDataTable(table); } } _QueryResult.DataSet = qr.DataSet; if (noChangedTables != _QueryResult.DataSet.Tables.Count) { DataCacheMgr.Insert(_SqlConnection, orginalSql, _QueryResult, DateTime.Now.AddSeconds(cacheTimeout), tableTicks); } else { DataCacheMgr.ChangeExpireTime(_SqlConnection, orginalSql, DateTime.Now.AddSeconds(cacheTimeout)); } } } return(_QueryResult.DataSet); }
private void AsyncInnerQuery(string orginalSql, int cacheTimeout, bool priorMessage) { if (!(_SqlConnection is HubbleAsyncConnection)) { throw new Exception("Command must bind HubbleAsyncConnection when it call AsyncInnerQuery!"); } HubbleAsyncConnection asyncConnection = _SqlConnection as HubbleAsyncConnection; string tableTicks = ""; bool needCache = cacheTimeout >= 0; if (cacheTimeout >= 0) { DateTime expireTime; int hitCount; _QueryResult = DataCacheMgr.Get(_SqlConnection, orginalSql, out expireTime, out hitCount, out tableTicks); if (_QueryResult == null) { tableTicks = ""; } if (DateTime.Now < expireTime && _QueryResult != null) { //not expire return; } else { if (orginalSql.Contains(";")) { //if multi select and not unionselect, disable data cache if (orginalSql.Trim().IndexOf("[UnionSelect]", 0, StringComparison.CurrentCultureIgnoreCase) != 0) { needCache = false; } } } } string sql = ""; if (needCache) { if (ResetDataCacheAfterTimeout) { tableTicks = ""; } if (tableTicks == "") { sql = "exec SP_InnerDataCache;"; } else { sql = "exec SP_InnerDataCache '" + tableTicks.Replace("'", "''") + "';"; } } sql += orginalSql; asyncConnection.BeginAsyncQuerySql(sql, this, ref _ClassId, priorMessage); }