Beispiel #1
0
        internal void PutConnection(IInnerConnection innerConnection)
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ConnectionPool.PutConnection ()");

            // Was connection in use when the pool was last cleared?
            // If so, don't return it to the pool.
            if (innerConnection.TimeStamp < lastCleared)
            {
                innerConnection.Close();
                return;
            }

            innerConnection.OuterConnectionWeakRef = null;
            if (CanPool(innerConnection))
            {
                if (innerConnection.DistributedTransaction != null &&
                    ContextUtil.IsInTransaction &&
                    ContextUtil.TransactionId == (Guid)innerConnection.DistributedTransactionId)
                {
                    DoDtcPool(innerConnection);
                }
                else
                {
                    DoPool(innerConnection);
                }
            }
            else
            {
                innerConnection.Close();
            }
        }
Beispiel #2
0
        private void ExpireConnections(object state)
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ConnectionPool.ExpireConnections ()");

            IInnerConnection connectionToClose = null;

            lock (this)
            {
                if (pool == null)
                {
                    return;
                }
                if (size > minSize)
                {
                    connectionToClose = pool[--size];
                }
            }
            if (connectionToClose != null)
            {
                Debug.WriteLineIf(Switch.Enabled, "Closing an expired connection.");
                try
                {
                    connectionToClose.Close();
                    connectionToClose.OuterConnectionWeakRef = null;
                }
                catch (Exception e)
                {
                    Debug.WriteLineIf(Switch.Enabled, "Error closing expired connections: " + e);
                    Trace.WriteLine("Error closing expired connections: " + e);
                }
            }
        }
Beispiel #3
0
        internal IInnerConnection GetConnection(ConnectionOptions options, VirtuosoConnection connection)
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ConnectionPool.GetConnection ()");

            IInnerConnection innerConnection = null;

            if (options.Enlist && ContextUtil.IsInTransaction)
            {
                innerConnection = (IInnerConnection)dtcPool.GetResource();
                if (innerConnection != null)
                {
                    innerConnection.OuterConnectionWeakRef = new WeakReference(connection);
                    return(innerConnection);
                }
            }

            lock (this)
            {
                if (pool == null)
                {
                    pool = new IInnerConnection[maxSize];
                    for (int i = 0; i < minSize; i++)
                    {
                        innerConnection           = connection.CreateInnerConnection(options, false);
                        innerConnection.TimeStamp = DateTime.Now;
                        PutConnection(innerConnection);
                    }
                }

                if (size > 0)
                {
                    innerConnection = pool[--size];
                }
            }

            if (innerConnection == null)
            {
                innerConnection           = connection.CreateInnerConnection(options, true);
                innerConnection.TimeStamp = DateTime.Now;
            }
            else
            {
                innerConnection.OuterConnectionWeakRef = new WeakReference(connection);
#if MTS
                if (options.Enlist && ContextUtil.IsInTransaction)
                {
                    connection.EnlistInnerConnection(innerConnection);
                }
#endif
            }

            return(innerConnection);
        }
Beispiel #4
0
        private void DistributedTransactionEnd(object resource)
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ConnectionPool.DistributedTransactionEnd ()");

            IInnerConnection innerConnection = (IInnerConnection)resource;

            innerConnection.OuterConnectionWeakRef = null;
            if (CanPool(innerConnection))
            {
                DoPool(innerConnection);
            }
            else
            {
                innerConnection.Close();
            }
        }
Beispiel #5
0
        private void DoDtcPool(IInnerConnection innerConnection)
        {
            bool rc;

            try
            {
                rc = dtcPool.PutResource(innerConnection);
            }
            catch
            {
                rc = false;
            }

            if (!rc)
            {
                DoPool(innerConnection);
            }
        }
Beispiel #6
0
        private bool CanPool(IInnerConnection innerConnection)
        {
            if (!innerConnection.IsValid())
            {
                return(false);
            }

            if (lifetime != 0)
            {
                DateTime endOfLifetime = innerConnection.TimeStamp + new TimeSpan(0, 0, lifetime);
                if (endOfLifetime < DateTime.Now)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #7
0
        private void DoPool(IInnerConnection innerConnection)
        {
            try
            {
                innerConnection.Pool();
            }
            catch
            {
                innerConnection.Close();
                throw;
            }

            lock (this)
            {
                if (size < maxSize)
                {
                    pool[size++] = innerConnection;
                    return;
                }
            }

            innerConnection.Close();
        }
 internal void EnlistInnerConnection (IInnerConnection conn)
 {
     try
     {
         object dt = System.EnterpriseServices.ContextUtil.Transaction;
         conn.Enlist (dt);
         conn.DistributedTransaction = dt;
         conn.DistributedTransactionId = System.EnterpriseServices.ContextUtil.TransactionId;
         autocommit = false;
     }
     catch
     {
         conn.Close ();
         throw;
     }
 }
        protected override void Dispose (bool disposing)
        {
	    try
	    {
            if (disposed)
                return;

            disposed = true;

            if (disposing)
            {
                Close ();
            }
            innerConnection = null;
            options = null;

            base.Dispose (disposing);
        }
	    catch (Exception e)
	    {
	        Debug.WriteLineIf(CLI.FnTrace.Enabled,
		    "VirtuosoConnection.Dispose caught exception: " + e.Message);
	    }
        }
        public void Open ()
#endif
        {
            /*
                         * Open the database connection and set the ConnectionState
                         * property. If the underlying connection to the server is 
                         * expensive to obtain, the implementation should provide
                         * implicit pooling of that connection.
                         * 
                         * If the provider also supports automatic enlistment in 
                         * distributed transactions, it should enlist during Open().
                         */
            Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoConnection.Open ()");

            if (state != ConnectionState.Closed)
                throw new InvalidOperationException ("The connection has already been open.");

            permission.Demand ();

            if (options.Pooling)
            {
                if (pool == null)
                    pool = ConnectionPool.GetPool (options);
                innerConnection = pool.GetConnection (options, this);
            }
            else
            {
                innerConnection = CreateInnerConnection (options, true);
            }
            state = ConnectionState.Open;
            options.Secure ();
            OnOpen ();
        }
        public void Close ()
#endif
        {
            /*
                         * Close the database connection and set the ConnectionState
                         * property. If the underlying connection to the server is
                         * being pooled, Close() will release it back to the pool.
                         */
            Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoConnection.Close ()");

            if (state != ConnectionState.Closed)
            {
                if (!autocommit && transactionStrongRef != null)
                {
                    VirtuosoTransaction transaction = (VirtuosoTransaction) transactionStrongRef;
                    if (transaction != null)
                        transaction.Dispose ();
                    else
                        EndTransaction (false);
                }

                if (pool != null)
                    pool.PutConnection (innerConnection);
                else
                    innerConnection.Close ();
                innerConnection = null;

                state = ConnectionState.Closed;
                OnClose ();
            }
        }
		private void DoDtcPool (IInnerConnection innerConnection)
		{
			bool rc;
			try
			{
				rc = dtcPool.PutResource (innerConnection);
			}
			catch
			{
				rc = false;
			}

			if (!rc)
			{
				DoPool (innerConnection);
			}
				
		}
		private void DoPool (IInnerConnection innerConnection)
		{
			try
			{
				innerConnection.Pool ();
			}
			catch
			{
				innerConnection.Close ();
				throw;
			}

			lock (this)
			{
				if (size < maxSize)
				{
					pool[size++] = innerConnection;
					return;
				}
			}

			innerConnection.Close ();
		}
		private bool CanPool (IInnerConnection innerConnection)
		{
			if (!innerConnection.IsValid ())
				return false;

			if (lifetime != 0)
			{
				DateTime endOfLifetime = innerConnection.TimeStamp + new TimeSpan (0, 0, lifetime);
				if (endOfLifetime < DateTime.Now)
					return false;
			}

			return true;
		}
		internal void PutConnection (IInnerConnection innerConnection)
		{
			Debug.WriteLineIf (CLI.FnTrace.Enabled, "ConnectionPool.PutConnection ()");

            // Was connection in use when the pool was last cleared?
            // If so, don't return it to the pool.
            if (innerConnection.TimeStamp < lastCleared)
            {
				innerConnection.Close ();
                return;
            }

			innerConnection.OuterConnectionWeakRef = null;
			if (CanPool (innerConnection))
			{
				if (innerConnection.DistributedTransaction != null
					&& ContextUtil.IsInTransaction
					&& ContextUtil.TransactionId == (Guid) innerConnection.DistributedTransactionId)
					DoDtcPool (innerConnection);
				else
					DoPool (innerConnection);
			}
			else
			{
				innerConnection.Close ();
			}
		}