internal SqlBatchItemSaver( ITelemetryLogger logger, StackIdContainer stackIdContainer, string connectionString, string databaseName, string tableName, long aliveRowsCount, LastRowIdContainer lastRowIdContainer ) { if (logger == null) { throw new ArgumentNullException("logger"); } if (stackIdContainer == null) { throw new ArgumentNullException("stackIdContainer"); } if (connectionString == null) { throw new ArgumentNullException("connectionString"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } if (lastRowIdContainer == null) { throw new ArgumentNullException("lastRowIdContainer"); } if (aliveRowsCount <= 0) { throw new ArgumentException("aliveRowsCount <= 0"); } _logger = logger; _stackIdContainer = stackIdContainer; _databaseName = databaseName; _tableName = tableName; _aliveRowsCount = aliveRowsCount; _lastRowIdContainer = lastRowIdContainer; var connection = new SqlConnection(connectionString); connection.Open(); try { var transaction = connection.BeginTransaction(IsolationLevel.Snapshot); _connection = connection; _transaction = transaction; } catch { #region close connection if (this._connection != null) { try { this._connection.Close(); } catch (Exception excp) { _logger.LogHandledException(this.GetType(), "ќшибка закрыти¤ конекшена в конструкторе", excp); } } if (this._connection != null) { try { this._connection.Dispose(); } catch (Exception excp) { _logger.LogHandledException(this.GetType(), "ќшибка утилизации конекшена в конструкторе", excp); } } #endregion throw; } var insertStackClause = InsertStackClause.Replace( "{_TableName_}", _tableName ); _insertStackCommand = new System.Data.SqlClient.SqlCommand(insertStackClause, _connection, _transaction); _insertStackCommand.Parameters.Add("class_name", SqlDbType.VarChar, SqlHelper.ClassNameMaxLength); _insertStackCommand.Parameters.Add("method_name", SqlDbType.VarChar, SqlHelper.MethodNameMaxLength); _insertStackCommand.Parameters.Add("creation_stack", SqlDbType.VarChar, -1); _insertStackCommand.Parameters.Add("key", SqlDbType.VarChar, -1); _insertStackCommand.Prepare(); _targetTable = new DataTable(); _targetTable.Columns.Add("id", typeof(long)); _targetTable.Columns.Add("date_commit", typeof(DateTime)); _targetTable.Columns.Add("id_parent", typeof(long)); _targetTable.Columns.Add("start_time", typeof(DateTime)); _targetTable.Columns.Add("exception_message", typeof(string)); _targetTable.Columns.Add("exception_stack", typeof(string)); _targetTable.Columns.Add("time_interval", typeof(float)); _targetTable.Columns.Add("id_stack", typeof(int)); _targetTable.Columns.Add("exception_full_text", typeof(string)); //создаем копировщик _copier = new SqlBulkCopy( connection, SqlBulkCopyOptions.TableLock //| SqlBulkCopyOptions.FireTriggers //| SqlBulkCopyOptions.UseInternalTransaction , _transaction ); _copier.DestinationTableName = tableName; }
public SqlBatchItemSaverFactory( ITelemetryLogger logger, string connectionString, string databaseName, string tableName, long aliveRowsCount ) { if (logger == null) { throw new ArgumentNullException("logger"); } if (connectionString == null) { throw new ArgumentNullException("connectionString"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } if (aliveRowsCount <= 0) { throw new ArgumentException("aliveRowsCount <= 0"); } _logger = logger; _connectionString = connectionString; _databaseName = databaseName; _tableName = tableName; _aliveRowsCount = aliveRowsCount; try { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); long lastRowId; SqlHelper.DoPreparation( connection, null, _databaseName, _tableName, _logger, out lastRowId ); _stackIdContainer = SqlHelper.ReadStackTable( connection, null, _databaseName, _tableName, _logger ); _lastRowIdContainer = new LastRowIdContainer(lastRowId); } } catch(Exception excp) { _safeMode = true; logger.LogHandledException( this.GetType(), "Error database patching. Telemetry is going offline.", excp ); } }
public static StackIdContainer ReadStackTable( SqlConnection connection, SqlTransaction transaction, string databaseName, string tableName, ITelemetryLogger logger ) { if (connection == null) { throw new ArgumentNullException("connection"); } //transaction allowed to be null if (databaseName == null) { throw new ArgumentNullException("databaseName"); } if (tableName == null) { throw new ArgumentNullException("tableName"); } if (logger == null) { throw new ArgumentNullException("logger"); } var result = new StackIdContainer(); logger.LogMessage(typeof(SqlHelper), "PerformanceTelemetry Before ReadStackTable"); var readStackClause = ReadStackTableClause.Replace("{_DatabaseName_}", databaseName); readStackClause = readStackClause.Replace("{_TableName_}", tableName); using (var cmd = new System.Data.SqlClient.SqlCommand(readStackClause, connection, transaction)) { using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { var id = (int)reader["id"]; var key = (string)reader["key"]; result.ForceAdd(key, id); } } } logger.LogMessage(typeof(SqlHelper), "PerformanceTelemetry After ReadStackTable"); return result; }