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
Microsoft .NET Framework Version: 2.0以降
コメント: .NETストアド・プロシージャではサポートされません
OracleTransaction
メンバーは、次の各表にリストしています。
OracleTransaction静的メソッド
OracleTransaction
静的メソッドを、表5-115にリストします。
OracleTransactionプロパティ
OracleTransaction
プロパティを、表5-116にリストします。
OracleTransactionパブリック・メソッド
OracleTransaction
パブリック・メソッドを、表5-117にリストします。
表5-117 OracleTransactionパブリック・メソッド
パブリック・メソッド | 説明 |
---|---|
|
データベース・トランザクションをコミットします。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ローカル・トランザクションをロールバックします(オーバーロード) |
現行トランザクション内にセーブポイントを作成します。 |
|
|
|
OracleTransaction
静的メソッドを、表5-118にリストします。
OracleTransaction
プロパティを、表5-119にリストします。
このプロパティでは、トランザクションの分離レベルを指定します。
宣言
// ADO.NET 2.0: C# public override IsolationLevel IsolationLevel {get;}
プロパティ値
IsolationLevel
実装
IDbTransaction
例外
InvalidOperationException
: トランザクションはすでに完了しています。
備考
デフォルト = IsolationLevel.ReadCommitted
このプロパティは、トランザクションに関連付けられた接続を指定します。
宣言
// C# public OracleConnection Connection {get;}
プロパティ値
Connection
実装
IDbTransaction
備考
このプロパティは、トランザクションに関連付けられたOracleConnection
オブジェクトを指定します。
OracleTransaction
パブリック・メソッドを、表5-120にリストします。
表5-120 OracleTransactionパブリック・メソッド
パブリック・メソッド | 説明 |
---|---|
|
データベース・トランザクションをコミットします。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ローカル・トランザクションをロールバックします(オーバーロード) |
現行トランザクション内にセーブポイントを作成します。 |
|
|
|
このメソッドは、データベース・トランザクションをコミットします。
宣言
// 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(); } }
このメソッドでは、OracleTransaction
オブジェクトに使用されるリソースを解放します。
宣言
// C# public void Dispose();
実装
IDisposable
備考
このメソッドは、OracleTransaction
オブジェクトが保持している管理リソースおよび非管理リソースの両方を解放します。トランザクションが完了した状態ではない場合、トランザクションをロールバックする試みが実行されます。
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(); } }
このメソッドは、現行トランザクション内のセーブポイントへデータベース・トランザクションをロールバックします。
宣言
// ADO.NET 2.0: C#
public override void Rollback(string savepointName);
パラメータ
savepointName
現行トランザクション内で、ロールバック先のセーブポイント名。
例外
InvalidOperationException
: トランザクションはすでに正常に完了しており、ロールバックされているか、または関連した接続がクローズされています。
備考
セーブポイントへのロールバックの後、現行トランザクションはアクティブを維持し、その後の操作に使用できます。
セーブポイントはデータベース内で大文字小文字を区別して作成されるため、指定されたsavepointName
はSave
メソッドを使用して作成されたsavepointName
の場合と一致する必要はありません。
このメソッドは現行トランザクション内にセーブポイントを作成します。
宣言
// 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(); } }