Tuesday, 10 September 2013

How to close the ADO connection if the database connection fails while inside a transaction

How to close the ADO connection if the database connection fails while
inside a transaction

I am using ADO via C++, and let's suppose that I have begun a transaction
via ADO when the connection drops out.
I wish to abandon my database changes and close my ADO database connection
object, in order to return to another code module which can display an
error message (without leaving this module in a dodgy state).
The problem is:
1) I cannot close the database connection because the error
adErrInTransaction (0x800a0cae) is thrown (connection object cannot be
explicitly closed while in a transaction)
2) I cannot rollback the transaction because the error 0x8000ffff is
thrown (catastrophic failure, .... connection failure)
This appears to be a catch 22 situation. Had it been that the transaction
was not started then calling Close on the ADO connection succeeds, but
unfortunately with the transaction open I appear to be stuck!
Is the only solution to record the fact that it's failed within a
transaction, so that when the database connection is restored, I can
complete the rollback before attempting anything else?
This was tested with SQLServer 2008 if it's relevant, though I don't
really want a SQLServer-specific solution.

No comments:

Post a Comment