示例#1
0
        /// <summary>
        /// Inserts a number of <see cref="DbExceptionEvent"/> entities into the database within a single transaction.
        /// </summary>
        /// <param name="connectionInfo">The database connection information.</param>
        /// <param name="dbExceptionEvents">A list of <see cref="DbExceptionEvent"/> entities to be inserted.</param>
        /// <param name="dbConfigFeedVersion">The <see cref="DbConfigFeedVersion"/> containing feed version information for the subject batch of records.</param>
        /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
        /// <param name="commandTimeout">The number of seconds before command execution timeout.</param>
        /// <returns></returns>
        public async Task <long> InsertAsync(ConnectionInfo connectionInfo, List <DbExceptionEvent> dbExceptionEvents, DbConfigFeedVersion dbConfigFeedVersion, CancellationTokenSource cancellationTokenSource, int commandTimeout)
        {
            CancellationToken cancellationToken = cancellationTokenSource.Token;
            long insertedRowsCount = 0;

            try
            {
                using (var connection = await new ConnectionProvider(connectionInfo).GetOpenConnectionAsync())
                {
                    using (var transaction = await connection.BeginTransactionAsync())
                    {
                        foreach (var dbFaultData in dbExceptionEvents)
                        {
                            await InsertAsync(connection, transaction, dbFaultData, commandTimeout);

                            insertedRowsCount += 1;
                            cancellationToken.ThrowIfCancellationRequested();
                        }

                        // Update DbConfigFeedVersion.
                        await new DbConfigFeedVersionRepository().UpdateAsync(connection, transaction, dbConfigFeedVersion, commandTimeout);

                        cancellationToken.ThrowIfCancellationRequested();
                        await transaction.CommitAsync();
                    }
                    return(insertedRowsCount);
                }
            }
            catch (Exception exception)
            {
                throw new DatabaseConnectionException($"Exception encountered while attempting database operation.", exception);
            }
        }
示例#2
0
 /// <summary>
 /// Inserts a number of <see cref="DbExceptionEvent"/> entities into the database within a single transaction.
 /// </summary>
 /// <param name="connectionInfo">The database connection information.</param>
 /// <param name="dbExceptionEvents">A list of <see cref="DbExceptionEvent"/> entities to be inserted.</param>
 /// <param name="dbConfigFeedVersion">The <see cref="DbConfigFeedVersion"/> containing feed version information for the subject batch of records.</param>
 /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
 /// <param name="commandTimeout">The number of seconds before command execution timeout.</param>
 /// <returns></returns>
 public static async Task <long> InsertAsync(ConnectionInfo connectionInfo, List <DbExceptionEvent> dbExceptionEvents, DbConfigFeedVersion dbConfigFeedVersion, CancellationTokenSource cancellationTokenSource, int commandTimeout)
 {
     return(await new DbExceptionEventRepository().InsertAsync(connectionInfo, dbExceptionEvents, dbConfigFeedVersion, cancellationTokenSource, commandTimeout));
 }
 /// <summary>
 /// Inserts/Updates a number of <see cref="DbDVIRLog"/>, <see cref="DbDVIRDefect"/> and <see cref="DbDVIRDefectRemark"/> entities into the database within a single transaction. Returns a string containing comma-separated counts: "[DbDVIRLog entities inserted],[DbDVIRDefect entities inserted],[DbDVIRDefect entities updated],[DbDVIRDefectRemark entities inserted]"
 /// </summary>
 /// <param name="connectionInfo">The database connection information.</param>
 /// <param name="dbDVIRLogsToInsert">A list of <see cref="DbDVIRLog"/> entities to be inserted.</param>
 /// <param name="dbDVIRDefectsToInsert">A list of <see cref="DbDVIRDefect"/> entities to be inserted.</param>
 /// <param name="dbDVIRDefectsToUpdate">A list of <see cref="DbDVIRDefect"/> entities to be updated.</param>
 /// <param name="dbDVIRDefectRemarksToInsert">A list of <see cref="DbDVIRDefectRemark"/> entities to be inserted.</param>
 /// <param name="dbConfigFeedVersion">The <see cref="DbConfigFeedVersion"/> containing feed version information for the subject batch of records.</param>
 /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
 /// <param name="commandTimeout">The number of seconds before command execution timeout.</param>
 /// <returns></returns>
 public static async Task <string> PersistAllDVIRLogChangesToDatabase(ConnectionInfo connectionInfo, List <DbDVIRLog> dbDVIRLogsToInsert, List <DbDVIRDefect> dbDVIRDefectsToInsert, List <DbDVIRDefect> dbDVIRDefectsToUpdate, List <DbDVIRDefectRemark> dbDVIRDefectRemarksToInsert, DbConfigFeedVersion dbConfigFeedVersion, CancellationTokenSource cancellationTokenSource, int commandTimeout)
 {
     return(await new DbDVIRLogRepository().PersistAllDVIRLogChangesToDatabase(connectionInfo, dbDVIRLogsToInsert, dbDVIRDefectsToInsert, dbDVIRDefectsToUpdate, dbDVIRDefectRemarksToInsert, dbConfigFeedVersion, cancellationTokenSource, commandTimeout));
 }
示例#4
0
        /// <summary>
        /// Creates a new instance of the <see cref="FeedManager"/> class.
        /// </summary>
        /// <param name="dbConfigFeedVersions">The list of <see cref="DbConfigFeedVersion"/> objects conatining the latest ToVersion for each of the supported data feeds.</param>
        /// <returns></returns>
        public static FeedManager Create(List <DbConfigFeedVersion> dbConfigFeedVersions)
        {
            MethodBase methodBase = MethodBase.GetCurrentMethod();

            logger.Trace($"Begin {methodBase.ReflectedType.Name}.{methodBase.Name}");

            var feedManager = new FeedManager();

            // If the FeedStartOption is not set to FeedVersion and the ConfigFeedVersions table has data for any of the feeds, switch the FeedStartOption to FeedVersion to avoid sending duplicate records to the database.
            var currentFeedStartOption = Globals.ConfigurationManager.FeedStartOption;

            if (currentFeedStartOption != Globals.FeedStartOption.FeedVersion && dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.LastProcessedFeedVersion > 0).Any())
            {
                logger.Info($"Switching FeedStartOption from '{currentFeedStartOption}' to '{Globals.FeedStartOption.FeedVersion}' to prevent data duplication because the '{Globals.ConfigurationManager.DbConfigFeedVersionsTableName}' table contains data for one or more feeds.");
                Globals.ConfigurationManager.FeedStartOption = Globals.FeedStartOption.FeedVersion;
            }

            // Setup a data feed for LogRecords.
            DbConfigFeedVersion logRecordDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.LogRecord.ToString()).First();

            SetupFeedContainer <LogRecord>(logRecordFeedContainer, false, Globals.ConfigurationManager.EnableLogRecordFeed,
                                           Globals.ConfigurationManager.LogRecordFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                           Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, logRecordDbConfigFeedVersion.LastProcessedFeedVersion);

            // Setup a data feed for StatusData.
            DbConfigFeedVersion statusDataDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.StatusData.ToString()).First();

            SetupFeedContainer <StatusData>(statusDataFeedContainer, false, Globals.ConfigurationManager.EnableStatusDataFeed,
                                            Globals.ConfigurationManager.StatusDataFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                            Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, statusDataDbConfigFeedVersion.LastProcessedFeedVersion);

            // Setup a data feed for FaultData.
            DbConfigFeedVersion faultDataDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.FaultData.ToString()).First();

            SetupFeedContainer <FaultData>(faultDataFeedContainer, false, Globals.ConfigurationManager.EnableFaultDataFeed,
                                           Globals.ConfigurationManager.FaultDataFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                           Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, faultDataDbConfigFeedVersion.LastProcessedFeedVersion);

            // Setup a data feed for DVIRLogs.
            DbConfigFeedVersion dvirLogDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.DVIRLog.ToString()).First();

            SetupFeedContainer <DVIRLog>(dvirLogFeedContainer, false, Globals.ConfigurationManager.EnableDVIRLogDataFeed,
                                         Globals.ConfigurationManager.DVIRLogDataFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                         Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, dvirLogDbConfigFeedVersion.LastProcessedFeedVersion);

            // Setup a data feed for Trips.
            DbConfigFeedVersion tripDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.Trip.ToString()).First();

            SetupFeedContainer <Trip>(tripFeedContainer, false, Globals.ConfigurationManager.EnableTripFeed,
                                      Globals.ConfigurationManager.TripFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                      Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, tripDbConfigFeedVersion.LastProcessedFeedVersion);

            // Setup a data feed for ExceptionEvents.
            DbConfigFeedVersion exceptionEventDbConfigFeedVersion = dbConfigFeedVersions.Where(dbConfigFeedVersion => dbConfigFeedVersion.FeedTypeId == Globals.SupportedFeedTypes.ExceptionEvent.ToString()).First();

            SetupFeedContainer <ExceptionEvent>(exceptionEventFeedContainer, false, Globals.ConfigurationManager.EnableExceptionEventFeed,
                                                Globals.ConfigurationManager.ExceptionEventFeedIntervalSeconds, Globals.ConfigurationManager.FeedStartOption,
                                                Globals.ConfigurationManager.FeedStartSpecificTimeUTC, Globals.GetFeedResultLimitDefault, exceptionEventDbConfigFeedVersion.LastProcessedFeedVersion);

            logger.Trace($"End {methodBase.ReflectedType.Name}.{methodBase.Name}");
            return(feedManager);
        }
示例#5
0
 /// <summary>
 /// Updates a <see cref="DbConfigFeedVersion"/> entity in the database.
 /// </summary>
 /// <param name="connectionInfo">The database connection information.</param>
 /// <param name="dbConfigFeedVersion">The <see cref="DbConfigFeedVersion"/> entity to be updated.</param>
 /// <param name="commandTimeout">The number of seconds before command execution timeout.</param>
 /// <returns></returns>
 public static async Task <bool> UpdateAsync(ConnectionInfo connectionInfo, DbConfigFeedVersion dbConfigFeedVersion, int commandTimeout)
 {
     return(await new DbConfigFeedVersionRepository().UpdateAsync(connectionInfo, dbConfigFeedVersion, commandTimeout));
 }
示例#6
0
        /// <summary>
        /// Inserts/Updates a number of <see cref="DbDVIRLog"/>, <see cref="DbDVIRDefect"/> and <see cref="DbDVIRDefectRemark"/> entities into the database within a single transaction. Returns a string containing comma-separated counts: "[DbDVIRLog entities inserted],[DbDVIRDefect entities inserted],[DbDVIRDefect entities updated],[DbDVIRDefectRemark entities inserted]"
        /// </summary>
        /// <param name="connectionInfo">The database connection information.</param>
        /// <param name="dbDVIRLogsToInsert">A list of <see cref="DbDVIRLog"/> entities to be inserted.</param>
        /// <param name="dbDVIRDefectsToInsert">A list of <see cref="DbDVIRDefect"/> entities to be inserted.</param>
        /// <param name="dbDVIRDefectsToUpdate">A list of <see cref="DbDVIRDefect"/> entities to be updated.</param>
        /// <param name="dbDVIRDefectRemarksToInsert">A list of <see cref="DbDVIRDefectRemark"/> entities to be inserted.</param>
        /// <param name="dbConfigFeedVersion">The <see cref="DbConfigFeedVersion"/> containing feed version information for the subject batch of records.</param>
        /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
        /// <param name="commandTimeout">The number of seconds before command execution timeout.</param>
        /// <returns></returns>
        public async Task <string> PersistAllDVIRLogChangesToDatabase(ConnectionInfo connectionInfo, List <DbDVIRLog> dbDVIRLogsToInsert, List <DbDVIRDefect> dbDVIRDefectsToInsert, List <DbDVIRDefect> dbDVIRDefectsToUpdate, List <DbDVIRDefectRemark> dbDVIRDefectRemarksToInsert, DbConfigFeedVersion dbConfigFeedVersion, CancellationTokenSource cancellationTokenSource, int commandTimeout)
        {
            CancellationToken cancellationToken = cancellationTokenSource.Token;
            string            rowCounts         = "";
            long rowCount;

            try
            {
                using (var connection = await new ConnectionProvider(connectionInfo).GetOpenConnectionAsync())
                {
                    using (var transaction = await connection.BeginTransactionAsync())
                    {
                        // Insert DbDVIRLogs.
                        rowCount = 0;
                        foreach (var dbDVIRLog in dbDVIRLogsToInsert)
                        {
                            await InsertAsync(connection, transaction, dbDVIRLog, commandTimeout);

                            rowCount += 1;
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        rowCounts = rowCount.ToString();

                        // Insert DbDVIRDefects.
                        rowCount = 0;
                        foreach (var dbDVIRDefect in dbDVIRDefectsToInsert)
                        {
                            await new DbDVIRDefectRepository().InsertAsync(connection, transaction, dbDVIRDefect, commandTimeout);
                            rowCount += 1;
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        rowCounts = $"{rowCounts},{rowCount}";

                        // Update DbDVIRDefects.
                        rowCount = 0;
                        foreach (var dbDVIRDefect in dbDVIRDefectsToUpdate)
                        {
                            await new DbDVIRDefectRepository().UpdateAsync(connection, transaction, dbDVIRDefect, commandTimeout);
                            rowCount += 1;
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        rowCounts = $"{rowCounts},{rowCount}";

                        // Insert DbDVIRDefectRemarks.
                        rowCount = 0;
                        foreach (var dbDVIRDefectRemark in dbDVIRDefectRemarksToInsert)
                        {
                            await new DbDVIRDefectRemarkRepository().InsertAsync(connection, transaction, dbDVIRDefectRemark, commandTimeout);
                            rowCount += 1;
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        rowCounts = $"{rowCounts},{rowCount}";

                        // Update DbConfigFeedVersion.
                        await new DbConfigFeedVersionRepository().UpdateAsync(connection, transaction, dbConfigFeedVersion, commandTimeout);

                        cancellationToken.ThrowIfCancellationRequested();
                        await transaction.CommitAsync();
                    }
                    return(rowCounts);
                }
            }
            catch (Exception exception)
            {
                throw new DatabaseConnectionException($"Exception encountered while attempting database operation.", exception);
            }
        }