public SqlTransactionRepository(
     string dsn,
     ISpecificationFinder specFinder,
     string[] indexedSegments,
     Type identityType,
     string schema        = "dbo",
     string commonSchema  = "dbo",
     int segmentBatchSize = 1000,
     string sqlDateType   = "date")
     : base(dsn, identityType, schema)
 {
     _commonDb      = new DbCreation(dsn, commonSchema, identityType, sqlDateType);
     _transactionDb = new DbCreation(dsn, schema, identityType, sqlDateType);
     _idProvider    = GetIdProvider(dsn, commonSchema, identityType, segmentBatchSize / 10);
     _schemaEnsured = false;
     _batchSize     = segmentBatchSize;
     _segmentBatch  = new SegmentBatch(this, identityType);
     _specs         = new Dictionary <string, SegmentSpecification>();
     foreach (var segmentId in indexedSegments)
     {
         var spec = specFinder.FindSegmentSpec("5010", segmentId.Trim());
         _specs.Add(segmentId.Trim(), spec);
     }
 }
        internal virtual void ExecuteBatch(SqlTransaction tran)
        {
            if (_segmentBatch.LoopCount > 0)
            {
                try
                {
                    using (var conn = tran == null ? new SqlConnection(_dsn) : tran.Connection)
                    {
                        if (conn.State != ConnectionState.Open)
                        {
                            conn.Open();
                        }

                        using (var sbc = new SqlBulkCopy(conn))
                        {
                            sbc.DestinationTableName = string.Format("[{0}].[Container]", _commonDb.Schema);

                            var containerTable = new DataTable();
                            containerTable.Columns.Add("Id", _identityType);
                            containerTable.Columns.Add("SchemaName", typeof(string));
                            containerTable.Columns.Add("Type", typeof(string));

                            foreach (DataRow row in _segmentBatch._loopTable.Rows)
                            {
                                var containerId = _idProvider.NextId(_commonDb.Schema, "Container");
                                containerTable.Rows.Add(containerId, _schema, row["StartingSegmentId"]);
                            }

                            foreach (DataColumn c in containerTable.Columns)
                            {
                                sbc.ColumnMappings.Add(c.ColumnName, c.ColumnName);
                            }
                            sbc.WriteToServer(containerTable);
                        }

                        using (var sbc = new SqlBulkCopy(conn))
                        {
                            sbc.DestinationTableName = string.Format("[{0}].[Loop]", _schema);
                            foreach (DataColumn c in _segmentBatch._loopTable.Columns)
                            {
                                sbc.ColumnMappings.Add(c.ColumnName, c.ColumnName);
                            }
                            sbc.WriteToServer(_segmentBatch._loopTable);
                        }
                    }

                    _segmentBatch._loopTable.Clear();
                }
                catch (Exception exc)
                {
                    Trace.WriteLine(exc.Message);
                    Trace.TraceInformation(
                        "Error Saving {0} loops to db starting with {1}.",
                        _segmentBatch.LoopCount,
                        _segmentBatch.StartingSegment);

                    throw;
                }
            }

            if (_segmentBatch.SegmentCount > 0)
            {
                try
                {
                    using (var conn = tran == null ? new SqlConnection(_dsn) : tran.Connection)
                    {
                        if (conn.State != ConnectionState.Open)
                        {
                            conn.Open();
                        }
                        using (var sbc = new SqlBulkCopy(conn))
                        {
                            sbc.DestinationTableName = string.Format("[{0}].Segment", _schema);
                            foreach (DataColumn c in _segmentBatch._segmentTable.Columns)
                            {
                                sbc.ColumnMappings.Add(c.ColumnName, c.ColumnName);
                            }
                            sbc.WriteToServer(_segmentBatch._segmentTable);

                            foreach (var pair in _segmentBatch._parsedTables)
                            {
                                sbc.ColumnMappings.Clear();

                                sbc.DestinationTableName = string.Format("[{0}].[{1}]", _schema, pair.Key);
                                foreach (DataColumn c in pair.Value.Columns)
                                {
                                    sbc.ColumnMappings.Add(c.ColumnName, c.ColumnName);
                                }
                                sbc.WriteToServer(pair.Value);
                            }
                        }
                    }
                    _segmentBatch = new SegmentBatch(this, _identityType);
                }
                catch (Exception exc)
                {
                    Trace.WriteLine(exc.Message);
                    Trace.TraceInformation(
                        "Error Saving {0} segments to db starting with {1}.",
                        _segmentBatch.SegmentCount,
                        _segmentBatch.StartingSegment);

                    throw;
                }
            }
        }