sql server - C# transaction with handled exception will still roll back? -
considering piece of code:
using(transactionscope tran = new transactionscope()) { insertstatementmethod1(); insertstatementmethod2(); // might fail try { insertstatementmethod3(); } catch (exception e) { // nothing } tran.complete(); }
is done in insertstatementmethod1
, insertstatementmethod2
going rolled back? in case? if want them execute anyway, need check if insertstatementmethod3
fail before transaction, , build transaction code based on that?
update
the code looks similar this
using(transactionscope tran = new transactionscope()) { // <standard code> yourextracode(); // <standard code> tran.complete(); }
where write yourextracode()
method
public void yourextracode() { insertstatementmethod1(); insertstatementmethod2(); // call might fail insertstatementmethod3(); }
i can edit yourextracode()
method, cannot chose in transaction scope or no. 1 simple possible solution this:
public void yourextracode() { insertstatementmethod1(); insertstatementmethod2(); // call might fail if (findoutificaninsert()) { // <-- come executing sql query try { insertstatementmethod3(); } catch (exception e) { // nothing } } }
but come need of looking things in db affect performance. there better way, or need find out before i'd call method? tried out and, of course transaction rolled expected.
if don't want first 2 methods transacted, move them out ambient transaction's scope.
if don't have control on code starts ambient transaction, can suppress creating new ambient transaction: using (var scope = new transactionscope(transactionscopeoption.suppress))
.
Comments
Post a Comment