SqlTransaction question

W

WALDO

Once a SqlTransaction object has been committed or rolledback, can the
object still be used?

The reason I ask is because I am building a class that uses a
SqlTransaction object. It has methods that would use the same
transaction. When the user is done, they would call my commit method,
which would commit my SqlTransaction object. What would happen if they
tried to use one of thse same methods AFTER the transaction has been
committed? Would it crash? Would it ignore it? Should I dispose and
destroy my SqlTransaction object upon commitment?
 
C

Cole

You will get an InvalidOperationException if you try to call any of the
transaction methods after you have committed or rolled back. This is listed
in the docs for SqlTransaction under Exceptions.
 
M

Miha Markic [MVP C#]

WALDO said:
Once a SqlTransaction object has been committed or rolledback, can the
object still be used?

The reason I ask is because I am building a class that uses a
SqlTransaction object. It has methods that would use the same
transaction. When the user is done, they would call my commit method,
which would commit my SqlTransaction object. What would happen if they
tried to use one of thse same methods AFTER the transaction has been
committed? Would it crash? Would it ignore it? Should I dispose and
destroy my SqlTransaction object upon commitment?

Yes, dispose the transaction instance asap.
 
W

WALDO

Cool. As I suspected. I will destroy then. Thank you.

(I'm assuming this is NOT the case with a partial rollback though,
correct?)
 

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