示例#1
0
 public virtual void Startup(NpgsqlConnector context, NpgsqlConnectionStringBuilder settings)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#2
0
 public void AttachConnector(NpgsqlConnector context)
 {
     mContext = context;
 }
示例#3
0
 /// <summary>
 /// Given command is executed upon Start() and all data from fromStream is passed to it as copy data.
 /// </summary>
 public NpgsqlCopyIn(NpgsqlCommand cmd, NpgsqlConnection conn, Stream fromStream)
 {
     _context    = conn.Connector;
     _cmd        = cmd;
     _copyStream = fromStream;
 }
示例#4
0
 private void CleanUpConnector(NpgsqlConnection Connection, NpgsqlConnector Connector)
 {
     new CleanUpConnectorDel(CleanUpConnectorMethod).BeginInvoke(Connection, Connector, null, null);
 }
示例#5
0
        /*
         *      /// <summary>
         *      /// Find an available shared connector in the shared pool, or create
         *      /// a new one if none found.
         *      /// </summary>
         *      private NpgsqlConnector GetSharedConnector(NpgsqlConnection Connection)
         *      {
         *          // To be implemented
         *
         *          return null;
         *      }
         */

        /// <summary>
        /// Put a pooled connector into the pool queue.
        /// </summary>
        /// <param name="Connection">Connection <paramref name="Connector"/> is leased to.</param>
        /// <param name="Connector">Connector to pool</param>
        private void UngetConnector(NpgsqlConnection Connection, NpgsqlConnector Connector)
        {
            ConnectorQueue queue;

            // Find the queue.
            // As we are handling all possible queues, we have to lock everything...
            lock (locker)
            {
                PooledConnectors.TryGetValue(Connection.ConnectionString, out queue);
            }

            if (queue == null)
            {
                Connector.Close(); // Release connection to postgres
                return;            // Queue may be emptied by connection problems. See ClearPool below.
            }

            /*bool inQueue = false;
             *
             * lock (queue)
             * {
             *  inQueue = queue.Busy.ContainsKey(Connector);
             *  queue.Busy.Remove(Connector);
             * }
             */

            if (!Connector.IsInitialized)
            {
                if (Connector.Transaction != null)
                {
                    Connector.Transaction.Cancel();
                }

                Connector.Close();
            }
            else
            {
                if (Connector.Transaction != null)
                {
                    try
                    {
                        Connector.Transaction.Rollback();
                    }
                    catch
                    {
                        Connector.Close();
                    }
                }
            }

            bool inQueue = queue.Busy.ContainsKey(Connector);

            if (Connector.State == ConnectionState.Open)
            {
                //If thread is good
                if ((Thread.CurrentThread.ThreadState & (ThreadState.Aborted | ThreadState.AbortRequested)) == 0)
                {
                    // Release all resources associated with this connector.
                    try
                    {
                        Connector.ReleaseResources();
                    }
                    catch (Exception)
                    {
                        //If the connector fails to release its resources then it is probably broken, so make sure we don't add it to the queue.
                        // Usually it already won't be in the queue as it would of broken earlier
                        inQueue = false;
                        Connector.Close();
                    }
                }
                else
                {
                    //Thread is being aborted, this connection is possibly broken. So kill it rather than returning it to the pool
                    inQueue = false;
                    Connector.Close();
                }
            }

            // Check if Connector should return to the queue of available connectors. If not, this connector is invalid and should
            // only be removed from the busy queue which effectvely removes it from the pool.
            if (inQueue)
            {
                lock (queue)
                {
                    queue.Busy.Remove(Connector);
                    queue.Available.Enqueue(Connector);
                }
            }
            else
            {
                lock (queue)
                {
                    queue.Busy.Remove(Connector);
                }
            }
        }
示例#6
0
 public virtual void FunctionCall(NpgsqlConnector context, NpgsqlCommand command)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
 /// <summary>
 /// Created only by NpgsqlCopyOutState.StartCopy()
 /// </summary>
 internal NpgsqlCopyOutStream(NpgsqlConnector context)
 {
     _context = context;
 }
示例#8
0
        // COPY methods

        protected virtual void StartCopy(NpgsqlConnector context, NpgsqlCopyFormat copyFormat)
        {
            throw new InvalidOperationException("Internal Error! " + this);
        }
示例#9
0
 public virtual byte[] GetCopyData(NpgsqlConnector context)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#10
0
 public virtual void Describe(NpgsqlConnector context, NpgsqlDescribe describe)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#11
0
 public virtual void CancelRequest(NpgsqlConnector context)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#12
0
 public virtual void Execute(NpgsqlConnector context, NpgsqlExecute execute)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#13
0
 public virtual void Bind(NpgsqlConnector context, NpgsqlBind bind)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#14
0
 public void TestConnector(NpgsqlConnector context)
 {
     EmptySync(context);
 }
示例#15
0
 public virtual void Authenticate(NpgsqlConnector context, byte[] password)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#16
0
 public virtual void SendCopyData(NpgsqlConnector context, byte[] buf, int off, int len)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#17
0
 public virtual void Query(NpgsqlConnector context, NpgsqlQuery query)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#18
0
 public virtual void SendCopyDone(NpgsqlConnector context)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#19
0
 public virtual void Parse(NpgsqlConnector context, NpgsqlParse parse)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#20
0
 public virtual void SendCopyFail(NpgsqlConnector context, String message)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#21
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="conn"></param>
 public NpgsqlCopySerializer(NpgsqlConnection conn)
 {
     _context = conn.Connector;
 }
示例#22
0
 ///<summary>
 ///This method is used by the states to change the state of the context.
 /// </summary>
 protected static void ChangeState(NpgsqlConnector context, NpgsqlState newState)
 {
     context.CurrentState = newState;
 }
示例#23
0
        /// <summary>
        /// Find an available pooled connector in the non-shared pool, or create
        /// a new one if none found.
        /// </summary>
        private NpgsqlConnector GetPooledConnector(NpgsqlConnection Connection)
        {
            ConnectorQueue  Queue     = null;
            NpgsqlConnector Connector = null;

            do
            {
                if (Connector != null)
                {
                    //This means Connector was found to be invalid at the end of the loop

                    lock (Queue)
                    {
                        Queue.Busy.Remove(Connector);
                    }

                    Connector.Close();
                    Connector = null;
                }

                // We only need to lock all pools when trying to get one pool or create one.

                lock (locker)
                {
                    // Try to find a queue.
                    if (!PooledConnectors.TryGetValue(Connection.ConnectionString, out Queue))
                    {
                        Queue = new ConnectorQueue();
                        Queue.ConnectionLifeTime = Connection.ConnectionLifeTime;
                        Queue.MinPoolSize        = Connection.MinPoolSize;
                        PooledConnectors[Connection.ConnectionString] = Queue;
                    }
                }

                // Now we can simply lock on the pool itself.
                lock (Queue)
                {
                    if (Queue.Available.Count > 0)
                    {
                        // Found a queue with connectors.  Grab the top one.

                        // Check if the connector is still valid.

                        Connector = Queue.Available.Dequeue();
                        Queue.Busy.Add(Connector, null);
                    }
                }
            } while (Connector != null && !Connector.IsValid());

            if (Connector != null)
            {
                return(Connector);
            }

            lock (Queue)
            {
                if (Queue.Available.Count + Queue.Busy.Count < Connection.MaxPoolSize)
                {
                    Connector = new NpgsqlConnector(Connection);
                    Queue.Busy.Add(Connector, null);
                }
            }

            if (Connector != null)
            {
                try
                {
                    Connector.Open();
                }
                catch
                {
                    lock (Queue)
                    {
                        Queue.Busy.Remove(Connector);
                    }

                    Connector.Close();

                    throw;
                }

                // Meet the MinPoolSize requirement if needed.
                if (Connection.MinPoolSize > 1)
                {
                    lock (Queue)
                    {
                        while (Queue.Available.Count + Queue.Busy.Count < Connection.MinPoolSize)
                        {
                            NpgsqlConnector Spare = new NpgsqlConnector(Connection);

                            Spare.Open();

                            Queue.Available.Enqueue(Spare);
                        }
                    }
                }
            }

            return(Connector);
        }
示例#24
0
 public ContextResetter(NpgsqlConnector connector)
 {
     _connector = connector;
 }
示例#25
0
 public override void Close(NpgsqlConnector context)
 {
     //DO NOTHING.
 }
示例#26
0
 public virtual void Open(NpgsqlConnector context, Int32 timeout)
 {
     throw new InvalidOperationException("Internal Error! " + this);
 }
示例#27
0
        public override void Open(NpgsqlConnector context, Int32 timeout)
        {
            try
            {
                NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Open");

                IAsyncResult result;
                // Keep track of time remaining; Even though there may be multiple timeout-able calls,
                // this allows us to still respect the caller's timeout expectation.
                DateTime attemptStart;

                attemptStart = DateTime.Now;

                result = Dns.BeginGetHostAddresses(context.Host, null, null);

                if (!result.AsyncWaitHandle.WaitOne(timeout, true))
                {
                    // Timeout was used up attempting the Dns lookup
                    throw new TimeoutException("Exception_DnsLookupTimeout");
                }

                timeout -= Convert.ToInt32((DateTime.Now - attemptStart).TotalMilliseconds);

                IPAddress[] ips    = Dns.EndGetHostAddresses(result);
                Socket      socket = null;
                Exception   lastSocketException = null;

                // try every ip address of the given hostname, use the first reachable one
                // make sure not to exceed the caller's timeout expectation by splitting the
                // time we have left between all the remaining ip's in the list.
                for (int i = 0; i < ips.Length; i++)
                {
                    NpgsqlEventLog.LogMsg("Log_ConnectingTo", LogLevel.Debug, ips[i]);

                    IPEndPoint ep = new IPEndPoint(ips[i], context.Port);
                    socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    attemptStart = DateTime.Now;

                    try
                    {
                        result = socket.BeginConnect(ep, null, null);

                        if (!result.AsyncWaitHandle.WaitOne(timeout / (ips.Length - i), true))
                        {
                            throw new TimeoutException("Exception_ConnectionTimeout");
                        }

                        socket.EndConnect(result);

                        // connect was successful, leave the loop
                        break;
                    }
                    catch (Exception e)
                    {
                        NpgsqlEventLog.LogMsg("Log_FailedConnection", LogLevel.Normal, ips[i]);

                        timeout            -= Convert.ToInt32((DateTime.Now - attemptStart).TotalMilliseconds);
                        lastSocketException = e;

                        socket.Close();
                        socket = null;
                    }
                }

                if (socket == null)
                {
                    throw lastSocketException;
                }

                NpgsqlNetworkStream baseStream = new NpgsqlNetworkStream(socket, true);
                Stream sslStream = null;

                // If the PostgreSQL server has SSL connectors enabled Open SslClientStream if (response == 'S') {
                if (context.SSL || (context.SslMode == SslMode.Require) || (context.SslMode == SslMode.Prefer))
                {
                    baseStream
                    .WriteInt32(8)
                    .WriteInt32(80877103);

                    throw new InvalidOperationException("Exception_Ssl_RequestError");
                }

                context.Socket     = socket;
                context.BaseStream = baseStream;
                context.Stream     = new BufferedStream(sslStream == null ? baseStream : sslStream, 8192);

                NpgsqlEventLog.LogMsg("Log_ConnectedTo", LogLevel.Normal, context.Host, context.Port);
                ChangeState(context, NpgsqlConnectedState.Instance);
            }
            catch (Exception e)
            {
                throw new NpgsqlException("Exception_FailedConnection", e);
            }
        }
 internal static void ExecuteBlindSuppressTimeout(NpgsqlConnector connector, string command)
 {
     // Bypass cpmmand parsing overhead and send command verbatim.
     ExecuteBlindSuppressTimeout(connector, new NpgsqlQuery(command));
 }