Index: AdoNetAppender.cs =================================================================== --- AdoNetAppender.cs (revision 607768) +++ AdoNetAppender.cs (working copy) @@ -373,8 +373,8 @@ /// protected IDbConnection Connection { - get { return this.m_dbConnection; } - set { this.m_dbConnection = value; } + get { return m_dbConnection; } + set { m_dbConnection = value; } } #endregion // Protected Instance Properties @@ -428,32 +428,8 @@ override protected void OnClose() { base.OnClose(); - - // Close the cached command and connection objects - if (m_dbCommand != null) - { - try - { - m_dbCommand.Dispose(); - } - catch (Exception ex) - { - LogLog.Warn(declaringType, "Exception while disposing cached command object", ex); - } - m_dbCommand = null; - } - if (m_dbConnection != null) - { - try - { - m_dbConnection.Close(); - } - catch (Exception ex) - { - LogLog.Warn(declaringType, "Exception while disposing cached connection object", ex); - } - m_dbConnection = null; - } + DisposeCommand(false); + DiposeConnection(); } #endregion @@ -474,7 +450,7 @@ { if (m_reconnectOnError && (m_dbConnection == null || m_dbConnection.State != ConnectionState.Open)) { - LogLog.Debug(declaringType, "Attempting to reconnect to database. Current Connection State: " + ((m_dbConnection==null)?"":m_dbConnection.State.ToString()) ); + LogLog.Debug(declaringType, "Attempting to reconnect to database. Current Connection State: " + ((m_dbConnection==null)?SystemInfo.NullText:m_dbConnection.State.ToString()) ); InitializeDatabaseConnection(); InitializeDatabaseCommand(); @@ -637,65 +613,19 @@ } } - /// - /// Connects to the database. - /// - private void InitializeDatabaseConnection() - { - string connectionStringContext = "Unable to determine connection string context."; - string resolvedConnectionString = string.Empty; - - try - { - // Cleanup any existing command or connection - if (m_dbCommand != null) - { - try - { - m_dbCommand.Dispose(); - } - catch (Exception ex) - { - LogLog.Warn(declaringType, "Exception while disposing cached command object", ex); - } - m_dbCommand = null; - } - if (m_dbConnection != null) - { - try - { - m_dbConnection.Close(); - } - catch (Exception ex) - { - LogLog.Warn(declaringType, "Exception while disposing cached connection object", ex); - } - m_dbConnection = null; - } - - // Create the connection object - m_dbConnection = (IDbConnection)Activator.CreateInstance(ResolveConnectionType()); - - // Set the connection string - resolvedConnectionString = ResolveConnectionString(out connectionStringContext); - m_dbConnection.ConnectionString = resolvedConnectionString; - - using(SecurityContext.Impersonate(this)) - { - // Open the database connection - m_dbConnection.Open(); - } - } - catch (System.Exception e) - { - // Sadly, your connection string is bad. - ErrorHandler.Error("Could not open database connection [" + resolvedConnectionString + "]. Connection string context [" + connectionStringContext + "].", e); - - m_dbConnection = null; - } - } - /// + /// + /// + /// + /// + virtual protected IDbConnection CreateConnection(string connectionString) + { + IDbConnection connection = (IDbConnection)Activator.CreateInstance(ResolveConnectionType()); + connection.ConnectionString = connectionString; + return connection; + } + + /// /// Resolves the connection string from the ConnectionString, ConnectionStringName, or AppSettingsKey /// property. /// @@ -771,116 +701,158 @@ } } - /// - /// Prepares the database command and initialize the parameters. - /// - private void InitializeDatabaseCommand() - { - if (m_dbConnection != null && m_usePreparedCommand) - { - try - { - // Cleanup any existing command or connection - if (m_dbCommand != null) - { - try - { - m_dbCommand.Dispose(); - } - catch (Exception ex) - { - LogLog.Warn(declaringType, "Exception while disposing cached command object", ex); - } - m_dbCommand = null; - } + #endregion // Protected Instance Methods - // Create the command object - m_dbCommand = m_dbConnection.CreateCommand(); - - // Set the command string - m_dbCommand.CommandText = m_commandText; + #region Private Instance Methods - // Set the command type - m_dbCommand.CommandType = m_commandType; - } - catch(System.Exception e) - { - ErrorHandler.Error("Could not create database command ["+m_commandText+"]", e); + /// + /// Prepares the database command and initialize the parameters. + /// + private void InitializeDatabaseCommand() + { + if (m_dbConnection != null && m_usePreparedCommand) + { + try + { + DisposeCommand(false); - if (m_dbCommand != null) - { - try - { - m_dbCommand.Dispose(); - } - catch - { - // Ignore exception - } - m_dbCommand = null; - } - } + // Create the command object + m_dbCommand = m_dbConnection.CreateCommand(); - if (m_dbCommand != null) - { - try - { - foreach(AdoNetAppenderParameter param in m_parameters) - { - try - { - param.Prepare(m_dbCommand); - } - catch(System.Exception e) - { - ErrorHandler.Error("Could not add database command parameter ["+param.ParameterName+"]", e); - throw; - } - } - } - catch - { - try - { - m_dbCommand.Dispose(); - } - catch - { - // Ignore exception - } - m_dbCommand = null; - } - } + // Set the command string + m_dbCommand.CommandText = m_commandText; - if (m_dbCommand != null) - { - try - { - // Prepare the command statement. - m_dbCommand.Prepare(); - } - catch (System.Exception e) - { - ErrorHandler.Error("Could not prepare database command ["+m_commandText+"]", e); - try - { - m_dbCommand.Dispose(); - } - catch - { - // Ignore exception - } - m_dbCommand = null; - } - } - } - } + // Set the command type + m_dbCommand.CommandType = m_commandType; + } + catch (Exception e) + { + ErrorHandler.Error("Could not create database command [" + m_commandText + "]", e); - #endregion // Protected Instance Methods + DisposeCommand(true); + } - #region Protected Instance Fields + if (m_dbCommand != null) + { + try + { + foreach (AdoNetAppenderParameter param in m_parameters) + { + try + { + param.Prepare(m_dbCommand); + } + catch (Exception e) + { + ErrorHandler.Error("Could not add database command parameter [" + param.ParameterName + "]", e); + throw; + } + } + } + catch + { + DisposeCommand(true); + } + } - /// + if (m_dbCommand != null) + { + try + { + // Prepare the command statement. + m_dbCommand.Prepare(); + } + catch (Exception e) + { + ErrorHandler.Error("Could not prepare database command [" + m_commandText + "]", e); + + DisposeCommand(true); + } + } + } + } + + /// + /// Connects to the database. + /// + private void InitializeDatabaseConnection() + { + string connectionStringContext = "Unable to determine connection string context."; + string resolvedConnectionString = string.Empty; + + try + { + DisposeCommand(true); + DiposeConnection(); + + // Set the connection string + resolvedConnectionString = ResolveConnectionString(out connectionStringContext); + + m_dbConnection = CreateConnection(resolvedConnectionString); + + using (SecurityContext.Impersonate(this)) + { + // Open the database connection + m_dbConnection.Open(); + } + } + catch (Exception e) + { + // Sadly, your connection string is bad. + ErrorHandler.Error("Could not open database connection [" + resolvedConnectionString + "]. Connection string context [" + connectionStringContext + "].", e); + + m_dbConnection = null; + } + } + + /// + /// + /// + /// + private void DisposeCommand(bool ignoreException) + { + // Cleanup any existing command or connection + if (m_dbCommand != null) + { + try + { + m_dbCommand.Dispose(); + } + catch (Exception ex) + { + if (!ignoreException) + { + LogLog.Warn(declaringType, "Exception while disposing cached command object", ex); + } + } + m_dbCommand = null; + } + } + + /// + /// + /// + private void DiposeConnection() + { + if (m_dbConnection != null) + { + try + { + m_dbConnection.Close(); + } + catch (Exception ex) + { + LogLog.Warn(declaringType, "Exception while disposing cached connection object", ex); + } + m_dbConnection = null; + } + } + + #endregion // Private Instance Methods + + #region Protected Instance Fields + + /// /// Flag to indicate if we are using a command object /// ///