How to raise an event when a transaction is rollbacked

G

graphicsxp

Hello,

I've got a few stored procedure called made within one sqlTransaction
object.

If the .Rollback() method of the transaction is called, then I would
like some extra code to be executed. The issue is that the .Rollback()
method can be called from many different places so I don't want to
write this code in all those places. Therefore how can I add an event
handler to this method, so that each time it is called, an event is
raised, which will then execute my code ?

Thank you
 
C

Cor Ligthert [MVP]

Graphix,

Why not just make a module or a shared class from which you can call the
method?

Cor
 
G

graphicsxp

I suppose I could do that... but I thought raising an event sounds
better, i don't have to share anything then.
 
J

jeff

How about ... extending the sqlTransaction object and create you own ...

- inherit from the sqlTransaction object...
- mySqlTransaction Object ...
- add you event there ... myRollBackExtended ...
- If you can, Override the .Rollback and include your raiseent here ...
- If you can not override the .Rollback, create you own .MyRollBack and us
it (this would required you to replace the .Rollback call with myRollback
call ... global / find and replace) ...

Now, in your program, instead of using the sqlTransaction object, use your
MySqltransaction object ... and you will have you custom events ...

to change you code ... use the global / find and replace ...

Find ...

System.Data.SqlClient.SQLTransaction or Data.SqlClient.SQLTransaction or ...
how ever declare it...

Replace

<myproject>.<mynamespace>.mySqltransaction

Sorry this does not work ... sqlTransaction is non-inheritable (do not know
why) ... should have checked before I wrote a response ... too bad ...

However, all is not lost, you could wrap the sqlTransaction in your own
object ... with sqlTransaction as a property / member ... and use your
custom object to extend the rollback functionality... basically, this wold
be a little 'overhead' up front (programming), but it will allow you to
'customize' or 'extend' the functionality of the sqlTransaction object ...
and will put all your code in one place ... and not spreadout over a few
modules or shared classes ... This design allow you to 'reliably' capture
those instances when you need to 'extend' the rollback, without having to
remember to 'include a call another shared class event'...

Jeff.
 
G

graphicsxp

Hi Jeff,
thanks for the long reply. Actually I've opted for the second solution
you offered (to wrap the SQLTransac object) and that works fine.
thanks again,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top