Пример #1
0
        private DBPooledObject CreateObject()
        {
            DBPooledObject newObj = null;

            try {
                newObj = PoolControl.CreateObject(this);

                Debug.Assert(newObj != null, "CreateObject succeeded, but object null");

                newObj.PrePush(null);

                lock (_objectList.SyncRoot) {
                    _objectList.Add(newObj);
#if !USECOUNTEROBJECT
                    _totalObjects = _objectList.Count;
#endif //!USECOUNTEROBJECT
                }
#if USECOUNTEROBJECT
                _poolCounter.Modify(cAddTotal);
#endif //USECOUNTEROBJECT

#if ALLOWTRACING
                ADP.TraceObjectPoolActivity("CreateObject", newObj);
#endif //ALLOWTRACING

                // Reset the error wait:
                _errorWait = ERROR_WAIT_DEFAULT;
            }
            catch (Exception e)  {
                ADP.TraceException(e);

                newObj = null; // set to null, so we do not return bad new object
                // Failed to create instance
                _resError = e;
                SafeNativeMethods.SetEvent(_waitHandles[ERROR_HANDLE].Handle);
#if USECOUNTEROBJECT
                _poolCounter.Modify(cErrorFlag);
#else //!USECOUNTEROBJECT
                _errorOccurred = true;
#endif //!USECOUNTEROBJECT
                _errorTimer = new Timer(new TimerCallback(this.ErrorCallback), null, _errorWait, _errorWait);
                _errorWait *= 2;
            }

            return(newObj);
        }
Пример #2
0
        public void PutObject(DBPooledObject obj, object owningObject)
        {
            if (obj == null)
            {
                throw ADP.ArgumentNull("obj");
            }

            obj.PrePush(owningObject);

            if (_state != State.ShuttingDown)
            {
                bool isInTransaction = obj.Deactivate();

                if (obj.CanBePooled())
                {
#if USEORAMTS
                    ITransaction transaction = obj.ManualEnlistedTransaction;

                    if (null != transaction)
                    {
                        // When the object is put back into the pool while it is manually
                        // enlisted in a distributed transaction, we must create an outcome
                        // event and let the object wait until the distributed transaction
                        // has finished.  Once it does, the TransactionOutcomeEvents class
                        // can put it back into the general population of the pool.

                        UCOMIConnectionPoint point = (UCOMIConnectionPoint)transaction;

                        TransactionOutcomeEvents outcomeEvent = new TransactionOutcomeEvents(this, obj, point);

                        Int32 cookie = 0;
                        point.Advise(outcomeEvent, out cookie); // Register for callbacks, obtain cookie
                        outcomeEvent.SetCookie(cookie);         // Set the cookie on the event

#if ALLOWTRACING
                        ADP.TraceObjectPoolActivity("WaitForOutcomeEvnt", obj);
#endif //ALLOWTRACING
                        return;
                    }
#endif //USEORAMTS
                    // Try shoving it in the tx context first.  If that succeeds,
                    // we're done.
                    if (isInTransaction && TryPutResourceInContext(obj))
                    {
                        return;
                    }

                    // If the above failed, we just shove it into our current collection
                    PutNewObject(obj);
                }
                else
                {
                    DestroyObject(obj);
                    // Make sure we're at quota by posting a callback to the threadpool.
                    ThreadPool.QueueUserWorkItem(new WaitCallback(PoolCreateRequest));
                }
            }
            else
            {
                // If we're shutting down, we destroy the object.
                DestroyObject(obj);
            }
        }