ヘッダーをスキップ
Oracle® Data Provider for .NET開発者ガイド
11g リリース2 (11.2.0.4)
B66456-02
  目次へ移動
目次
索引へ移動
索引

前
 
次
 

OracleTransactionクラス

OracleTransactionオブジェクトは、ローカル・トランザクションを表します。

クラスの継承

System.Object

  System.MarshalByRefObject

    System.Data.Common.DbTransaction (ADO.NET 2.0専用)

      Oracle.DataAccess.Client.OracleTransaction

宣言

// ADO.NET 2.0: C#
public sealed class OracleTransaction : DbTransaction
// C#
public sealed class OracleTransaction : MarshalByRefObject, 
   IDbTransaction, IDisposable

スレッド安全性

パブリック静的メソッドはスレッドセーフですが、インスタンス・メソッドではスレッド安全性は保証されません。

備考

アプリケーションはOracleConnectionオブジェクトに対してBeginTransactionをコールし、OracleTransactionオブジェクトを作成します。OracleTransactionオブジェクトは、コミット読取りモードでのみ作成できます。他のモードでは、例外が発生します。

トランザクションのコンテキスト内でDDL文を実行することは、OracleTransactionオブジェクトの状態に反映されない暗黙的コミットとなるため、お薦めできません。

セーブポイントに関連する操作はすべて、現行のローカル・トランザクションに関連しています。このトランザクションに対して実行されるコミットおよびロールバックなどの操作は、既存のDataSet内のデータには影響しません。

// Database Setup, if you have not done so yet.
/*
connect scott/tiger@oracle
DROP TABLE MyTable;
CREATE TABLE MyTable (MyColumn NUMBER);
--CREATE TABLE MyTable (MyColumn NUMBER PRIMARY KEY);
 
*/
 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client;
 
class OracleTransactionSample
{
  static void Main()
  {
    // Drop & Create MyTable as indicated Database Setup, at beginning
    
    // This sample starts a transaction and inserts two records with the same
    // value for MyColumn into MyTable.
    // If MyColumn is not a primary key, the transaction will commit.
    // If MyColumn is a primary key, the second insert will violate the 
    // unique constraint and the transaction will rollback.
 
 
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    OracleConnection con = new OracleConnection(constr);
    con.Open();
 
    OracleCommand cmd = con.CreateCommand();
 
    // Check the number of rows in MyTable before transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";    
    int myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Print the number of rows in MyTable
    Console.WriteLine("myTableCount = " + myTableCount);
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(
      IsolationLevel.ReadCommitted);
 
    try
    {
      // Insert the same row twice into MyTable
      cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
      cmd.ExecuteNonQuery();
      cmd.ExecuteNonQuery(); // This may throw an exception
      txn.Commit();
    }
    catch (Exception e)
    {
      // Print the exception message
      Console.WriteLine("e.Message    =  " + e.Message);
 
      // Rollback the transaction
      txn.Rollback();
    }    
 
    // Check the number of rows in MyTable after transaction    
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
    myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Prints the number of rows
    // If MyColumn is not a PRIMARY KEY, the value should increase by two.
    // If MyColumn is a PRIMARY KEY, the value should remain same.
    Console.WriteLine("myTableCount = " + myTableCount);
 
    txn.Dispose();
    cmd.Dispose();
 
    con.Close();
    con.Dispose();
  }
}

要件

ネームスペース: Oracle.DataAccess.Client

アセンブリ: Oracle.DataAccess.dll

ODP.NETのバージョン: ODP.NET for .NET Framework 2.0またはODP.NET for .NET Framework 4

コメント: .NETストアド・プロシージャではサポートされません


OracleTransactionメンバー

OracleTransactionメンバーは、次の各表にリストしています。

OracleTransaction静的メソッド

OracleTransaction静的メソッドを、表5-115にリストします。

表5-115 OracleTransaction静的メソッド

メソッド 説明

Equals

System.Objectからの継承(オーバーロード)


OracleTransactionプロパティ

OracleTransactionプロパティを、表5-116にリストします。

表5-116 OracleTransactionプロパティ

プロパティ 説明

IsolationLevel


トランザクションの分離レベルを指定します。

Connection


トランザクションに関連付けられた接続を指定します。


OracleTransactionパブリック・メソッド

OracleTransactionパブリック・メソッドを、表5-117にリストします。

表5-117 OracleTransactionパブリック・メソッド

パブリック・メソッド 説明

Commit


データベース・トランザクションをコミットします。

CreateObjRef

System.MarshalByRefObjectからの継承

Dispose


OracleTransactionオブジェクトに使用されるリソースを解放します。

Equals

System.Objectからの継承(オーバーロード)

GetHashCode

System.Objectからの継承

GetLifetimeService

System.MarshalByRefObjectからの継承

GetType

System.Objectからの継承

InitializeLifetimeService

System.MarshalByRefObjectからの継承

Rollback


ローカル・トランザクションをロールバックします(オーバーロード)

Save

現行トランザクション内にセーブポイントを作成します。

ToString

System.Objectからの継承



OracleTransaction静的メソッド

OracleTransaction静的メソッドを、表5-118にリストします。

表5-118 OracleTransaction静的メソッド

メソッド 説明

Equals

System.Objectからの継承(オーバーロード)



OracleTransactionプロパティ

OracleTransactionプロパティを、表5-119にリストします。

表5-119 OracleTransactionプロパティ

プロパティ 説明

IsolationLevel


トランザクションの分離レベルを指定します。

Connection


トランザクションに関連付けられた接続を指定します。


IsolationLevel

このプロパティでは、トランザクションの分離レベルを指定します。

宣言

// ADO.NET 2.0: C#
public override IsolationLevel IsolationLevel {get;}

プロパティ値

IsolationLevel

実装

IDbTransaction

例外

InvalidOperationException - トランザクションはすでに完了しています。

備考

デフォルト = IsolationLevel.ReadCommitted

Connection

このプロパティは、トランザクションに関連付けられた接続を指定します。

宣言

// C#
public OracleConnection Connection {get;}

プロパティ値

Connection

実装

IDbTransaction

備考

このプロパティは、トランザクションに関連付けられたOracleConnectionオブジェクトを指定します。


OracleTransactionパブリック・メソッド

OracleTransactionパブリック・メソッドを、表5-120にリストします。

表5-120 OracleTransactionパブリック・メソッド

パブリック・メソッド 説明

Commit


データベース・トランザクションをコミットします。

CreateObjRef

System.MarshalByRefObjectからの継承

Dispose


OracleTransactionオブジェクトに使用されるリソースを解放します。

Equals

System.Objectからの継承(オーバーロード)

GetHashCode

System.Objectからの継承

GetLifetimeService

System.MarshalByRefObjectからの継承

GetType

System.Objectからの継承

InitializeLifetimeService

System.MarshalByRefObjectからの継承

Rollback


ローカル・トランザクションをロールバックします(オーバーロード)

Save

現行トランザクション内にセーブポイントを作成します。

ToString

System.Objectからの継承


Commit

このメソッドは、データベース・トランザクションをコミットします。

宣言

// ADO.NET 2.0: C#
public override void Commit();

実装

IDbTransaction

例外

InvalidOperationException - トランザクションはすでに正常に完了しており、ロールバックされているか、または関連した接続がクローズされています。

備考

正常なコミットの後、トランザクションは完了した状態に入ります。

// Database Setup, if you have not done so yet
/*
connect scott/tiger@oracle
DROP TABLE MyTable;
CREATE TABLE MyTable (MyColumn NUMBER);
--CREATE TABLE MyTable (MyColumn NUMBER PRIMARY KEY);
 
*/
 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class CommitSample
{
  static void Main()
  {
    // Drop & Create MyTable as indicated in Database Setup, at beginning
    
    // This sample starts a transaction and inserts two records with the same
    // value for MyColumn into MyTable.
    // If MyColumn is not a primary key, the transaction will commit.
    // If MyColumn is a primary key, the second insert will violate the 
    // unique constraint and the transaction will rollback.
 
 
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    OracleConnection con = new OracleConnection(constr);
    con.Open();
 
    OracleCommand cmd = con.CreateCommand();
 
    // Check the number of rows in MyTable before transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";    
    int myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Print the number of rows in MyTable
    Console.WriteLine("myTableCount = " + myTableCount);
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(
      IsolationLevel.ReadCommitted);
 
    try
    {
      // Insert the same row twice into MyTable
      cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
      cmd.ExecuteNonQuery();
      cmd.ExecuteNonQuery(); // This may throw an exception
      txn.Commit();
    }
    catch (Exception e)
    {
      // Print the exception message
      Console.WriteLine("e.Message    =  " + e.Message);
 
      // Rollback the transaction
      txn.Rollback();
    }    
 
    // Check the number of rows in MyTable after transaction    
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
    myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Prints the number of rows
    // If MyColumn is not a PRIMARY KEY, the value should increase by two.
    // If MyColumn is a PRIMARY KEY, the value should remain same.
    Console.WriteLine("myTableCount = " + myTableCount);
 
    txn.Dispose();
    cmd.Dispose();
 
    con.Close();
    con.Dispose();
  }
}

Dispose

このメソッドでは、OracleTransactionオブジェクトに使用されるリソースを解放します。

宣言

// C#
public void Dispose();

実装

IDisposable

備考

このメソッドは、OracleTransactionオブジェクトが保持している管理リソースおよび非管理リソースの両方を解放します。トランザクションが完了した状態ではない場合、トランザクションをロールバックする試みが実行されます。

Rollback

Rollbackは、データベース・トランザクションをロールバックします。

オーバーロード・リスト:

  • Rollback()

    このメソッドは、データベース・トランザクションをロールバックします。

  • Rollback(string)

    このメソッドは、現行トランザクション内のセーブポイントへデータベース・トランザクションをロールバックします。

Rollback()

このメソッドは、データベース・トランザクションをロールバックします。

宣言

// ADO.NET 2.0: C#
public override void Rollback();

実装

IDbTransaction

例外

InvalidOperationException - トランザクションはすでに正常に完了しており、ロールバックされているか、または関連した接続がクローズされています。

備考

Rollback()の後、Rollbackによりトランザクションが終了するため、OracleTransactionオブジェクトはそれ以上使用されません。

// Database Setup, if you have not done so yet.
/*
connect scott/tiger@oracle
DROP TABLE MyTable;
CREATE TABLE MyTable (MyColumn NUMBER);
 
*/
 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class RollbackSample
{
  static void Main()
  {
    // Drop & Create MyTable as indicated previously in Database Setup
    
    // This sample starts a transaction and inserts one record into MyTable.
    // It then rollsback the transaction, the number of rows remains the same
 
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    OracleConnection con = new OracleConnection(constr);
    con.Open();
 
    OracleCommand cmd = con.CreateCommand();
 
    // Check the number of rows in MyTable before transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";    
    int myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Print the number of rows in MyTable
    Console.WriteLine("myTableCount = " + myTableCount);
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(
      IsolationLevel.ReadCommitted);
 
    // Insert a row into MyTable
    cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
    cmd.ExecuteNonQuery();
 
    // Rollback the transaction
    txn.Rollback();
 
    // Check the number of rows in MyTable after transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
    myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Prints the number of rows, should remain the same
    Console.WriteLine("myTableCount = " + myTableCount);
 
    txn.Dispose();
    cmd.Dispose();
 
    con.Close();
    con.Dispose();
  }
}

Rollback(string)

このメソッドは、現行トランザクション内のセーブポイントへデータベース・トランザクションをロールバックします。

宣言

// ADO.NET 2.0: C#
public override void Rollback(string savepointName);

パラメータ

  • savepointName

    現行トランザクション内で、ロールバック先のセーブポイント名。

例外

InvalidOperationException - トランザクションはすでに正常に完了しており、ロールバックされているか、または関連した接続がクローズされています。

備考

セーブポイントへのロールバックの後、現行トランザクションはアクティブを維持し、その後の操作に使用できます。

セーブポイントはデータベース内で大文字小文字を区別して作成されるため、指定されたsavepointNameSaveメソッドを使用して作成されたsavepointNameの場合と一致する必要はありません。

Save

このメソッドは現行トランザクション内にセーブポイントを作成します。

宣言

// C#
public void Save(string savepointName);

パラメータ

  • savepointName

    現行トランザクション内で作成されるセーブポイント名。

例外

InvalidOperationException - トランザクションはすでに完了しています。

備考

セーブポイントの作成後、トランザクションは完了状態に入らず、その後の操作に使用できます。

指定されたsavepointNameは大文字小文字を区別して作成されます。RollbackメソッドのコールによりsavepointNameにロールバックします。これにより、トランザクション全体ではなく、トランザクションの一部をロールバックできます。

// Database Setup, if you have not done so yet.
/*
connect scott/tiger@oracle
DROP TABLE MyTable;
CREATE TABLE MyTable (MyColumn NUMBER);
 
*/
 
// C#
 
using System;
using System.Data;
using Oracle.DataAccess.Client; 
 
class SaveSample
{
  static void Main()
  {
    // Drop & Create MyTable as indicated in Database Setup, at beginning
    
    // This sample starts a transaction and creates a savepoint after
    // inserting one record into MyTable.
    // After inserting the second record it rollsback to the savepoint
    // and commits the transaction. Only the first record will be inserted
 
    string constr = "User Id=scott;Password=tiger;Data Source=oracle";
    OracleConnection con = new OracleConnection(constr);
    con.Open();
 
    OracleCommand cmd = con.CreateCommand();
 
    // Check the number of rows in MyTable before transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";    
    int myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Print the number of rows in MyTable
    Console.WriteLine("myTableCount = " + myTableCount);
 
    // Start a transaction
    OracleTransaction txn = con.BeginTransaction(
      IsolationLevel.ReadCommitted);
 
    // Insert a row into MyTable
    cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
    cmd.ExecuteNonQuery();
 
    // Create a savepoint
    txn.Save("MySavePoint");
 
    // Insert another row into MyTable
    cmd.CommandText = "insert into mytable values (2)";
    cmd.ExecuteNonQuery();
 
    // Rollback to the savepoint
    txn.Rollback("MySavePoint");
 
    // Commit the transaction
    txn.Commit();
 
    // Check the number of rows in MyTable after transaction
    cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
    myTableCount = int.Parse(cmd.ExecuteScalar().ToString());
 
    // Prints the number of rows, should have increased by 1
    Console.WriteLine("myTableCount = " + myTableCount);
 
    txn.Dispose();
    cmd.Dispose();
 
    con.Close();
    con.Dispose();
  }
}