DB2 Version 10.1 for Linux, UNIX, and Windows

DB2Transaction Class

Represents an SQL transaction to be made at a database. This class cannot be inherited.

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

.NET Framework 2.0 3.0, 3.5, and 4.0 Inheritance hierarchy

System.Object
   System.MarshalByRefObject
      System.Data.Common.DbTransaction
         IBM.Data.DB2.DB2Transaction

.NET Framework 2.0 3.0, 3.5, and 4.0 Syntax

[Visual Basic]
NotInheritable Public Class DB2Transaction
   Inherits DbTransaction
   Implements IDbTransaction, IDisposable
[C#]
public sealed class DB2Transaction : DbTransaction,
   IDbTransaction, IDisposable
[C++]
public __gc __sealed class DB2Transaction : public
   DbTransaction, IDbTransaction, IDisposable
[JScript]
public class DB2Transaction extends DbTransaction implements
   IDbTransaction, IDisposable

Remarks

The application creates an DB2Transaction object by calling BeginTransaction on the DB2Connection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction), are performed on the DB2Transaction object.

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 DB2Transaction.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(IsolationLevel.ReadCommitted)
    ' 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(IsolationLevel.ReadCommitted);
   // 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();
   }
}

Thread safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Version information

Last update
This topic was last updated for: IBM DB2 Version 9.5
.NET Framework version
Supported in: 2.0, 3.0, 3.5, and 4.0
IBM Data Server Client
Supported in: 8.1.2 and up