Ejemplo n.º 1
0
        public void ReleaseConnection(Tds connection)
        {
            if (connection == null)
            {
                return;
            }
            if (no_pooling)
            {
                connection.Disconnect();
                return;
            }

            if (connection.poolStatus == 2 || connection.Expired)
            {
                lock (conns)
                    conns.Remove(connection);
                connection.Disconnect();
                connection = null;
            }
            lock (available) {
                if (connection != null)                 // connection is still open
                {
                    available.Enqueue(connection);
                }
                // We pulse even if we don't queue, because null means that there's a slot
                // available in 'conns'
                Monitor.Pulse(available);
            }
        }
Ejemplo n.º 2
0
 public void Close()
 {
     if (Transaction != null && Transaction.IsOpen)
     {
         Transaction.Rollback();
     }
     if (pooling)
     {
         pool.ReleaseConnection(tds);
     }
     else
     {
         tds.Disconnect();
     }
     tds.TdsErrorMessage -= new TdsInternalErrorMessageEventHandler(ErrorHandler);
     tds.TdsInfoMessage  -= new TdsInternalInfoMessageEventHandler(MessageHandler);
     ChangeState(ConnectionState.Closed);
 }
Ejemplo n.º 3
0
        void Close()
        {
            if (transaction != null && transaction.IsOpen)
            {
                transaction.Rollback();
            }

            if (dataReader != null)
            {
                if (tds != null)
                {
                    tds.SkipToEnd();
                }
                dataReader = null;
            }

            if (tds != null && tds.IsConnected)
            {
                if (pooling && tds.Pooling)
                {
                    if (pool != null)
                    {
                        pool.ReleaseConnection(tds);
                    }
                }
                else
                if (tds != null)
                {
                    tds.Disconnect();
                }
            }

            if (tds != null)
            {
                tds.TdsErrorMessage -= new TdsInternalErrorMessageEventHandler(ErrorHandler);
                tds.TdsInfoMessage  -= new TdsInternalInfoMessageEventHandler(MessageHandler);
            }

            ChangeState(ConnectionState.Closed);
        }
Ejemplo n.º 4
0
        public Tds GetConnection()
        {
            if (no_pooling)
            {
                return(manager.CreateConnection(info));
            }

            Tds  result = null;
            bool create_new;
            int  retries = info.PoolMaxSize * 2;

retry:
            while (result == null)
            {
                create_new = false;
                lock (available) {
                    if (available.Count > 0)
                    {
                        result = (Tds)available.Dequeue();
                        break;                         // .. and do the reset out of the loop
                    }
                    Monitor.Enter(conns);
                    try {
                        if (conns.Count >= info.PoolMaxSize - in_progress)
                        {
                            Monitor.Exit(conns);
                            bool got_lock = Monitor.Wait(available, info.Timeout * 1000);
                            if (!got_lock)
                            {
                                throw new InvalidOperationException(
                                          "Timeout expired. The timeout period elapsed before a " +
                                          "connection could be obtained. A possible explanation " +
                                          "is that all the connections in the pool are in use, " +
                                          "and the maximum pool size is reached.");
                            }
                            else if (available.Count > 0)
                            {
                                result = (Tds)available.Dequeue();
                                break;                                 // .. and do the reset out of the loop
                            }
                            continue;
                        }
                        else
                        {
                            create_new = true;
                            in_progress++;
                        }
                    } finally {
                        Monitor.Exit(conns);                          // Exiting if not owned is ok < 2.x
                    }
                }
                if (create_new)
                {
                    try {
                        result = manager.CreateConnection(info);
                        lock (conns)
                            conns.Add(result);
                        return(result);
                    } finally {
                        lock (available)
                            in_progress--;
                    }
                }
            }

            bool      remove_cnc = true;
            Exception exc        = null;

            try {
                remove_cnc = (!result.IsConnected || !result.Reset());
            } catch (Exception e) {
                remove_cnc = true;
                exc        = e;
            }
            if (remove_cnc)
            {
                lock (conns)
                    conns.Remove(result);
                result.Disconnect();
                retries--;
                if (retries == 0)
                {
                    throw exc;
                }
                result = null;
                goto retry;
            }
            return(result);
        }
Ejemplo n.º 5
0
		public void ReleaseConnection (Tds connection)
		{
			if (connection == null)
				return;
			if (no_pooling) {
				connection.Disconnect ();
				return;
			}

			if (connection.poolStatus == 2) {
				lock (conns)
					conns.Remove (connection);
				connection.Disconnect ();
				connection = null;
			}
			lock (available) {
				if (connection != null) // connection is still open
 					available.Enqueue (connection);
				// We pulse even if we don't queue, because null means that there's a slot
				// available in 'conns'
 				Monitor.Pulse (available);
			}
		}