Czy instrukcja using wycofa transakcję bazy danych, jeśli wystąpi błąd?

Mam IDbTransaction w instrukcji using, ale nie jestem pewien, czy zostanie wycofana, jeśli wyjątek zostanie wyrzucony w instrukcji using. Wiem, że instrukcja using wymusi wywołanie Dispose ()...ale czy ktoś wie czy to samo dotyczy Rollback()?

Update: również, czy muszę wywoływać Commit() jawnie tak, jak mam poniżej, czy też będzie to również obsługiwane przez instrukcję using?

Mój kod wygląda mniej więcej tak:

using Microsoft.Practices.EnterpriseLibrary.Data;

...

using(IDbConnection connection = DatabaseInstance.CreateConnection())
{
    connection.Open();

    using(IDbTransaction transaction = connection.BeginTransaction())
    {
       //Attempt to do stuff in the database
       //potentially throw an exception
       transaction.Commit();
    }
}
Author: mezoid, 2009-03-13

3 answers

Najwyraźniej tak (dla SQL Server). Tak wygląda metoda Dispose SqlInternalTransaction (wywołania Dispose sqltransaction) z Reflector :

private void Dispose(bool disposing)
{
    // ...
    if (disposing && (this._innerConnection != null))
    {
        this._disposing = true;
        this.Rollback(); // there you go
    }
}

EDIT: @ Medinoc wspomniał, że OracleConnection nie robi tego, więc wydaje się, że jest to implementacja specyficzna.

 90
Author: Sedat Kapanoglu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-21 19:57:22

Musisz wywołać commit. Using statement nie popełni niczego za Ciebie.

 17
Author: jhale,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-14 00:57:52

Wierzę, że jeśli istnieje wyjątek, który nigdy nie został wywołany Commit (), to transakcja zostanie automatycznie wycofana.

 4
Author: Tommy Hui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-13 06:38:39