You are analyzing a Windows client application that uses Microsoft Visual Studio 2010 and Microsoft SQL Server 2008.
The application updates two database tables from the main user interface (UI) thread. You need to ensure that the following requirements are met:
The database tables are either updated simultaneously or not updated at all. Users are notified of the success or failure of the updates. Users are able to perform other tasks during the update process.
What should you do?
A.
� Use TransactionScope in a using block on the UI thread.
� Batch the database updates by setting the DbDataAdapter.UpdateBatchSize property to 2.
B.
� Move the database update logic to a BackgroundWorker thread.
� Ensure that the thread is enclosed in a TransactionScopeusing block in the BackgroundWorker DoWork method.
C.
� Use TransactionScope in a using block on the main thread.
� Create a BackgroundWorker thread within the block.
� Move the database updates to the BackgroundWorker DoWork method.
D.
� Use TransactionScope in a using block on the UI thread.
� Create a DependentTransaction object within the block and pass the object to the BackgroundWorker ReportProgress method
� Use the object in the ReportProgress method to create a new TransactionScope block.
Explanation:
We need a background worker process => A out.
Users are able to perform other tasks during the update process. => Users are notified of the success or failure of the updates. => yes for B,C,D ( whether because the process is completed or because the process is cancelled, the RunWorkerCompleted event is raised ) The DependentTransaction is a clone of a Transaction object created using the DependentClone method.
Its sole purpose is to allow the application to come to rest and guarantee that the transaction cannot commit while work is still being performed on the transaction (for example, on a worker thread). => Users are able to perform other tasks during the update process => D out B,C => still left => DoWork event handler is used for a worker thread => B correct The code in the DoWork event handler is executed on a separate, dedicated thread, allowing the UI to remain responsive.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 1;i < 11; i++)
{
RunTimeConsumingProcess();
// Calls the Report Progress method, indicating the percentage // complete
backgroundWorker1.ReportProgress(i*10);
}
}
I still think option D is a better answer. There is an example on msdn as well: http://msdn.microsoft.com/en-us/library/system.transactions.dependenttransaction.aspx.