简体   繁体   English

在SQL中隔离提交事务

[英]Isolated committing of transaction in SQL

I have a n-tier C# ASP .Net application server which uses stored procedures to communicate with the database. 我有一个n层C#ASP .Net应用程序服务器,该服务器使用存储过程与数据库进行通信。

I have a service layer which rolls back all ADO .net transactions if an exception is thrown, using TransactionScope.requiresNew. 我有一个服务层,使用TransactionScope.requiresNew可以在引发异常时回滚所有ADO .net事务。

In my stored procedure, I want to track login attempt numbers, so we want to keep the transaction framework as is, but want to have an isolated transaction which we commit. 在我的存储过程中,我想跟踪登录尝试次数,因此我们希望保持事务框架不变,但是希望提交一个隔离的事务。

How do I do this? 我该怎么做呢?

I have tried using a new TransactionScope.RequiresNew in our data layer, but this has no effect. 我已经尝试在数据层中使用新的TransactionScope.RequiresNew,但这无效。

Strange - RequiresNew in the inner (Logging) TransactionScope should work. 奇怪-内部(记录)TransactionScope中的RequiresNew应该可以工作。

In the below nested transaction, TransactionScopeOption.Suppress or TransactionScopeOption.RequiresNew both work for me - the inner transaction is committed (Dal2.x), and the outer one aborted (Dal1.x). 在下面的嵌套事务中,TransactionScopeOption.Suppress或TransactionScopeOption.RequiresNew对我都起作用-内部事务已提交(Dal2.x),外部事务已中止(Dal1.x)。

    try
    {
        using (TransactionScope tsOuter = new TransactionScope(TransactionScopeOption.Required))
        {
            DAL1.Txn1();
            using (TransactionScope tsLogging = new TransactionScope(TransactionScopeOption.Suppress))
            {
                DAL2.Txn2();
                tsLogging.Complete();
            }
            throw new Exception("Big Hairy Exception");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

Edit : Mixing TransactionScope and explicit T-SQL transactions is to be avoided - this is stated in the same link you've referenced viz http://msdn.microsoft.com/en-us/library/ms973865.aspx , quoted below 编辑:避免混合使用TransactionScope和显式T-SQL事务-这在您引用的同一链接中有所说明,即http://msdn.microsoft.com/zh-cn/library/ms973865.aspx ,引用如下

TransactionScopes manage transaction escalation quite intelligently - they will use the (eg DTC will only be used if the transactions span multiple databases or resources - eg SQL and MSMQ). TransactionScopes非常智能地管理事务升级-它们将使用(例如,仅当事务跨越多个数据库或资源时才使用DTC,例如SQL和MSMQ)。 They also work with the SQL 2005+ Lightweight transactions, so multiple connections to the same database will also be managed within a transaction without the overheads of DTC. 它们还与SQL 2005+轻量级事务一起使用,因此,在没有DTC开销的情况下,也可以在事务内管理与同一数据库的多个连接。

IMHO the decision as to whether to use Suppress vs RequiresNew will depend on whether you need to do your auditing within a transaction at all - RequiresNew for an isolated txn, vs Suppress for none. 恕我直言,是否使用Suppress vs RequiresNew的决定将取决于您是否完全需要在事务内进行审核-对于单独的txn,是否需要RequiresNew,而对于不使用txn则为Suppress。

When using System.Transactions, applications should not directly utilize transactional programming interfaces on resource managers—for example the T-SQL BEGIN TRANSACTION or COMMIT TRANSACTION verbs, or the MessageQueueTransaction() object in System.Messaging namespace, when dealing with MSMQ. 使用System.Transactions时,应用程序在处理MSMQ时不应直接利用资源管理器上的事务编程接口,例如T-SQL BEGIN TRANSACTION或COMMIT TRANSACTION动词,或System.Messaging命名空间中的MessageQueueTransaction()对象。 Those mechanisms would bypass the distributed transaction management handled by System.Transactions, and combining the use of System.Transactions with these resource manager "internal" transactions will lead to inconsistent results .... Never mix the two 这些机制将绕过由System.Transactions处理的分布式事务管理,并将System.Transactions与这些资源管理器“内部”事务的结合使用将导致不一致的结果..切勿将两者混为一谈

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

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