Exemple #1
0
        private ObjectPoolWaitHandle CreateWaitHandle(IntPtr handle, bool fMutex)
        {
            ObjectPoolWaitHandle whandle = new ObjectPoolWaitHandle(fMutex);

            whandle.Handle = handle;
            return(whandle);
        }
Exemple #2
0
        public SqlInternalConnection GetConnection(out bool isInTransaction)
        {
            SqlInternalConnection con = null;

            isInTransaction = false;

            if (_state != State.Running)
            {
                return(null);
            }

            // Try to get from the context if we're context specific:
            con = TryGetResourceFromContext(out isInTransaction);

            if (null == con)
            {
                _poolCounter.Modify(cAddWait);
            }

            ObjectPoolWaitHandle[] localWaitHandles = _waitHandles;

            while (con == null)
            {
                int r = WaitHandle.WaitAny(localWaitHandles, _ctrl.CreationTimeout, false);
                // From the WaitAny docs: "If more than one object became signaled during
                // the call, this is the array index of the signaled object with the
                // smallest index value of all the signaled objects."  This is important
                // so that the free object signal will be returned before a creation
                // signal.

                if (r == WAIT_TIMEOUT)
                {
                    _poolCounter.Modify(-cAddWait);

                    return(null);
                }
                else if (r == ERROR_HANDLE)
                {
                    // Throw the error that PoolCreateRequest stashed.
                    _poolCounter.Modify(-cAddWait);
                    throw _resError;
                }
                else if (r == CREATION_HANDLE)
                {
                    try {
                        try {
                            con = UserCreateRequest();

                            if (null != con)
                            {
                                _poolCounter.Modify(-cAddWait);
                            }
                            else
                            {
                                // If we were not able to create a connection, check to see if
                                // we reached MaxPool.  If so, we will no longer wait on the
                                // CreationHandle, but instead wait for a free connection or
                                // the timeout.
                                // BUG - if we receive the CreationHandle midway into the wait
                                // period and re-wait, we will be waiting on the full period
                                if (_poolCounter.TotalCount == _ctrl.MaxPool)
                                {
                                    if (!CheckForDeadConnections())
                                    {
                                        // modify handle array not to wait on creation mutex anymore
                                        localWaitHandles    = new ObjectPoolWaitHandle[2];
                                        localWaitHandles[0] = _waitHandles[0];
                                        localWaitHandles[1] = _waitHandles[1];
                                    }
                                }
                            }
                        }
                        finally { // ReleaseMutex
                            _creationMutex.ReleaseMutex();
                        }
                    }
                    catch { // MDAC 80973
                        throw;
                    }
                }
                else
                {
                    //
                    //	guaranteed available inventory
                    //
                    _poolCounter.Modify(-cAddWait - cAddFree);
                    con = GetFromPool();
                }
            }

            Debug.Assert(con != null, "Failed to create pooled object, resulted in null instance.");

            return(con);
        }