Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing an Oracle Transaction using C# and ODP.NET

I'm confused. On the face of it, performing a transaction in C# seems simple. From here:

http://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleTransactionClass.htm

string constr = "User Id=scott;Password=tiger;Data Source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();

OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM MyTable";

// 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();
}....

So, create a connection, begin a transaction on that connection, and then off you go until you want to commit or rollback.

However, other sources, such as here:

https://forums.oracle.com/thread/319121

advocate setting the Transaction property of the OracleCommand object itself. e.g.

cmd.Transaction = txn;

Yet other sources say that this property is read only. It's not actually read only, but nowhere appears to clearly say what it does.

My confusion, therefore, is that the existence of the Transaction property on the OracleCommand object seems to suggest that it should be used to perform that command as part of a transaction, and yet Oracle's own documentation does not use this property. So what is it for?

So my questions are:

  1. do I need to set the Transaction property of my OracleCommand, and if so, what exactly does this do?
  2. If I've started a transaction on a connection, are ALL subsequent commands performed on that connection (until a commit or rollback) part of that transaction, even if I don't set the Transaction property on those commands?
like image 883
bornfromanegg Avatar asked Sep 21 '13 09:09

bornfromanegg


People also ask

What is Oracle transaction processing?

A fully automated database service optimized to run transactional, analytical, and batch workloads concurrently. To accelerate performance, it is preconfigured for row format, indexes, and data caching, while providing scalability, availability, transparent security, and real-time operational analytics.

How do transactions work in Oracle?

A transaction is a logical, atomic unit of work that contains one or more SQL statements. A transaction groups SQL statements so that they are either all committed, which means they are applied to the database, or all rolled back, which means they are undone from the database.

What is transaction control in Oracle?

Define transaction controls to specify the types of transactions that are chargeable or nonchargeable for projects and tasks. Use transaction controls to configure your projects and tasks to allow only charges that you expect or plan.

What type of information might an Oracle provide to a transaction?

This could include anything from the current exchange rate for a transfer of funds, to the weather in Minnesota for an insurance policy. Oracles are not the source of information—they're an entity that queries, verifies, and authenticates external data sources, then relays that information back to a smart contract .


1 Answers

1) do I need to set the Transaction property of my OracleCommand,

No.

and if so, what exactly does this do?

It's a no-op.

The OracleCommand automatically "reuses" the transaction that is currently active on the command's OracleConnection. The Transaction property is there simply because it was declared in the base class (DbCommand) and you cannot "undeclare" a member in the inherited class. If you read it you'll get the connection's transaction (if any), setting it does nothing.

2) If I've started a transaction on a connection, are ALL subsequent commands performed on that connection (until a commit or rollback) part of that transaction, even if I don't set the Transaction property on those commands?

Exactly.

like image 177
Branko Dimitrijevic Avatar answered Oct 13 '22 15:10

Branko Dimitrijevic