Is rollback automatic on SqlTransaction with using?

Viewed 88

Let's assume the following code :

try 
{
    using(SqlConnection conn = new SqlConnection(...))
    using(SqlTransaction trans = conn.BeginTransaction())
    using(SQlCommand cmd = new SqlCommand())
    {
        ...
        Sql Exception occurs !
        trans.Commit()
    }
} catch (SqlException) 
{
}

Will there be an automatic rollback because of this exception or is it mandatory to call trans.Rollback() ?

1 Answers

From the documentation:

The transaction is rolled back on any error, or if it is disposed without first being committed.

Related