Testing Transaction Commit and Rollback in C# ASP.Net

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

I am just wondering how do we test the transaction's Commit and Rollback?
How do we simulate a situation to let the transaction fail then Rollback?
Thanks for help.

Jason
 
Hello Jason,

U can do it with transaction resource managers (resources that can be enlisted
in transaction), for example database
See this CodeSnippet

string connectionString = "...";
IDbConnection connection = new SqlConnection(connectionString);
connection.Open();
IDbCommand command = new SqlCommand();
command.Connection = connection;

IDbTransaction transaction;
transaction = connection.BeginTransaction(); //Enlisting database
command.Transaction = transaction;
try
{
/* Interact with database here, then commit the transaction */
transaction.Commit();
}
catch
{
transaction.Rollback(); //Abort transaction
}
finally
{
connection.Close();
}


JH> I am just wondering how do we test the transaction's Commit and
JH> Rollback? How do we simulate a situation to let the transaction fail
JH> then Rollback? Thanks for help.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 

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

Back
Top