BeginTransaction(IsolationLevel)

This method begins a local transaction with the specified isolation level.

Declaration

// C#
public OracleTransaction BeginTransaction(IsolationLevel isolationLevel);

Parameters

  • isolationLevel

    The isolation level for the new transaction.

Return Value

An OracleTransaction object representing the new transaction.

Implements

IDbConnection

Exceptions

InvalidOperationException - A transaction has already been started.

ArgumentException - The isolationLevel specified is invalid.

Remarks

The following isolation levels are supported: IsolationLevel.ReadCommitted and IsolationLevel.Serializable.

Although the BeginTransaction method supports the IsolationLevel.Serializable isolation level, serializable transactions are not supported when using System.Transactions and TransactionScope.

Requesting other isolation levels causes an exception.

If a local transaction is already started implicitly, invoking BeginTransaction() will inherit that transaction.

If the transaction is created explicitly using BeginTransaction(), the transaction can be operated on either through the OracleConnection methods or OracleTransaction methods. But once the transaction is over via a Commit() or a Rollback() invoked on either object, the OracleTransaction cannot be used henceforth.

The BeginTransaction(IsolationLevel) method overrides the OracleConnection IsolationLevel property. Once that transaction commits or rolls back, the connection’s IsolationLevel returns to its previous value.

Auto-commit is disabled when this method is invoked successfully.

Example

// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class BeginTransactionSample
{
  static void Main()
  {
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    OracleConnection con = new OracleConnection(constr);
    con.Open();
 
    // Create an OracleCommand object using the connection object
    OracleCommand cmd = con.CreateCommand();
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted);
 
    // Update EMP table
    cmd.CommandText = "update emp set sal = sal + 100";
    cmd.ExecuteNonQuery();
 
    // Rollback transaction
    txn.Rollback();
    Console.WriteLine("Transaction rolledback");
 
    // Clean up
    txn.Dispose();
    cmd.Dispose();
    con.Dispose();
  }
}