Starting with your final question, Suppress
, on its own, not much use:
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 transaction deleted within another transaction will cause errors within the deleted transaction not to abort the ambient transaction (a transaction is considered to be environment when it is the root, or the first, of all transactions on a connection)which means that the ambient transaction is successfully completed.
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 that you have two distinct pieces of code, one vital to your application (e. g, save data from a client) and another piece of non-vital code (write a log to the database).
in the demonstrated case, even if the written log failed, customer data is backed up because the code responsible for saving it is inside a transaction associated with the transaction ambiente
and both will be completed at the end of the block ambiente
.
In turn, the writing of logs, though inside the block ambiente
, is within a block whose transaction has been deleted, which means that if it fails, the errors will not cause the transaction to go back ambiente
.
Finally, the third option, RequireNew
indicates that even if there is an ambient transaction, the code within its block must be completed and stored independently of the ambient transaction.
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 in this article)
Se você quer ProcessRequest para utilizar a operação criado pela SomeOtherMethod, use TransactionScope.Required
andSe você quer que ele para forçar esse método para usar sempre sua própria transação (novo)..
That explanation got confused. @taisbevalle– rubStackOverflow
@rubStackOverflow, improved.
– Taisbevalle