交易没有回滚 - 范围问题?

时间:2013-04-04 13:01:37

标签: sql vb.net transactions scope rollback

我很难理解为什么我不能让这个事务回滚。我有一个处理连接/事务的DataAccess对象。我有一个MailingData业务对象,以及一个MailingDataAccess对象,它从MailingData对象获取DataReccess对象byRef并执行存储过程。我立即回滚了更新,但它没有回滚。我猜这是一个范围问题。救命?我正在使用dataaccess进行byval,但这不起作用所以我改为toref。

vb.net控制台应用 - 代码:

     Dim da As DataAccess = New DataAccess("mydb")

     Try
        ' business object
        Dim mailingData as New MailingData()

        ' start a transaction
        da.StartTransaction()

        ' do stuff, set properties...

        ' pass the data access object byref to a method that calls a stored proc
        mailingData.UpdateCategorize(da)


        ' testing, this doesn't roll back the update from the above stored proc            
        da.RollBackTransaction()

        'da.CommitTransaction()
    Catch ex As Exception
        da.RollBackTransaction()

    End Try

我的数据访问对象:

    Private _database As Database
    Private _transaction As Data.IDbTransaction
    Private _connection As Data.IDbConnection


    Public ReadOnly Property Database() As Database
        Get
            Return _database
        End Get
    End Property

    Public ReadOnly Property Connection() As IDbConnection
        Get
            Return _connection
        End Get
    End Property

    Public ReadOnly Property Transaction() As Data.IDbTransaction
        Get
            Return _transaction
        End Get
    End Property

    Public Sub New()
        _database = GetDatabase()
    End Sub

    Public Sub New(ByVal database As String)
        _database = GetDatabase(database)
    End Sub


    Private Function GetDatabase() As Database

        Return DatabaseFactory.CreateDatabase()

    End Function


    Private Function GetDatabase(ByVal database As String) As Database
            Return DatabaseFactory.CreateDatabase(database)
    End Function

    '-- if not transaction started, start new one.
    Public Sub StartTransaction()
        If _transaction Is Nothing Then
            _connection = _database.CreateConnection()

            _connection.Open()
            _transaction = _connection.BeginTransaction
        End If
    End Sub

    Public Sub CommitTransaction()
        _transaction.Commit()
        If Not IsNothing(Connection) Then
            Connection.Close()
        End If

    End Sub

    Public Sub RollBackTransaction()

        _transaction.Rollback()

        If Not IsNothing(Connection) Then
            Connection.Close()
        End If
    End Sub


End Class

mailingData.UpdateCategorize()在mailingData业务对象中定义为:

Public Function UpdateCategorize(ByRef da As DataAccess) As Integer
    Dim mailingDA As New MailingDataAccess

    Return mailingDA.UpdateCategorize(da, Me)
End Function

MailingDataAccess.UpdateCategorize定义为:

Public Function UpdateCategorize(ByRef da As DataAccess, ByVal clsMailing As MailingData) As Integer
        Dim db As Database = da.Database

        Dim DbCommand As DbCommand
        Dim dbStatus As Integer = 0

        DbCommand = da.Database.GetStoredProcCommand("proc_UpdateCategorize")
        With db
            .AddInParameter(DbCommand, "@pMailingID", Data.DbType.Int32, clsMailing.MailingID)

        dbStatus = db.ExecuteNonQuery(DbCommand)

        Return dbStatus
    End Function

1 个答案:

答案 0 :(得分:0)

我想通了,这一行:

 dbStatus = db.ExecuteNonQuery(DbCommand)

应该是:

 dbStatus = db.ExecuteNonQuery(DbCommand, da.Transaction)