OracleTransaction.Rollback Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Rolls back a transaction from a pending state.
public:
virtual void Rollback();
public:
override void Rollback();
public void Rollback ();
public override void Rollback ();
abstract member Rollback : unit -> unit
override this.Rollback : unit -> unit
override this.Rollback : unit -> unit
Public Sub Rollback ()
Public Overrides Sub Rollback ()
Implements
Exceptions
An error occurred while trying to commit the transaction.
The transaction has already been committed or rolled back.
-or-
The connection is broken.
Examples
The following example creates an OracleConnection and an OracleTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.
public void RunOracleTransaction(string connectionString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand command = connection.CreateCommand();
OracleTransaction transaction;
// Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
command.Transaction = transaction;
try
{
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
command.ExecuteNonQuery();
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
command.ExecuteNonQuery();
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
}
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
Using connection As New OracleConnection(connectionString)
connection.Open()
Dim command As OracleCommand = connection.CreateCommand()
Dim transaction As OracleTransaction
' Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
' Assign transaction object for a pending local transaction
command.Transaction = transaction
Try
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
command.ExecuteNonQuery()
command.CommandText = _
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
command.ExecuteNonQuery()
transaction.Commit()
Console.WriteLine("Both records are written to database.")
Catch e As Exception
transaction.Rollback()
Console.WriteLine(e.ToString())
Console.WriteLine("Neither record was written to database.")
End Try
End Using
End Sub
Remarks
The transaction can be rolled back only from a pending state (after BeginTransaction has been called, but before Commit is called).