Beispiel #1
0
        //
        // Summary:
        //     Enlists in the specified transaction. TODO
        //
        // Parameters:
        //   transaction:
        //     A reference to an existing System.Transactions.Transaction in which to enlist.
        //public override void EnlistTransaction(PqsqlTransaction transaction)
        //{
        //	throw new NotImplementedException();
        //}

        //
        // Summary:
        //     Opens a database connection with the settings specified by the System.Data.Common.DbConnection.ConnectionString.
        public override void Open()
        {
            if (mConnection != IntPtr.Zero && !mNewConnectionString)
            {
                // close and open with current connection setting
                PqsqlWrapper.PQreset(mConnection);

                // update connection and transaction status
                if (Status == ConnStatusType.CONNECTION_BAD || TransactionStatus != PGTransactionStatusType.PQTRANS_IDLE)
                {
                    string err = GetErrorMessage();
                    PqsqlWrapper.PQfinish(mConnection);                     // force release of mConnection memory
                    Init();
                    var connStr = mConnectionStringBuilder.GetConnectionStringWithObfuscatedPassword();
                    throw new PqsqlException("Could not reset connection with connection string «" + connStr + "»: " + err, (int)PqsqlState.CONNECTION_FAILURE);
                }

                OnStateChange(new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));

                // always set application_name, after a DISCARD ALL (usually issued by PqsqlConnectionPool / pgbouncer)
                // the session information is gone forever, and the shared connection will drop application_name
                SetApplicationName();

                // successfully reestablished connection
                return;
            }

            if (mStatus != ConnStatusType.CONNECTION_BAD)
            {
                Close();                 // force release of mConnection memory
            }

            // check connection pool for a connection
            mConnection = PqsqlConnectionPool.GetPGConn(mConnectionStringBuilder, out mStatus, out mTransStatus);

            if (mConnection == IntPtr.Zero)
            {
                throw new PqsqlException("libpq: unable to allocate struct PGconn");
            }

            // check connection and transaction status
            if (mStatus == ConnStatusType.CONNECTION_BAD || mTransStatus != PGTransactionStatusType.PQTRANS_IDLE)
            {
                string err = GetErrorMessage();
                PqsqlWrapper.PQfinish(mConnection);                 // force release of mConnection memory
                Init();
                var connStr = mConnectionStringBuilder.GetConnectionStringWithObfuscatedPassword();
                throw new PqsqlException("Could not create connection with connection string «" + connStr + "»: " + err);
            }

            mNewConnectionString = false;

            OnStateChange(new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));

            // always set application_name, after a DISCARD ALL (usually issued by PqsqlConnectionPool / pgbouncer)
            // the session information is gone forever, and the shared connection will drop application_name
            SetApplicationName();
        }
Beispiel #2
0
        //
        // Summary:
        //     Closes the connection to the database. This is the preferred method of closing
        //     any open connection.
        //
        // Exceptions:
        //   System.Data.Common.DbException:
        //     The connection-level error that occurred while opening the connection.
        public override void Close()
        {
            if (mConnection == IntPtr.Zero)
            {
                return;
            }

            // release connection to the pool for mConnectionStringBuilder
            PqsqlConnectionPool.ReleasePGConn(mConnectionStringBuilder, mConnection);

            Init();             // reset state, next Open() call might end up at a different server / db

            OnStateChange(new StateChangeEventArgs(ConnectionState.Open, ConnectionState.Closed));
        }