从 asp.net 更新 Oracle 时是否需要“提交”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/335192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is "Commit" necessary when updating Oracle from asp.net?
提问by Justin Cave
I have this code
我有这个代码
protected void btnUpdateAddress_Click(object sender, EventArgs e)
{
sdsAddressComparison.Update();
}
that I'm using to update an oracle database. When I run the update sql code in SQL Navigator I have to type "Commit" or hit the commit button.
我正在使用它来更新 oracle 数据库。当我在 SQL Navigator 中运行更新 sql 代码时,我必须输入“提交”或点击提交按钮。
Do I have to code in a "Commit" somewhere in ASP.NET? and if so how and where do i do it?
我是否必须在 ASP.NET 的某个地方的“提交”中编码?如果是这样,我该怎么做?在哪里做?
回答by Brian Schmitt
Normally, No you do not need the commit.
通常,不,您不需要提交。
However you can write the code to use a transaction, and at the completion of the transaction you can call commit.
但是,您可以编写代码来使用事务,并且在事务完成时您可以调用 commit。
E.G. (Regular):
EG(常规):
try {
? ? ? ? // Open connection
? ? ? ? dbConn.Open();
? ? ? ? //DB Update Code Here
? ? }
? ? catch (Exception ex) {
? ? ? ? throw;
? ? }
? ? finally {
? ? ? ? // Close database connection
? ? ? ? dbConn.Close();
? ? }
E.G. (As Transaction):
EG(作为交易):
try {
? ? ? ? // Open connection & begin transaction
? ? ? ? dbConn.Open();
? ? ? ? dbTran = dbConn.BeginTransaction();
? ? ? ?
//DB Update Code Here
? ? ? ? // Commit transaction
? ? ? ? dbTran.Commit();
? ? }
? ? catch (Exception ex) {
? ? ? ? // Rollback transaction
? ? ? ? dbTran.Rollback();
? ? ? ? throw;
? ? }
? ? finally {
? ? ? ? // Close database connection
? ? ? ? dbConn.Close();
? ? }
回答by Justin Cave
By default, your ASP.Net code, and most other client API's for databases (ODBC, OLE DB, JDBC, etc), run in auto-commit mode. That is, any time a statement is executed successfully, the result is committed. If you are running in that sort of a default mode, there is no need to explicitly commit your update.
默认情况下,您的 ASP.Net 代码和大多数其他用于数据库的客户端 API(ODBC、OLE DB、JDBC 等)在自动提交模式下运行。也就是说,只要成功执行了一条语句,就会提交结果。如果您在那种默认模式下运行,则无需明确提交更新。
On the other hand, there is generally a great deal to be said for putting your updates in explicit transactions-- if you ever have to issue multiple updates in order to make one logical business change, the default auto-commit mode is a very poor one. The classic example here is that if you update account A to withdraw $50 and then update account B to deposit $50 and you end up having two different transactions because of auto-commit being enabled, it is possible that the first transaction would succeed while the other transaction fails and the system loses track of $50.
另一方面,将更新放入显式事务通常有很多要说的——如果您不得不发布多个更新以进行一个逻辑业务更改,那么默认的自动提交模式非常糟糕一。这里的经典示例是,如果您更新帐户 A 以提取 50 美元,然后更新帐户 B 以存入 50 美元,并且由于启用了自动提交,您最终有两个不同的交易,则第一笔交易可能会成功,而另一笔交易可能会成功交易失败,系统丢失 50 美元。
So you generally want to write code similar to what Brian has demonstrated where you use transactions and issue the explicit commit. But by default, you don't have to and your updates will auto-commit.
因此,您通常希望编写类似于 Brian 所演示的代码,其中您使用事务并发出显式提交。但默认情况下,您不必这样做,您的更新将自动提交。
回答by BQ.
The behavior you're seeing in SQL Navigator is probably determined by an options setting.
您在 SQL Navigator 中看到的行为可能由选项设置决定。
I haven't used SQL Navigator, but I do use TOADwhich is also by Quest Software. In the options dialog there, it's under View->Toad Options..., then the Oracle->Transactions node.
我没有使用SQL Navigator,但我确实使用了 Quest Software 的TOAD。在那里的选项对话框中,它位于 View->Toad Options... 下,然后是 Oracle->Transactions 节点。
There's the following relevant settings:
有以下相关设置:
[ ] Commit after every statement (checkbox)
When closing connections: (radio selection)
- ( ) Commit
- ( ) Rollback
- ( ) Prompt for commit/rollback when changes detected, or detection is not possible due to lack of privileges on dmbs_transaction.
[ ] 在每个语句之后提交(复选框)
关闭连接时:(无线电选择)
- ( ) 犯罪
- ( ) 回滚
- ( ) 在检测到更改时提示提交/回滚,或者由于缺乏 dmbs_transaction 的权限而无法检测。
So you could change the setting so you don't need to hit the commit button (or type "commit"), but it's generally a bad practice since a commit is something that you should explicitly be doing (or explicitly rolling back).
因此,您可以更改设置,这样您就无需点击提交按钮(或键入“提交”),但这通常是一种不好的做法,因为提交是您应该明确执行(或明确回滚)的事情。
回答by David Aldridge
Autocommit is also available in SQL*Plus.
SQL*Plus 中也提供自动提交。
SET AUTOCOMMIT ON
SET AUTOCOMMIT OFF
or
或者
SET AUTOCOMMIT 100
Use SHOW AUTOCOMMIT to see the current setting.
使用 SHOW AUTOCOMMIT 查看当前设置。
But ... I hate this setting. You commit at the end of a meaningful unit of work, not part way through.
但是......我讨厌这种设置。你在一个有意义的工作单元结束时提交,而不是部分完成。
回答by David Aldridge
Just for clarification....I'm not talking about SQL Server transactions ....I'm talking about Oracle updates which usually require a commit command when I use either SQL navigator or SQL Plus
只是为了澄清......我不是在谈论 SQL Server 事务......我在谈论 Oracle 更新,当我使用 SQL 导航器或 SQL Plus 时,它通常需要提交命令
The reason i'm posting this is because I can update this data in SQL Navigator but it doesn't update when I use ASP.NET.
我发布此信息的原因是我可以在 SQL Navigator 中更新此数据,但在使用 ASP.NET 时它不会更新。