Beginning with your final question, Suppress
, by itself, is not very useful:
using(TransactionScope ambiente = new TransactionScope())
{
using(TransactionScope suprimida = new TransactionScope(TransactionScopeOption.Suppress))
{
// erros aqui não cancelam a transação 'ambiente'
}
}
That is, a single suppressed transaction within another transaction will cause errors within the deleted transaction to not abort the transaction environment (a transaction is considered to be environment when it is the root, or first, of all transactions of a connection), which means that the transaction environment is completed successfully.
However, consider the following case:
using(TransactionScope ambiente = new TransactionScope())
{
using(TransactionScope requer = new TransactionScope(TransactionScopeOption.Required))
{
// erros aqui CANCELAM a transacção 'ambiente'
}
using(TransactionScope suprimida = new TransactionScope(TransactionScopeOption.Suppress))
{
// erros aqui NÃO cancelam a transacção 'ambiente'
}
}
In this case, consider having two separate pieces of code, one vital for your application (eg, saving a customer's data) and another non-vital piece of code (write a log for the database).
In the case shown, even if the log writing fails, the client data is safeguarded because the code responsible for saving them is inside a transaction associated with the ambiente
transaction and both will be completed at the end of block ambiente
.
In turn, the logs writing, even within the ambiente
block, is inside a block whose transaction was suppressed, which means that if it fails, errors will not cause a rollback of the ambiente
transaction.
Finally, the third option, RequireNew
indicates that even if there is an environment transaction, the code within your block must be completed and saved regardless of the transaction environment.
using(TransactionScope ambiente = new TransactionScope())
{
using(TransactionScope requer = new TransactionScope(TransactionScopeOption.Required))
{
// erros aqui CANCELAM a transacção 'ambiente'
}
using(TransactionScope requerNova = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// erros aqui NÃO CANCELAM a transacção 'ambiente'
// mas CANCELAM a transacção 'requerNova'
}
using(TransactionScope suprimida = new TransactionScope(TransactionScopeOption.Suppress))
{
// erros aqui NÃO cancelam a transacção 'ambiente'
}
}
(response based on in this article ) p>