Example #1
0
        /// <summary>
        /// Opens a database connection with the property settings specified by the
        /// <see cref="Npgsql.NpgsqlConnection.ConnectionString">ConnectionString</see>.
        /// </summary>
        public override void Open()
        {
            CheckConnectionClosed();

            // Check if there is any missing argument.
            if (!settings.ContainsKey(Keywords.Host))
            {
                throw new ArgumentException("Connection string argument missing!",
                                            NpgsqlConnectionStringBuilder.GetKeyName(Keywords.Host));
            }
            if (!settings.ContainsKey(Keywords.UserName) && !settings.ContainsKey(Keywords.IntegratedSecurity))
            {
                throw new ArgumentException("Connection string argument missing!",
                                            NpgsqlConnectionStringBuilder.GetKeyName(Keywords.UserName));
            }

            // Get a Connector, either from the pool or creating one ourselves.
            if (Pooling)
            {
                connector = NpgsqlConnectorPool.ConnectorPoolMgr.RequestConnector(this);
            }
            else
            {
                connector = new NpgsqlConnector(this);

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

                connector.Open();
            }

            connector.Notice       += NoticeDelegate;
            connector.Notification += NotificationDelegate;
            connector.StateChanged += connector_StateChanged;

            if (SyncNotification)
            {
                connector.AddNotificationThread();
            }

            if (Enlist)
            {
                Promotable.Enlist(Transaction.Current);
            }
            this.OnStateChange(new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));
        }