Ejemplo n.º 1
0
        protected void Open(TcpClient tcpClient)
        {
            int    elapseConnectTime = 1;
            Random rand = new Random();

            _ServerBusy = false;

            while (elapseConnectTime < this.ConnectionTimeout * 1000)
            {
                tcpClient.Connect(this.ConnectionTimeout * 1000);

                try
                {
                    tcpClient.SendSyncMessage((short)ConnectEvent.Connect, BuildConnectionMessage());
                    break;
                }
                catch (Exception e)
                {
                    if (e.Message.Trim() == "Too many connects on server")
                    {
                        tcpClient.Close();
                        _Connected       = false;
                        ConnectionString = ConnectionString;

                        if (!CommandReportNoCache && TryConnectTimeout > 0)
                        {
                            if (elapseConnectTime >= TryConnectTimeout)
                            {
                                _ServerBusy = true;
                                return;
                            }
                        }

                        int sleepMillisecond = rand.Next(200, 800);
                        elapseConnectTime += sleepMillisecond;
                        System.Threading.Thread.Sleep(sleepMillisecond);

                        continue;
                    }

                    tcpClient.Close();

                    throw e;
                }
            }

            if (elapseConnectTime > this.ConnectionTimeout * 1000)
            {
                throw new System.Data.DataException("Connect timeout.The hubbledotnet server is too busy right now.");
            }

            _Connected = true;
            _State     = System.Data.ConnectionState.Open;
            DataCacheMgr.OnConnect(this);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }