/// <summary> /// Initializes a new instance of <see cref="DbBatchOperation"/>. /// </summary> /// <param name="commandTemplate">The <see cref="DbCommand"/> template on which the <see cref="DbBatchOperation"/> is based.</param> /// <param name="dialect">The <see cref="IDbDialect"/> associated with this <see cref="DbBatchOperation"/>.</param> /// <param name="batchSize">The maximum number of items to be written in a batch.</param> /// <param name="flushInterval">The frequency with which the current batch is to be flushed.</param> public DbBatchOperation(IDbDialect dialect, IDbCommand commandTemplate, Int32 batchSize, TimeSpan flushInterval) { this.autoFlush = true; this.dialect = dialect; this.batchSize = batchSize; this.flushInterval = flushInterval; this.commandTemplate = commandTemplate.Clone(); this.waitHandle = new ManualResetEvent(initialState: false); this.backgroundWorker = new Thread(WaitForData) { IsBackground = true }; this.buffer = CreateBuffer(commandTemplate); this.backgroundWorker.Start(); }
/// <summary> /// Write out the current batch to the underlying data store. /// </summary> private void WriteBufferToDataStore() { using (var batch = CopyAndClearBuffer()) using (var command = commandTemplate.Clone()) using (var connection = dialect.OpenConnection()) using (var dataAdapter = dialect.Provider.CreateDataAdapter()) using (var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)) { Debug.Assert(dataAdapter != null); command.Connection = connection; command.Transaction = transaction; command.UpdatedRowSource = UpdateRowSource.None; dataAdapter.InsertCommand = (DbCommand)command; dataAdapter.ContinueUpdateOnError = true; dataAdapter.UpdateBatchSize = batchSize; dataAdapter.Update(batch); transaction.Commit(); } }