我是否需要将数据库事务与 c# TableAdapters 一起使用?
我发现这篇很棒的文章 关于表适配器的事务。然而,本文并没有解释为什么需要甚至需要交易!
为什么值得我尝试与 TableAdapter 一起实现事务?
I found this great article on Transactions with Table Adapters. However, this article doesn't explain why Transactions are needed or even desirable!
Why would it be worth me trying to implement Transactions alongside my TableAdapters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设当您正在保存需要多次查询数据库的内容时,发生了一些不好的事情。当您开始保存操作时,您希望对已保存的所有数据发生什么情况?
大多数开发人员都希望使之前保存的数据失效。
嗯..这就是事务的用途:将所有保存逻辑封装在事务中,这样如果/当中间发生一些不好的事情时,就不会保存任何内容。
有关事务主题的更多信息:http://en.wikipedia.org/wiki/Database_transaction
Suppose that something bad happens when you are in the middle of saving something that takes more than one query to the database. What do you want to happen to all data that has already been saved when you began the save operation?
Most of the developers want to invalidate the data that has been saved previously.
Well.. that's what transactions are for: you encapsulate all the save logic in a transaction so that if/when something bad happens in the middle, nothing is saved.
More on the Transactions subject: http://en.wikipedia.org/wiki/Database_transaction
“为什么”是将这些数据库操作作为更广泛的事务单元的一部分来执行,以便您可以以原子(全有或全无)方式提交和其他事情,或者确保您的读取和写入发生在同一事务中(以避免幻象/不可重复读取)。事实上,我并不是适配器模型的忠实粉丝,但是......
如何;
TransactionScope
会更简单,因为 ADO.NET 连接应该自动登记:工作完成...
The "why" would be to perform those database operations as part of a wider transactional unit, so that you can commit that and other things in an atomic (all-or-nothing) way, or ensure that your reads and writes happen in the same transaction (to avoid phantom/non-repeatable reads). Actually I'm not a huge fan of the adapter model, but...
For how;
TransactionScope
would be simpler, since ADO.NET connections should auto-enlist:job done...
如果您曾经遇到过这样的情况:您有多个表,并且希望在原子调用中保证更新,那么事务使这成为可能。如果没有事务,您可能能够更新一个表,然后第二个表会失败,并且留下有问题的数据。
例如,您可能会遇到这样的情况:您只有一个屏幕,并且希望通过单击一个按钮来添加一条父记录和一堆子记录。如果没有事务,父记录会成功保存,但其中一个子记录会被破坏。通过事务,您可以回滚整个事情并要求用户修复数据问题。
If you ever have the situatino where you have multiple tables that you want to have a guaranted update for in an atomic call, transactions make this possible. Without transactions you may be able to update one table, then the second fails and you are left with problem data.
For example, you may have the situation where yuo have one screen and want to add a parent record and a bunch of child records with a single button click. Without transactions, the parent successfully saves but one of the child records blows up. With transactions, you rollback the whole thing and ask the user to fix the data problem.
事务允许您维护数据库中的数据一致性。通常首选在所有数据库更新/插入中引入事务。如果指定的存储过程因任何原因失败,您始终会回滚。
Transactions allow you to maintain data consistency in database. It is usually preferred to introduce Transactions in all database updates/inserts. You always rollback if a specified stored procedure fails for any reason.
你们在这里发布的每一个内容对我来说听起来都不错,但我们不应该忘记,解决方案总是有利有弊
例如,在应用程序端管理事务(无论如何),您将增加网络流量,因为 .net 必须将所有命令发送到 SQL Server:
using(var tran = new TransactionScope()) {
}
In在这种情况下,它必须发送“开始交易”和“提交”。
最糟糕的想法是,如果在“//do work b”之后连接被切断,会发生什么?这意味着.Net 将无法发送“回滚”或“提交”,因此我们将在 SQL Server 端打开一个可能导致死锁的事务。
Every this you guys have posted here it sound good to me but we shouldn’t forget that against a solution there are always vantages and disadvantage
For example managing the transaction on the application side (doesn’t matter how) you are going to increase the network traffic because .net has to send all the command to SQL Server:
using(var tran = new TransactionScope()) {
}
In this case it has to send a “begin transaction” and “commit” .
The worst think can happen is what does it happen if after “//do work b” the connection cut off?. It means the .Net won’t be able to send neither the “rollback “ or” commit” so that we are going to have a opened transaction on the SQL Server side that can cause dead-lock.