自定义数据访问层事务管理

时间:2015-03-08 16:50:08

标签: c# multithreading singleton data-access-layer

我有一个数据访问层(DatabaseHelper),我主要在Web应用程序中使用它。在我的DatabaseHelper课程中,有DbConnectionDbProviderFactoryDbTransaction字段。例如,我的ExecuteNonQuery方法如下:

public int ExecuteNonQuery(string cmd, CommandType commandType, DbParameter[] parameters = null)
{
    int num;
    this.OpenDbConnection();
    try
    {
        DbCommand command = this.connection.CreateCommand();
        //command.transaction = this.transaction; // I am not sure of this line
        command.CommandText = cmd;
        command.CommandType = commandType;
        if (parameters != null)
        {
            command.Parameters.AddRange(parameters);
        }
        num = command.ExecuteNonQuery();
    }
    catch (Exception)
    {
        num = -1;
        throw;
    }
    finally
    {
        this.CloseDbConnection();
    }
    return num;
}

我在我的Web应用程序中使用此类具有单例模式。如您所知,Web应用程序本质上是多线程的。我的问题是如何处理数据访问层中的事务?在我的应用程序中,由于单例模式,只有一个DatabaseHelper实例。因此,是否可以重叠不同线程的交易?在这种情况下,begin,commit和rollback transaction的正确用法是什么?

0 个答案:

没有答案