Beispiel #1
0
        protected async override Task DumpToDBAsync(T[] data, TargetTable targetTable)
        {
            using (var bulkReader = new BulkDataReader <T>(m_typeAccessor, data))
            {
                using (var conn = new SqlConnection(targetTable.ConnectionString))
                {
                    await conn.OpenAsync().ConfigureAwait(false);

                    var transaction = conn.BeginTransaction();
                    try
                    {
                        using (var bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.TableLock, transaction))
                        {
                            foreach (SqlBulkCopyColumnMapping map in bulkReader.ColumnMappings)
                            {
                                bulkCopy.ColumnMappings.Add(map);
                            }

                            bulkCopy.DestinationTableName = targetTable.TableName;
                            bulkCopy.BulkCopyTimeout      = (int)TimeSpan.FromMinutes(30).TotalMilliseconds;
                            bulkCopy.BatchSize            = m_bulkSize;

                            // Write from the source to the destination.
                            await bulkCopy.WriteToServerAsync(bulkReader).ConfigureAwait(false);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is NullReferenceException)
                        {
                            m_logger.Error($"{this.FullName} NullReferenceException occurred in bulk insertion for type {typeof(T).Name}. This is probably caused by forgetting assigning value to a [NoNullCheck] attribute when constructing your object.");
                        }

                        m_logger.ErrorFormat("{0} Bulk insertion failed. Rolling back all changes...", this.FullName, e);
                        transaction.Rollback();
                        m_logger.InfoFormat("{0} Changes successfully rolled back", this.FullName);

                        //As this is an unrecoverable exception, rethrow it
                        throw;
                    }

                    transaction.Commit();
                    await this.OnPostBulkInsert(conn, targetTable, data).ConfigureAwait(false);
                }
            }
        }
Beispiel #2
0
        protected override async Task DumpToDBAsync(T[] data, TargetTable targetTable)
        {
            m_logger.Debug(h => h("{3} starts bulk-inserting {0} {1} to db table {2}", data.Length, typeof(T).Name, targetTable.TableName, this.FullName));

            using (var bulkReader = new BulkDataReader <T>(m_typeAccessor, data))
            {
                try
                {
                    using (var bulkCopy = new SqlBulkCopy(m_longConnection, SqlBulkCopyOptions.TableLock, m_transaction))
                    {
                        foreach (SqlBulkCopyColumnMapping map in bulkReader.ColumnMappings)
                        {
                            bulkCopy.ColumnMappings.Add(map);
                        }

                        bulkCopy.DestinationTableName = targetTable.TableName;
                        bulkCopy.BulkCopyTimeout      = (int)TimeSpan.FromMinutes(30).TotalMilliseconds;
                        bulkCopy.BatchSize            = m_bulkSize;

                        // Write from the source to the destination.
                        await bulkCopy.WriteToServerAsync(bulkReader).ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    if (e is NullReferenceException)
                    {
                        m_logger.ErrorFormat(
                            "{0} NullReferenceException occurred in bulk insertion. This is probably caused by forgetting assigning value to a [NoNullCheck] attribute when constructing your object.",
                            this.FullName);
                    }

                    m_logger.ErrorFormat("{0} Bulk insertion error in the first place", e, this.FullName);

                    //As this is an unrecoverable exception, rethrow it
                    throw new AggregateException(e);
                }

                await this.OnPostBulkInsert(m_longConnection, targetTable, data).ConfigureAwait(false);
            }

            m_logger.Info(h => h("{3} bulk-inserted {0} {1} to db table {2}", data.Length, typeof(T).Name, targetTable.TableName, this.FullName));
        }