protected override bool ReleaseHandle()
        {
            IntPtr statementHandle = base.handle;

            base.handle = IntPtr.Zero;
            if (IntPtr.Zero != statementHandle)
            {
                ODBC32.SQL_HANDLE handleType = this.HandleType;
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                {
                    ODBC32.RetCode code = UnsafeNativeMethods.SQLFreeHandle(handleType, statementHandle);
                    Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", code);
                    break;
                }
                }
            }
            OdbcHandle handle = this._parentHandle;

            this._parentHandle = null;
            if (handle != null)
            {
                handle.DangerousRelease();
                handle = null;
            }
            return(true);
        }
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true) {

            _handleType   = handleType;

            bool mustRelease = false;
            ODBC32.RetCode retcode = ODBC32.RetCode.SUCCESS;

            // using ConstrainedRegions to make the native ODBC call and AddRef the parent
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                // validate handleType
                switch(handleType) {
                case ODBC32.SQL_HANDLE.ENV:
                    Debug.Assert(null == parentHandle, "did not expect a parent handle");
                    retcode = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out base.handle);
                    break;
                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    // must addref before calling native so it won't be released just after
                    Debug.Assert(null != parentHandle, "expected a parent handle"); // safehandle can't be null
                    parentHandle.DangerousAddRef(ref mustRelease);

                    retcode = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out base.handle);
                    break;
//              case ODBC32.SQL_HANDLE.DESC:
                default:
                    Debug.Assert(false, "unexpected handleType");
                    break;
                }
            }
            finally {
                if (mustRelease) {
                    switch(handleType) {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (IntPtr.Zero != base.handle) {
                            // must assign _parentHandle after a handle is actually created
                            // since ReleaseHandle will only call DangerousRelease if a handle exists
                            _parentHandle = parentHandle;
                        }
                        else {
                            // without a handle, ReleaseHandle may not be called
                            parentHandle.DangerousRelease();
                        }
                        break;
                    }
                }
            }
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", retcode);

            if((ADP.PtrZero == base.handle) || (ODBC32.RetCode.SUCCESS != retcode)) {
                // 
                throw ODBC.CantAllocateEnvironmentHandle(retcode);
            }
        }
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            this._handleType = handleType;
            bool success = false;

            ODBC32.RetCode sUCCESS = ODBC32.RetCode.SUCCESS;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out this.handle);
                    goto Label_0099;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    parentHandle.DangerousAddRef(ref success);
                    sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out this.handle);
                    goto Label_0099;
                }
            }
            finally
            {
                if (success)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (!(IntPtr.Zero != base.handle))
                        {
                            goto Label_0092;
                        }
                        this._parentHandle = parentHandle;
                        break;
                    }
                }
                goto Label_0098;
Label_0092:
                parentHandle.DangerousRelease();
                Label_0098 :;
            }
            Label_0099 :
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", sUCCESS);
            if ((ADP.PtrZero == base.handle) || (sUCCESS != ODBC32.RetCode.SUCCESS))
            {
                throw ODBC.CantAllocateEnvironmentHandle(sUCCESS);
            }
        }
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            this._handleType = handleType;
            bool success = false;
            ODBC32.RetCode sUCCESS = ODBC32.RetCode.SUCCESS;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                switch (handleType)
                {
                    case ODBC32.SQL_HANDLE.ENV:
                        sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, IntPtr.Zero, out this.handle);
                        goto Label_0099;

                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        parentHandle.DangerousAddRef(ref success);
                        sUCCESS = UnsafeNativeMethods.SQLAllocHandle(handleType, parentHandle, out this.handle);
                        goto Label_0099;
                }
            }
            finally
            {
                if (success)
                {
                    switch (handleType)
                    {
                        case ODBC32.SQL_HANDLE.DBC:
                        case ODBC32.SQL_HANDLE.STMT:
                            if (!(IntPtr.Zero != base.handle))
                            {
                                goto Label_0092;
                            }
                            this._parentHandle = parentHandle;
                            break;
                    }
                }
                goto Label_0098;
            Label_0092:
                parentHandle.DangerousRelease();
            Label_0098:;
            }
        Label_0099:
            Bid.TraceSqlReturn("<odbc.SQLAllocHandle|API|ODBC|RET> %08X{SQLRETURN}\n", sUCCESS);
            if ((ADP.PtrZero == base.handle) || (sUCCESS != ODBC32.RetCode.SUCCESS))
            {
                throw ODBC.CantAllocateEnvironmentHandle(sUCCESS);
            }
        }
Beispiel #5
0
        override protected bool ReleaseHandle()
        {
            // NOTE: The SafeHandle class guarantees this will be called exactly once and is non-interrutible.
            IntPtr handle = base.handle;

            base.handle = IntPtr.Zero;

            if (IntPtr.Zero != handle)
            {
                ODBC32.SQL_HANDLE handleType = HandleType;

                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.DBC:
                // Disconnect happens in OdbcConnectionHandle.ReleaseHandle
                case ODBC32.SQL_HANDLE.ENV:
                case ODBC32.SQL_HANDLE.STMT:
                    ODBC32.RetCode retcode = UnsafeNativeMethods.SQLFreeHandle(handleType, handle);
                    Bid.TraceSqlReturn("<odbc.SQLFreeHandle|API|ODBC|RET> %08X{SQLRETURN}\n", retcode);
                    break;

                case ODBC32.SQL_HANDLE.DESC:
                    // nothing to free on the handle
                    break;

                // case 0: ThreadAbortException setting handle before HandleType
                default:
                    Debug.Assert(ADP.PtrZero == handle, "unknown handle type");
                    break;
                }
            }

            // If we ended up getting released, then we have to release
            // our reference on our parent.
            OdbcHandle parentHandle = _parentHandle;

            _parentHandle = null;
            if (null != parentHandle)
            {
                parentHandle.DangerousRelease();
                parentHandle = null;
            }
            return(true);
        }
Beispiel #6
0
        protected OdbcHandle(ODBC32.SQL_HANDLE handleType, OdbcHandle parentHandle) : base(IntPtr.Zero, true)
        {
            _handleType = handleType;

            bool mustRelease = false;

            ODBC32.RetCode retcode = ODBC32.RetCode.SUCCESS;

            // using ConstrainedRegions to make the native ODBC call and AddRef the parent
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                // validate handleType
                switch (handleType)
                {
                case ODBC32.SQL_HANDLE.ENV:
                    Debug.Assert(null == parentHandle, "did not expect a parent handle");
                    retcode = Interop.Odbc.SQLAllocHandle(handleType, IntPtr.Zero, out base.handle);
                    break;

                case ODBC32.SQL_HANDLE.DBC:
                case ODBC32.SQL_HANDLE.STMT:
                    // must addref before calling native so it won't be released just after
                    Debug.Assert(null != parentHandle, "expected a parent handle");     // safehandle can't be null
                    parentHandle.DangerousAddRef(ref mustRelease);

                    retcode = Interop.Odbc.SQLAllocHandle(handleType, parentHandle, out base.handle);
                    break;

                //              case ODBC32.SQL_HANDLE.DESC:
                default:
                    Debug.Fail("unexpected handleType");
                    break;
                }
            }
            finally
            {
                if (mustRelease)
                {
                    switch (handleType)
                    {
                    case ODBC32.SQL_HANDLE.DBC:
                    case ODBC32.SQL_HANDLE.STMT:
                        if (IntPtr.Zero != base.handle)
                        {
                            // must assign _parentHandle after a handle is actually created
                            // since ReleaseHandle will only call DangerousRelease if a handle exists
                            _parentHandle = parentHandle;
                        }
                        else
                        {
                            // without a handle, ReleaseHandle may not be called
                            parentHandle.DangerousRelease();
                        }
                        break;
                    }
                }
            }

            if ((ADP.PtrZero == base.handle) || (ODBC32.RetCode.SUCCESS != retcode))
            {
                //
                throw ODBC.CantAllocateEnvironmentHandle(retcode);
            }
        }