Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 ADO Connection - How to Check if in Transaction?

Tags:

vb6

ado

Is there any way to tell, using just the ADODB.Connection object, whether or not it is currently involved in a transaction?

I'd love to be able to test this on the connect object itself, without relying on keeping a boolean next to it updated.

like image 406
Tom Tresansky Avatar asked Apr 07 '10 12:04

Tom Tresansky


2 Answers

The BeginTrans method can be used as a function that returns the nesting level of the transaction. If you create a property to store this you can check it where ever you need to to see if it is greater than 0. When you commit or rollback you will need to decrement the property yourself.

Private m_TransLevel As Long

Public Property Get TransactionLevel() As Long
    TransactionLevel = m_TransLevel
End Property
Public Property Let TransactionLevel(vLevel As Long)
    m_TransLevel = vLevel
End Property

Public Sub SaveMyData()

    TransactionLevel = adoConnection.BeginTrans()
    ...

End Sub

You could also adapt the return value to work inside a function that returns True/False if the level > 1. I don't like this as well, but it would look something like this (without error handling)

Public Function IsConnectionInsideTransaction(ByVal vADOConnection as ADOBD.Connection) As Boolean
    Dim intLevel As Integer

    If vADOConnection.State = AdStateOpen Then
        intLevel = vADOConnection.BeginTrans()
        IsConnectionInsideTransaction = (intLevel > 1)
        vADOConnection.RollbackTrans
    End If

End Function
like image 164
jac Avatar answered Oct 19 '22 09:10

jac


If you're connecting to an Microsoft SQL Server and can count on it to respond fast enough (i.e. it's not on the other side of the planet) you can perform the query:

SELECT @@TRANCOUNT
like image 35
Stijn Sanders Avatar answered Oct 19 '22 11:10

Stijn Sanders