I have a question about the code shown below.
try
{
using (TransactionScope outerTransScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// 1.Change values in certain columns from the SQL database table
using (TransactionScope innerTransScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// 2.Update a column from the SQL database table by using the changed values from the outerTransScope as a JOIN condition.
// 3.Commit the update query
innerTransScope.Complete();
}
}
// 4.Rollback the changed values that were executed in the outerTransScope
}
catch (Exception)
{
// Log the exception
}
Am I right to say that this isn't going to work as I expect? As the outerTransScope is actually rolling back the innerTransScope, because I never commit the outerTransScope.
This means you can only commit nested transactions if the root transaction is committed at the end. If the root transaction isn't committed then all the nested transactions are rolled back even if you commited the inner transactions.
Is it in any way possible to get the functionality shown in the code above? Thanks in advance.
EDIT: Let's say we got two tables, table A and table B.
Table A: Table B:
Column A | Column B Column A | Column B
Siemens | 100 SIE | null
Siemens | 101 SIE | null
Siemens | 102 SIE | null
Siemens | 103 SIE | null
I want to update Column B from Table B with Column B from Table A, however I only want to do this when Column A from Table A and Column A from Table B are a 100% match. Right now nothing will happen because Column A from Table A does not equals Column A from Table B.
So to tackle this problem the user has the ability to edit columns with something I call an actionscheme. This actionscheme will replace 'Siemens' with 'SIE' in table A. When this is replaced the Column A from Table A equals Column A from Table B. With this match I can now update Column B from Table B with the values from Column B from Table A. When this update is finished I want to commit those changes and Rollback the edits I made on Column A from Table A ('Siemens' changed into 'SIE' has to be rolled back).
In my code it looks something like this:
try
{
using (TransactionScope outerTransScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// 1.Change Siemens into SIE Column A from Table A
using (TransactionScope innerTransScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// 2.Update Column B from Table B with the values of Column B from Table B, but only if Column A from Table A equals Column A from Table B.
// 3.Commit the update of Column B from Table B
innerTransScope.Complete();
}
}
// 4.Rollback the changes in Column A from Table A (So SIE becomes Siemens again).
}
catch (Exception)
{
// Log the exception
}