简体   繁体   English

在vb.net中打开交易验证

[英]opening transaction validation in vb.net

Can anyone help me on how can I validate transaction 谁能帮助我如何验证交易

example: 例:

transaction = mySqlConn.BeginTransaction(IsolationLevel.ReadCommitted)

If there's still an opened transaction, then the code above will ignore.. How do I know if there was a transaction not yet committed before opening new transaction to avoid Nested Transaction? 如果仍然有一个已打开的事务,那么上面的代码将被忽略。我如何才能知道在打开新事务之前是否尚未提交事务以避免嵌套事务?

Have you looked at using System.Transactions.TransactionScope ? 您是否看过使用System.Transactions.TransactionScope It is designed to handle this type of scenario implicitly without having to write custom code. 它旨在隐式处理这种情况,而无需编写自定义代码。

If you are going to use explicit transaction management without using System.Transactions you are going to have to pass your transaction object around (or somehow make it available) and you will need to decide when to start a transaction. 如果要在不使用System.Transactions的情况下使用显式事务管理,则必须传递事务对象(或以某种方式使其可用),并且需要确定何时开始事务。 eg check if the SqlTransaction is null and if so start a transaction otherwise just use the existing transaction. 例如,检查SqlTransaction是否为null,如果是,则启动一个事务,否则就使用现有事务。

You could do something like this (Error handling for transaction.Rollback() is omitted): 您可以执行以下操作(忽略transaction.Rollback()的错误处理):

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlTransaction transaction = null;

    DoSomething(connection, ref transaction);
    DoSomethingElse(connection, ref transaction);

    transaction.Commit();
}

public void DoSomething(SqlConnection connection, ref SqlTransaction transaction)
{
    SqlCommand command = connection.CreateCommand();
    transaction = GetTransaction(connection, transaction);

    command.Connection = connection;
    command.Transaction = transaction;

    ...
}

public void DoSomethingElse(SqlConnection connection, ref SqlTransaction transaction)
{
    ...
}

public SqlTransaction GetTransaction(SqlConnection connection, SqlTransaction transaction)
{
    if (transaction == null)
    {
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
    }

    return transaction;  
}

But usually you would just create your transaction and pass it in to other methods with the understanding that it had already been initialized and is safe to use. 但是通常您只需要创建事务并将其传递给其他方法即可,前提是该事务已被初始化并且可以安全使用。

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

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