| Oracle® Data Provider for .NET Developer's Guide Release 9.2.0.4 Part Number B10961-01 |
|
Oracle.DataAccess.Client Namespace, 21 of 30
An OracleTransaction object represents a local transaction.
Object
MarshalByRefObject
OracleTransaction
// C# public sealed class OracleTransaction : MarshalByRefObject, IDbTransaction, IDisposable
All public static methods are thread-safe, although instance methods do not guarantee thread safety.
The application calls BeginTransaction on the OracleConnection object to create an OracleTransaction object. The OracleTransaction object can be created in one of the following two modes:
Any other mode results in an exception.
The execution of a DDL statement in the context of a transaction is not recommended since it results in an implicit commit that is not reflected in the state of the OracleTransaction object.
All operations related to savepoints pertain to the current local transaction. Operations like commit and rollback performed on the transaction have no effect on data in any existing 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."); } ...
Namespace: Oracle.DataAccess.Client
Assembly: Oracle.DataAccess.dll
OracleTransaction members are listed in the following tables:
OracleTransaction static methods are listed in Table 4-112.
| Methods | Description |
|---|---|
|
|
Inherited from |
OracleTransaction properties are listed in Table 4-113.
| Name | Description |
|---|---|
|
Specifies the isolation level for the transaction |
|
|
Specifies the connection that is associated with the transaction |
OracleTransaction public methods are listed in Table 4-114.
OracleTransaction static methods are listed in Table 4-115.
| Methods | Description |
|---|---|
|
|
Inherited from |
OracleTransaction properties are listed in Table 4-116.
| Name | Description |
|---|---|
|
Specifies the isolation level for the transaction |
|
|
Specifies the connection that is associated with the transaction |
This property specifies the isolation level for the transaction.
// C# public IsolationLevel IsolationLevel {get;}
IsolationLevel
IDbTransaction
InvalidOperationException - The transaction has already completed.
Default = IsolationLevel.ReadCommitted
This property specifies the connection that is associated with the transaction.
// C# public OracleConnection Connection {get;}
Connection
IDbTransaction
This property indicates the OracleConnection object that is associated with the transaction.
OracleTransaction public methods are listed in Table 4-117.
This method commits the database transaction.
// C# public void Commit();
IDbTransaction
InvalidOperationException - The transaction has already been completed successfully, has been rolled back, or the associated connection is closed.
Upon a successful commit, the transaction enters a completed state.
// 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."); } ...
This method frees the resources used by the OracleTransaction object.
// C# public void Dispose();
IDisposable
This method releases both the managed and unmanaged resources held by the OracleTransaction object. If the transaction is not in a completed state, an attempt to rollback the transaction is made.
Rollback rolls back a database transaction.
This method rolls back a database transaction.
This method rolls back a database transaction to a savepoint within the current transaction.
This method rolls back a database transaction.
// C# public void Rollback();
IDbTransaction
InvalidOperationException - The transaction has already been completed successfully, has been rolled back, or the associated connection is closed.
After a Rollback(), the OracleTransaction object can no longer be used because the Rollback ends the transaction.
// 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."); ...
This method rolls back a database transaction to a savepoint within the current transaction.
// C# public void Rollback(string savepointName);
InvalidOperationException - The transaction has already been completed successfully, has been rolled back, or the associated connection is closed.
After a rollback to a savepoint, the current transaction remains active and can be used for further operations.
The savepointName specified does not have to match the case of the savepointName created using the Save method, since savepoints are created in the database in a case-insensitive manner.
This method creates a savepoint within the current transaction.
// C# public void Save(string savepointName);
InvalidOperationException - The transaction has already been completed.
After creating a savepoint, the transaction does not enter a completed state and can be used for further operations.
The savepointName specified is created in the database in a case-insensitive manner. Calling the Rollback method rolls back to savepointName. This allows portions of a transaction to be rolled back, instead of the entire transaction.
// 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. |
|