| Oracle® Data Provider for .NET開発者ガイド リリース9.2.0.4 部品番号 B13807-01 |
|
Oracle.DataAccess.Clientネームスペース、21/30
ローカル・トランザクションを表すOracleTransactionオブジェクト。
Object
MarshalByRefObject
OracleTransaction
// C# public sealed class OracleTransaction : MarshalByRefObject, IDbTransaction, IDisposable
インスタンス・メソッドではスレッド・セーフティを保証しませんが、すべてのpublic staticメソッドはスレッド・セーフです。
アプリケーションは、OracleConnectionオブジェクトでBeginTransactionをコールして、OracleTransactionオブジェクトを作成します。OracleTransactionオブジェクトは、次の2つのモードのいずれかで作成できます。
その他のモードで作成すると、例外が発生します。
トランザクションのコンテキストでのDDL文の実行は、OracleTransactionオブジェクトの状態に反映されない暗黙的コミットになるのでお薦めしません。
セーブポイントに関連するすべての操作は、現在のローカル・トランザクションに含まれます。トランザクションで実行されるコミットおよびロールバックなどの操作は、既存のDataSetのデータには影響しません。
// C# // Starts a transaction and inserts one record. If insert fails, rolls back // the transaction. Otherwise, commits the transaction. ... string ConStr = "User Id=myschema;Password=mypassword;" + "Data Source=oracle;"; OracleConnection con = new OracleConnection(ConStr); con.Open(); //Create an OracleCommand object using the connection object OracleCommand cmd = new OracleCommand("", con); // Start a transaction OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted); try { cmd.CommandText = "insert into mytable values (99, 'foo')"; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); txn.Commit(); Console.WriteLine("One record is inserted into the database table."); } catch(Exception e) { txn.Rollback(); Console.WriteLine("No record was inserted into the database table."); } ...
ネームスペース: Oracle.DataAccess.Client
アセンブリ: Oracle.DataAccess.dll
OracleTransactionのメンバーを次の表に示します。
OracleTransactionのstaticメソッドを表4-112に示します。
| メソッド | 説明 |
|---|---|
|
|
|
OracleTransactionのプロパティを表4-113に示します。
| 名前 | 説明 |
|---|---|
|
トランザクションの分離レベルを指定します。 |
|
|
トランザクションに関連付けられた接続を指定します。 |
OracleTransactionのpublicメソッドを表4-114に示します。
OracleTransactionのstaticメソッドを表4-115に示します。
| メソッド | 説明 |
|---|---|
|
|
|
OracleTransactionのプロパティを表4-116に示します。
| 名前 | 説明 |
|---|---|
|
トランザクションの分離レベルを指定します。 |
|
|
トランザクションに関連付けられた接続を指定します。 |
このプロパティは、トランザクションの分離レベルを指定します。
// C# public IsolationLevel IsolationLevel {get;}
IsolationLevel
IDbTransaction
InvalidOperationException: トランザクションはすでに完了しています。
デフォルトはIsolationLevel.ReadCommittedです。
このプロパティは、トランザクションに関連付けられた接続を指定します。
// C# public OracleConnection Connection {get;}
Connection
IDbTransaction
このプロパティは、トランザクションに関連付けられたOracleConnectionオブジェクトを示します。
OracleTransactionのpublicメソッドを表4-117に示します。
このメソッドは、データベース・トランザクションをコミットします。
// C# public void Commit();
IDbTransaction
InvalidOperationException: トランザクションはすでに正常に完了しているか、ロールバックされたか、または関連する接続がクローズされています。
正常にコミットされると、トランザクションは完了状態になります。
// C# // Starts a transaction and inserts one record. If insert fails, rolls back // the transaction. Otherwise, commits the transaction. ... string ConStr = "User Id=myschema;Password=mypassword;" + "Data Source=oracle;"; OracleConnection con = new OracleConnection(ConStr); con.Open(); //Create an OracleCommand object using the connection object OracleCommand cmd = new OracleCommand("", con); // Start a transaction OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted); try { cmd.CommandText = "insert into mytable values (99, 'foo')"; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); txn.Commit(); Console.WriteLine("One record was inserted into the database table."); } catch(Exception e) { txn.Rollback(); Console.WriteLine("No record was inserted into the database table."); } ...
このメソッドは、OracleTransactionオブジェクトによって使用されているリソースを解放します。
// C# public void Dispose();
IDisposable
このメソッドは、OracleTransactionオブジェクトが保持する管理リソースおよび非管理リソースの両方を解放します。トランザクションが完了状態でない場合は、そのトランザクションをロールバックしようとします。
Rollbackは、データベース・トランザクションをロールバックします。
このメソッドは、データベース・トランザクションをロールバックします。
このメソッドは、現在のトランザクション内のセーブポイントまで、データベース・トランザクションをロールバックします。
このメソッドは、データベース・トランザクションをロールバックします。
// C# public void Rollback();
IDbTransaction
InvalidOperationException: トランザクションはすでに正常に完了しているか、ロールバックされたか、または関連する接続がクローズされています。
Rollback()の後は、Rollbackがトランザクションを終了するので、OracleTransactionオブジェクトは使用できなくなります。
// C# // Starts a transaction and inserts one record. Then rolls back the // transaction. ... string ConStr = "User Id=myschema;Password=mypassword;" + "Data Source=oracle;"; OracleConnection con = new OracleConnection(ConStr); con.Open(); OracleCommand cmd = Con.CreateCommand(); // Start a transaction OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted); cmd.CommandText = "insert into mytable values (99, 'foo')"; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); txn.Rollback(); Console.WriteLine("Nothing was inserted into the database table."); ...
このメソッドは、現在のトランザクション内のセーブポイントまで、データベース・トランザクションをロールバックします。
// C# public void Rollback(string savepointName);
InvalidOperationException: トランザクションはすでに正常に完了しているか、ロールバックされたか、または関連する接続がクローズされています。
セーブポイントまでロールバックした後も、現在のトランザクションはアクティブのままとなり、他の操作に使用できます。
セーブポイントは、大/小文字を区別せずにデータベースに作成されるので、指定されたsavepointNameと、Saveメソッドを使用して作成されるsavepointNameは、大/小文字が一致する必要はありません。
このメソッドは、現在のトランザクション内にセーブポイントを作成します。
// C# public void Save(string savepointName);
InvalidOperationException: トランザクションはすでに完了しています。
セーブポイントを作成した後、トランザクションは完了状態にならず、他の操作に使用できます。
指定されたsavepointNameは、大/小文字が区別されずにデータベースに作成されます。Rollbackメソッドをコールすると、savepointNameまでロールバックされます。これにより、トランザクション全体ではなく、トランザクションの一部をロールバックできます。
// C# // Starts a transaction and inserts two records. Creates a savepoint // within the current transaction for the first insert. Then rolls back to // the savepoint to commit the first record. ... string ConStr = "User Id=myschema;Password=mypassword;" + "Data Source=oracle;"; OracleConnection con = new OracleConnection(ConStr); con.Open(); OracleCommand cmd = Con.CreateCommand(); // Start a transaction OracleTransaction txn = con.BeginTransaction(IsolationLevel.ReadCommitted); cmd.CommandText = "insert into mytable values (99, 'foo')"; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); //Create a savepoint txn.Save("MySavePoint"); cmd.CommandText = "insert into mytable values (100, 'bar')"; cmd.ExecuteNonQuery(); //Rollback to the savepoint txn.Rollback("MySavePoint"); //Commit the first insert txn.Commit();
|
|
![]() Copyright © 2002, 2003 Oracle Corporation. All Rights Reserved. |
|