OracleTransactionオブジェクトは、ローカル・トランザクションを表します。
クラスの継承
System.Object
System.MarshalByRefObject
System.Data.Common.DbTransaction
Oracle.DataAccess.Client.OracleTransaction
宣言
// C# public sealed class OracleTransaction : DbTransaction
// C# public sealed class OracleTransaction : MarshalByRefObject, IDbTransaction, IDisposable
要件
| プロバイダ | ODP.NET管理対象外ドライバ | ODP.NET管理対象ドライバ |
| アセンブリ | Oracle.DataAccess.dll |
Oracle.ManagedDataAccess.dll |
| ネームスペース | Oracle.DataAccess.Client |
Oracle.ManagedDataAccess.Client |
| .NET Framework | 3.5, 4.0, 4.5 | 4.0, 4.5 |
スレッド安全性
パブリック静的メソッドはスレッドセーフですが、インスタンス・メソッドではスレッド安全性は保証されません。
備考
アプリケーションは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();
}
}
.NETストアド・プロシージャではサポートされません
OracleTransactionメンバーは、次の各表にリストしています。
OracleTransaction静的メソッド
OracleTransaction静的メソッドを、表6-137にリストします。
OracleTransactionプロパティ
OracleTransactionプロパティを、表6-138にリストします。
OracleTransactionパブリック・メソッド
OracleTransactionパブリック・メソッドを、表6-139にリストします。
表6-139 OracleTransactionパブリック・メソッド
| パブリック・メソッド | 説明 |
|---|---|
|
|
データベース・トランザクションをコミットします。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ローカル・トランザクションをロールバックします(オーバーロード) |
|
現行トランザクション内にセーブポイントを作成します。 |
|
|
|
|
OracleTransactionプロパティを、表6-141にリストします。
このプロパティでは、トランザクションの分離レベルを指定します。
宣言
// C#
public override IsolationLevel IsolationLevel {get;}
プロパティ値
IsolationLevel
実装
IDbTransaction
例外
InvalidOperationException - トランザクションはすでに完了しています。
備考
デフォルト = IsolationLevel.ReadCommitted
OracleTransactionパブリック・メソッドを、表6-142にリストします。
表6-142 OracleTransactionパブリック・メソッド
| パブリック・メソッド | 説明 |
|---|---|
|
|
データベース・トランザクションをコミットします。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ローカル・トランザクションをロールバックします(オーバーロード) |
|
現行トランザクション内にセーブポイントを作成します。 |
|
|
|
|
このメソッドは、データベース・トランザクションをコミットします。
宣言
// 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は、データベース・トランザクションをロールバックします。
オーバーロード・リスト:
このメソッドは、データベース・トランザクションをロールバックします。
このメソッドは、現行トランザクション内のセーブポイントへデータベース・トランザクションをロールバックします。
このメソッドは、データベース・トランザクションをロールバックします。
宣言
// 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();
}
}
このメソッドは、現行トランザクション内のセーブポイントへデータベース・トランザクションをロールバックします。
宣言
// 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();
}
}