Oracle equivalent for SQLConnection.BeginTransaction(String TransactionName)
I have been trying to replicate the SQLConnection.BeginTransaction(String TransactionName) for oracle. There is a class OracleConnection.BeginTransaction , but i was not able to find an overload for the method to specify the name of the transaction which needs to be used. Any help on this would be a开发者_运维知识库ppreciated.
Thanks in Advance
I think you may need to execute the SET TRANSACTION SQL statement with the NAME parameter before starting the transaction.
You might be able to inherit from the DbConnection class and create your own overload of the BeginTransaction() method. Then you'd have to inherit from the DbTransaction class to create your own overloads of the Commit() and Rollback() methods. Then use this together with DbProviderFactory and DbCommand objects.
You can associate a Transaction with a Command object. When that command will be executed the connection will run in context of that Transaction.
There is also a Connection Property of Transaction Object which you can use to specify the Connection associated with the Transaction.
See this Example from MSDN:
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.");
}
}
The object OracleTransaction does not have any string member that returns a "name". I think it's the main point to face.
OracleConnection.BeginTransaction() returns an OracleTransaction object so i can't figure how it should be able to assign a name to the transaction.
I hope it helps.
精彩评论