English 中文(简体)
是否有办法检验潜在的z子交易,看看它是否能够收回?
原标题:
  • 时间:2009-05-23 12:45:48
  •  标签:

如果说由于僵局而暂停交易,那么交易会变成一个z子交易——我猜测,我的代码或框架代码就是退缩。 Sql Transaction是一纸空文,但如果你试图进行退缩,那就会造成错误。 我找不到IsZombie财产。

// Make sure the transaction is not null
if (transaction != null)
{
    //TODO: Is there a way to test a transaction to see if it can be rolled back?
    transaction.Rollback();  
}
最佳回答

You could try using the TransactionScope class from .NET 2.0 s System.Transactions namespace. This class allows you to specify a timeout after which the transaction will automatically be cancelled and rolled back. ADO.NET in .NET 2.0+ is TransactionScope aware, and will automatically enroll a DbTransaction in the scope if one is present at the time the database is called:

public void DoSomething()
{
    using (TransactionScope scope = new TransactionScope(TransactionScopeOptions.Required, TimeSpan.FromSeconds(60)))
    {
        MyDac();

        scope.Complete(); // If timeout occurrs, this line is never hit, scope is disposed, which causes rollback if Complete() was not called
    }
}

public class MyDac()
{

    using (SqlConnection ...)
    {
        using (SqlCommand ...)
        {
            // Do something with ADO.NET here...it will autoenroll if a transaction scope is present
        }
    }
}

交易 范围建立了一个系统。 交易。 内部交易,如果仅涉及一个服务器,则根据违约允许对服务器进行轻重交易。 如果交易涉及多个服务器或分配的资源管理人员,将推动通过交易复印件进行的交易,这将要求贸易与发展协调中心进行协调,从而使交易范围的使用复杂化。 如果你的所有交易都是轻重的,那么,交易复印机可以提供大量好处,而不是手工管理你的 d交易。

问题回答

I beg your pardon but I cannot avoid to disagree. Client transactions is what makes it possible to have a business-process atomic operation. If you want to move all the transactioned operations into DB you are invariably moving business logic into it. It is an approach, but highly un-recommended provided you will use some mildly complex logic on your program. Having whiles/for/foreachs, string checks and other trivial operations are really heavy to move into DB (and sometimes, even impossible). The deadlock hint, however, seems to be quite useful and provides further control to the client application (which is the best way to go, in my opinion).

卡车

我知道老问题,但我最近正在处理这个问题,为我安全地开展/发行SqlTransaction创造了一些帮助作用。 我认为,如果任何其他人都寻求解决这一问题,我就这样说了。

public void CompleteTransaction(SqlTransaction transaction, bool isRollback) {
    if (transaction == null) { return; }
    try {
        if (isRollback) { 
            transaction.Rollback(); 
        } else { 
            transaction.Commit(); 
        }
    } catch (InvalidOperationException ex) {
        // In my case, I just ignored exceptions due to zombie transactions, 
        // but you could handle it differently depending on your needs
        if (ex.TargetSite == null || ex.TargetSite.ToString() != "Void ZombieCheck()") {
            throw; // Not a zombie transaction, so re-throw the exception
        }
    }
}

As I was examining the Exception details for the exception throw due to a zombie transaction, I noticed the TargetSite was Void ZombieCheck(), so my solution assumes that if TargetSite is not set to that, it is not an error due to a zombie transaction.

您不妨考虑将您的交易处理代码放在数据库内,然后可以测试@ERROR = 1205,以确定您的询问是否是僵局的受害者,在这种情况下,你可以重新研究或研究LBACK。 允许客户申请创建和管理交易是一种极易的风险,如果可能,最好避免。

希望这一帮助,

法案





相关问题
热门标签