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
スレッド安全性
パブリック静的メソッドはスレッドセーフですが、インスタンス・メソッドではスレッド安全性は保証されません。
備考
アプリケーションでは、OracleTransactionオブジェクトを作成するため、OracleConnectionオブジェクトに対してBeginTransactionがコールされます。OracleTransactionオブジェクトは、次の2つのモードのいずれかで作成できます。
コミット読取り(デフォルト)
シリアライズ可能
その他のモードを使用すると例外が発生します。
トランザクションのコンテキスト内で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バージョン: 1.xまたは2.0
コメント: .NETストアド・プロシージャではサポートされません。
OracleTransactionメンバーを次の表にリストします。
OracleTransaction静的メソッド
OracleTransaction静的メソッドのリストを、表5-96に示します。
OracleTransactionプロパティ
OracleTransactionプロパティのリストを、表5-97に示します。
OracleTransactionパブリック・メソッド
OracleTransactionパブリック・メソッドのリストを、表5-98に示します。
表5-98 OracleTransactionパブリック・メソッド
| パブリック・メソッド | 説明 |
|---|---|
|
|
データベースのトランザクションをコミットします |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
データベースのトランザクションをロールバックします(オーバーロード) |
|
現行のトランザクション内にセーブポイントを作成します |
|
|
|
|
OracleTransaction静的メソッドのリストを、表5-99に示します。
OracleTransactionプロパティのリストを、表5-100に示します。
このプロパティは、トランザクションの分離レベルを指定します。
宣言
// ADO.NET 2.0: C#
public override IsolationLevel IsolationLevel {get;}
// ADO.NET 1.x: C#
public IsolationLevel IsolationLevel {get;}
プロパティ値
IsolationLevel
実装
IDbTransaction
例外
InvalidOperationException - トランザクションはすでに完了しています。
備考
デフォルト = IsolationLevel.ReadCommitted
このプロパティは、トランザクションに関連付けられる接続を指定します。
宣言
// C#
public OracleConnection Connection {get;}
プロパティ値
Connection
実装
IDbTransaction
備考
このプロパティは、トランザクションに関連付けられるOracleConnectionオブジェクトを示します。
OracleTransactionパブリック・メソッドのリストを、表5-101に示します。
表5-101 OracleTransactionパブリック・メソッド
| パブリック・メソッド | 説明 |
|---|---|
|
|
データベースのトランザクションをコミットします |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
データベースのトランザクションをロールバックします(オーバーロード) |
|
現行のトランザクション内にセーブポイントを作成します |
|
|
|
|
このメソッドは、データベースのトランザクションをコミットします。
宣言
// ADO.NET 2.0: C# public override void Commit();
// ADO.NET 1.x: C#
public 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();
// ADO.NET 1.x: C#
public void Rollback();
実装
IDbTransaction
例外
InvalidOperationException - トランザクションはすでに正常に完了、ロールバック済であるか、関連する接続がクローズされています。
備考
Rollback()の後にOracleTransactionオブジェクトを使用することはできません。Rollbackによりトランザクションが終了されるためです。
例
// 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);
// ADO.NET 1.x: C# public 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();
}
}