DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Transaction.Rollback Method

Rolls back a transaction from a pending state.

Namespace:
IBM®.Data.DB2®
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

Syntax

[Visual Basic]
NotOverridable Public Sub Rollback() Implements _
   IDbTransaction.Rollback
[C#]
public void Rollback();
[C++]
public: __sealed void Rollback();
[JScript]
public function Rollback();
Implements:
IDbTransaction.Rollback

Exceptions

Exception type Condition
Exception An error occurred while trying to commit the transaction.
InvalidOperationException The transaction has already been committed or rolled back.

-or-

The connection is broken.

Remarks

The transaction can be rolled back only from a pending state (after DB2Connection.BeginTransaction has been called, but before DB2Transaction.Commit is called).

Example

[Visual Basic, C#] The following example creates a DB2Connection and a DB2Transaction. It also demonstrates how to use the DB2Connection.BeginTransaction, DB2Transaction.Commit, and Rollback methods.

[Visual Basic]
Public Sub RunDB2Transaction(myConnString As String)
    Dim myConnection As New DB2Connection(myConnString)
    myConnection.Open()

    Dim myCommand As New DB2Command()
    Dim myTrans As DB2Transaction

    ' Start a local transaction
    myTrans = myConnection.BeginTransaction()
    ' Assign transaction object for a pending local transaction
    myCommand.Transaction = myTrans

    Try
        myCommand.CommandText = "Insert into org(
          DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION) 
          VALUES (100, 'Head Office', 160, 'Corporate', 'New York')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "Insert into org(
          DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION) 
          VALUES (101, 'New England', 50, 'Eastern', 'Boston')"
        myCommand.ExecuteNonQuery()
        myTrans.Commit()
        Console.WriteLine("Both records are written to database.")
    Catch e As Exception
        myTrans.Rollback()
        Console.WriteLine(e.ToString())
        Console.WriteLine("Neither record was written to database.")
    Finally
        myConnection.Close()
    End Try
End Sub

[C#]
public void RunDB2Transaction(string myConnString)
{
   DB2Connection myConnection = new DB2Connection(myConnString);
   myConnection.Open();

   DB2Command myCommand = new DB2Command();
   DB2Transaction myTrans;

   // Start a local transaction
   myTrans = myConnection.BeginTransaction();
   // Assign transaction object for a pending local transaction
   myCommand.Transaction = myTrans;

   try
   {
     myCommand.CommandText = "Insert into org(
      DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION) 
      VALUES (100, 'Head Office', 160, 'Corporate', 'New York')";
     myCommand.ExecuteNonQuery();
     myCommand.CommandText = "Insert into org(
       DEPTNUMB, DEPTNAME, MANAGER, DIVISION,LOCATION) 
       VALUES (101, 'New England', 50, 'Eastern', 'Boston')";
     myCommand.ExecuteNonQuery();
     myTrans.Commit();
     Console.WriteLine("Both records are written to database.");
   }
   catch(Exception e)
   {
     myTrans.Rollback();
     Console.WriteLine(e.ToString());
     Console.WriteLine("Neither record was written to database.");
   }
   finally
   {
     myConnection.Close();
   }
}