哪一个更好的aspx.cs页面事务与数据库事务?为什么? [英] which one is better aspx.cs page transaction vs database transaction?Why?

查看:49
本文介绍了哪一个更好的aspx.cs页面事务与数据库事务?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个更好的aspx.cs页面事务vs数据库事务?为什么?

Which one is better aspx.cs page transaction vs database transaction? Why?

推荐答案

与许多事情一样,答案是它取决于。两者都有利有弊,所以两者都没有正确。你要做的是了解利弊,这样你就可以做出适合你目前面临的情况的决定,而采访者将使用魔鬼的拥护者策略来判断你是否了解两者的利弊。如果你说代码隐藏他会问为什么?为什么不在SP?他并没有问这个问题,因为你给出了一个错误的答案,他看到你是否只是理解其中的差异而不只是重复你在互联网上读到的废话。



MVC比webforms更好为什么? 嗯...因为互联网?



如果您在SP中放置交易,那么您就知道调用该SP的任何代码都使用您的交易。但是,如果您正在嵌套SP调用并且他们有自己的事务管理,那么事情就会变得棘手。 SP中的交易也可能表现得更好,但我不会引用我的话。



如果您将交易保持在代码隐藏级别,那么您可以确保任何和所有SPs \ SQL命令都在同一个事务中,如果您不想使用事务,那么您可以在没有它的情况下调用SP。因此,如果您有一个Add_Money SP和一个Remove_Money SP,并且有人将钱存入帐户,您可以从您的页面调用Add_Money。如果他们将钱从一个帐户转移到另一个帐户,您可以创建一个交易并在同一笔交易中调用Add_Money和Remove_Money。



因此,在页面级别,您可以获得灵活性如果要使用事务,在SP级别可以强制使用事务,但如果要在同一事务中拥有多个SP,则可能会变得棘手。为了在混合中添加更多复杂性,还有分布式事务,其中多个数据库上的SP,甚至是您对相同文件等具有事务支持的其他资源都可以在同一个全局事务中登记,如果不是这样的话,这也是不可能的。您的交易在SP中。
The answer, as with many things, is "it depends". Both have pros and cons so neither is "right". What you have to do is understand the pros and cons so you can make a decision that is right for the situation you are currently facing, and interviewers will use "devil's advocate" tactics to see if you know the pros and cons of both. Had you have said code-behind he would have asked "Why? Why not in the SP?" He isn't asking this because you gave a "wrong" answer, he is seeing if you simply understand the difference and aren't just repeating nonsense you've read on the internet.

"MVC is better than webforms" "Why?" "Um...because internet?"

If you put transactions in the SP then you know that any code that calls that SP uses your transactions. However if you're nesting SP calls and they have their own transaction management then things can get tricky. Transactions in the SP probably perform better too, but I wouldn't quote me on that.

If you keep your transaction at the code-behind level then you can ensure that any and all SPs\SQL commands are in that same transaction, and if you don't want to use transactions then you can call the SPs without it. So if you have an Add_Money SP and a Remove_Money SP, and someone deposits money into an account you can call the Add_Money from your page. If they are moving money from one account to another you can create a transaction and call Add_Money and Remove_Money in that same transaction.

So at page-level you get the flexibility of if\when to use transactions, at the SP level you can force the use of transactions, but if you want to have multiple SPs in the same transaction it can get tricky. To add even more complexity into the mix there are also distributed transactions where SPs on multiple databases, and even other recourses that you have transaction support for like files etc, can all be enlisted in the same global transaction, which again isn't possible if your transactions are in the SP.


这篇关于哪一个更好的aspx.cs页面事务与数据库事务?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆