コード例 #1
0
ファイル: Connection.cs プロジェクト: weiplanet/openpetra
        /// <summary>
        /// Closes a DB connection. Also calls the <c>Dispose()</c> Method on the DB connection object.
        /// </summary>
        /// <remarks>
        /// Although the .NET FCL allows the <see cref="IDbConnection.Close" /> method to be
        /// called even on already closed connections without causing an error,
        /// for the purposes of cleaner application development this method throws
        /// an exception when the caller tries to close an already/still closed
        /// connection.
        /// </remarks>
        /// <param name="AConnection">Open Database connection</param>
        /// <param name="AConnectionName">Name of the DB Connection (can be an empty string).</param>
        /// <exception cref="EDBConnectionAlreadyClosedException">When trying to close an
        /// already/still closed DB connection.</exception>
        public static void CloseDBConnection(IDbConnection AConnection, string AConnectionName)
        {
            if (AConnection == null)
            {
                throw new ArgumentNullException("AConnection", "AConnection must not be null!");
            }

            if (AConnection.State != ConnectionState.Closed)
            {
                try
                {
                    AConnection.Close();
                    AConnection.Dispose();

                    TLogging.LogAtLevel(DBAccess.DB_DEBUGLEVEL_DETAILED_CONN_INFO,
                                        "    " +
                                        (TLogging.DL >= DBAccess.DB_DEBUGLEVEL_TRACE ? "CloseDBConnection:" : "") +
                                        "Database connection closed." + AConnectionName + " " +
                                        TDataBase.GetThreadAndAppDomainCallInfoForDBConnectionEstablishmentAndDisconnection());
                }
                catch (Exception exp)
                {
                    TLogging.Log(
                        (TLogging.DL >=
                         DBAccess.DB_DEBUGLEVEL_TRACE ? "CloseDBConnection:" : "") + "Error closing Database connection!" +
                        AConnectionName + " " + TDataBase.GetThreadAndAppDomainCallInfoForDBConnectionEstablishmentAndDisconnection() +
                        " Exception: " + exp.ToString());
                    throw;
                }
            }
            else
            {
                throw new EDBConnectionAlreadyClosedException();
            }
        }