简体   繁体   English

使用C#和ODP.NET执行Oracle事务

[英]Performing an Oracle Transaction using C# and ODP.NET

I'm confused. 我糊涂了。 On the face of it, performing a transaction in C# seems simple. 从表面上看,在C#中执行事务似乎很简单。 From here: 从这里:

http://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleTransactionClass.htm 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 https://forums.oracle.com/thread/319121

advocate setting the Transaction property of the OracleCommand object itself. 提倡设置OracleCommand对象本身的Transaction属性。 eg 例如

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. 因此,我的困惑是OracleCommand对象上存在Transaction属性似乎表明它应该用作执行该命令作为事务的一部分,但Oracle自己的文档不使用此属性。 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? 我是否需要设置OracleCommand的Transaction属性,如果是,那究竟是做什么的?
  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? 如果我在连接上启动了一个事务,那么即使我没有在这些命令上设置Transaction属性,那么在该连接上执行所有后续命令(直到提交或回滚)为该事务的一部分?

1) do I need to set the Transaction property of my OracleCommand, 1)我是否需要设置OracleCommand的Transaction属性,

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 . OracleCommand自动“重用”命令的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. Transaction属性就在那里,因为它是在基类( DbCommand )中声明的,并且你不能“取消声明”继承类中的成员。 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? 2)如果我在连接上启动了一个事务,那么即使我没有在这些命令上设置Transaction属性,是否在该连接上执行了所有后续命令(直到提交或回滚)为该事务的一部分?

Exactly. 究竟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM