Re: How To Tell You're Within An Exception?



Hello Axel!

I've created a Transaction object which is supposed to be used like
this:

using (Transaction transaction = new Transaction())
{
...
}

In the Dispose() method of the Transaction object I want to call
Rollback() if the current process is currently unwinding an exception
stack. If the current process is just reaching the closing brace in
normal program flow, I want to call Commit() within the Dispose()
method.

I already thought that you plan to do something like this. I've also
developed a very similar thing. My solution was to call a method right
before the using(){} block is finished:

(pseudo-code)

using(Transaction t = new Transaction()){
// perform commands
t.SetComplete(); // executing commands after .SetComplete() is not
supported => NotSupportedException
}

Transaction.Dispose() {
if(Complete){
Commit();
} else {
Rollback();
}
}

It is very straight forward, and the developer can control this process.
Allthough in most cases you may rollback on error and commit on success,
this is not always the case. By using the "Exception dedection" Rollback can
only be triggered by an Exception.

There may be possiblities to tell if you're in a Exception by reading the
right CLR's memory ranges. But this may be just something for debugging
purposes, doing this every time you complete an Exception would be an hugh
overhead, and it's totally unsupported.

Maybe you can also take a look a System.Transactions which implements the
whole TX-Framework (it works very similar like your posted Code
(TransactionScope Class)).

e.g.:

using(TransactionScope txScope = new TranscationScope()){
// create sQL Connection, execute commands or whatever
// if a resource is implemented by System.Transactions it will
participate in the TX

txScope.Complete();
}


If needed (when you speak about not ADO.Net Transaction but you have an own
Resource-Type) you may integrate into System.Transactions this is possible
by implementing an Resource-Manager.


OK?
GP


.



Relevant Pages

  • Re: Another question regarding exceptions and loops
    ... In this case I define a transaction as saving a single Contact not the List ... > I think that having separate exceptions for when the list is partially ... > rollback for this kind of thing? ... if there is an exception that is thrown (and I hope you are ...
    (microsoft.public.dotnet.languages.csharp)
  • BEGIN TRANSACTION problem
    ... there was no BEGIN TRANSACTION. ... The test prior to the exception is the first to execute the parent ... The is the exception the unit test is expecting. ... Rollback statement faile with "The ROLLBACK TRANSACTION request has no ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: HELP on New request is not allowed to start because [1264822]
    ... allowed to start because it should come with valid transaction descriptor. ... System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean ... TRXOrders.CLogin.GetTradexUserIDFromQube(String QubeUserID, String GroupCode) ... This error always occurs when we issue the command to SQL Server. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: CMutex /CEvent (multiple threads)
    ... deals with exception detection. ...  If your function does not handle an exception in Java, ... designer did not understand what a Mutex was and that the notion that Lock could return ... ...roll back transaction ...
    (microsoft.public.vc.mfc)
  • Re: Error when calling SqlTransaction.Rollback method
    ... I've seen this when there was an open data reader on the same connection as ... before rolling back the transaction. ... To get the exception, just debug into your code, or alternatively, ... temporarily remove the rollback code or place it in its own try catch. ...
    (microsoft.public.dotnet.framework.adonet)

Loading