1

我很难理解为什么我不能让这个事务回滚。我有一个处理连接/事务的 DataAccess 对象。我有一个 MailingData 业务对象和一个 MailingDataAccess 对象,它从 MailingData 对象中获取 DataAccess 对象 byRef 并执行一个存储过程。我立即回滚更新,但它不会回滚。我猜这是一个范围问题。帮助?我正在使用数据访问进行 byval,但这不起作用,所以我改为 byref。

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
4

1 回答 1

0

我想通了,这一行:

 dbStatus = db.ExecuteNonQuery(DbCommand)

应该:

 dbStatus = db.ExecuteNonQuery(DbCommand, da.Transaction)
于 2013-04-04T21:27:26.067 回答