c# - automatic datacontext transaction management -
i assumed when using datacontext in following fashion automatic rollback:
update called submitchanges
twice question still applies.
public void updateuser(user user) { using (var context = new userdatacontext()) { //update stuff. context.submitchanges(); //update stuff. context.submitchanges(); } }
when goes wrong there no rollback.
instead, provide rollback i've implemented following:
public void updateuser(user user) { var context = new userdatacontext(); try { context.connection.open(); context.transaction = context.connection.begintransaction(); //update stuff. context.submitchanges(); context.transaction.commit(); } catch (exception e) { context.transaction.rollback(); throw; } { context.dispose(); } }
which alot more plumbing want. there better way indicate datacontext want automatic rollback?
if not inside external transaction (e.g. transactionscope
), submitchanges
starts transaction , should automatically rollback if exception occurs.
suggest post actual code causing problem , exception occurs.
if performing multiple submitchanges
, want them atomic, wrap in transactionscope
:
using (transactionscope ts = new transactionscope()) { blah1.submitchanges() blah2.submitchanges(); ts.commit(); }
Comments
Post a Comment