Npgsql.NpgsqlConnector.IsValid C# (CSharp) Method

IsValid() private method

This method checks if the connector is still ok. We try to send a simple query text, select 1 as ConnectionTest;
private IsValid ( ) : Boolean
return Boolean
        internal Boolean IsValid()
        {
            try
            {
                // Here we use a fake NpgsqlCommand, just to send the test query string.

                // Get random test value.
                Byte[] testBytes = new Byte[2];
                rng.GetNonZeroBytes(testBytes);
                String testValue = String.Format("Npgsql{0}{1}", testBytes[0], testBytes[1]);

                //Query(new NpgsqlCommand("select 1 as ConnectionTest", this));
                string compareValue = string.Empty;
                string sql = "select '" + testValue + "'";
                // restore initial connection parameters resetted by "Discard ALL"
                if (SupportsDiscard)
                {
                    sql = this.initQueries + sql;
                }
                using(NpgsqlCommand cmd = new NpgsqlCommand(sql, this))
                {
                    compareValue = (string) cmd.ExecuteScalar();
                }

                if (compareValue != testValue)
                {
                    return false;
                }

                this.RequireReadyForQuery = true;
            }
            catch
            {
                return false;
            }

            return true;
        }

Usage Example

        /// <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;
            NpgsqlConnector Connector = null;

            // 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;
            }

            // Fix queue use count. Use count may be dropped below zero if Queue was cleared and there were connections open.
            if (Queue.UseCount < 0)
            {
                Queue.UseCount = 0;
            }


            if (Queue.Count > 0)
            {
                // Found a queue with connectors.  Grab the top one.

                // Check if the connector is still valid.

                Connector = Queue.Dequeue();

                /*try
                 * {
                 *      Connector.TestConnector();
                 *      Connector.RequireReadyForQuery = true;
                 * }
                 * catch //This connector is broken!
                 * {
                 *      try
                 *      {
                 *              Connector.Close();
                 *      }
                 *      catch
                 *      {
                 *              try
                 *              {
                 *                      Connector.Stream.Close();
                 *              }
                 *              catch
                 *              {
                 *              }
                 *      }
                 *      return GetPooledConnector(Connection); //Try again
                 * }*/

                if (!Connector.IsValid())
                {
                    try
                    {
                        Connector.Close();
                    }
                    catch
                    {
                        try
                        {
                            Connector.Stream.Close();
                        }
                        catch
                        {
                        }
                    }
                    return(GetPooledConnector(Connection));                            //Try again
                }
                Queue.UseCount++;
            }
            else if (Queue.Count + Queue.UseCount < Connection.MaxPoolSize)
            {
                Connector = CreateConnector(Connection);

                Connector.ProvideClientCertificatesCallback += Connection.ProvideClientCertificatesCallbackDelegate;
                Connector.CertificateSelectionCallback      += Connection.CertificateSelectionCallbackDelegate;
                Connector.CertificateValidationCallback     += Connection.CertificateValidationCallbackDelegate;
                Connector.PrivateKeySelectionCallback       += Connection.PrivateKeySelectionCallbackDelegate;

                try
                {
                    Connector.Open();
                }
                catch
                {
                    try
                    {
                        Connector.Close();
                    }
                    catch
                    {
                    }

                    throw;
                }


                Queue.UseCount++;
            }

            // Meet the MinPoolSize requirement if needed.
            if (Connection.MinPoolSize > 0)
            {
                while (Queue.Count + Queue.UseCount < Connection.MinPoolSize)
                {
                    NpgsqlConnector Spare = CreateConnector(Connection);

                    Spare.ProvideClientCertificatesCallback += Connection.ProvideClientCertificatesCallbackDelegate;
                    Spare.CertificateSelectionCallback      += Connection.CertificateSelectionCallbackDelegate;
                    Spare.CertificateValidationCallback     += Connection.CertificateValidationCallbackDelegate;
                    Spare.PrivateKeySelectionCallback       += Connection.PrivateKeySelectionCallbackDelegate;

                    Spare.Open();

                    Spare.ProvideClientCertificatesCallback -= Connection.ProvideClientCertificatesCallbackDelegate;
                    Spare.CertificateSelectionCallback      -= Connection.CertificateSelectionCallbackDelegate;
                    Spare.CertificateValidationCallback     -= Connection.CertificateValidationCallbackDelegate;
                    Spare.PrivateKeySelectionCallback       -= Connection.PrivateKeySelectionCallbackDelegate;

                    Queue.Enqueue(Spare);
                }
            }

            return(Connector);
        }
All Usage Examples Of Npgsql.NpgsqlConnector::IsValid