Solved

ADO Transaction - How do I tell if one has been started?

Posted on 2004-10-18
8
254 Views
Last Modified: 2013-11-23
I'm running VB 6, ADO 2.5, and SQL Server 7

In my program, I have a function that updates a Table in the database and a second function that updates a different table in the same database. Both of the Functions can be called individually by other Functions. My problem is that I need to use Transactions in case the update fails, but SQL Server 7 doesn't allow Nested transactions. Is there a way that I can tell if the connection already has a transaction started (similar to checking if an Async Query is still executing - cmdData.State = adStateExecuting)?

Code:

Public Function UpdateTable1() As Boolean
     g_cnConnection.BeginTrans

     Set cmdData = New ADODB.Command
     cmdData.ActiveConnection = g_cnConnection
     cmdData.CommandText = "sp_StoredProc1"
     cmdData.CommandType = adCmdStoredProc
     cmdData.Parameters.Refresh
     cmdData.Parameters("@Param1") = lngParam1

     cmdData.Execute lngRecordsAffected, , adExecuteNoRecords

     If lngRecordsAffected = 1 Then
          Call UpdateTable2
     End If
 
     'Code to Commit or Rollback
     If UpdateTable1 = True Then
          g_cnConnection.CommitTrans
     Else
          g_cnConnection.RollbackTrans
     End If

End Function


Public Function UpdateTable2() As Boolean

     'Code to check if Transaction was started already
     If {Transaction was not started}then
          g_cnConnection.BeginTrans
     End If

     Set cmdData = New ADODB.Command
     cmdData.ActiveConnection = g_cnConnection
     cmdData.CommandText = "sp_StoredProc2"
     cmdData.CommandType = adCmdStoredProc
     cmdData.Parameters.Refresh
     cmdData.Parameters("@Param1") = lngParam1

     cmdData.Execute lngRecordsAffected, , adExecuteNoRecords

     'Code to Commit or Rollback
     If UpdateTable2 = True And {Transaction was not started} Then
          g_cnConnection.CommitTrans
     Elseif UpdateTable2 = False And {Transaction was not started} Then
          g_cnConnection.RollbackTrans
     End If

End Function  


0
Comment
Question by:vrivera22
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:Drifter88zxtW
ID: 12341635
0
 
LVL 2

Accepted Solution

by:
Drifter88zxtW earned 250 total points
ID: 12341661
To sum it up..

When you start a transation. Just set a variable like Dim TransStarted as Boolean to True and set it to false when you have commited the trans or rolled it back. Then just check that variable when you need to know if you have one started.
0
 

Author Comment

by:vrivera22
ID: 12341709

That's what I'm currently doing, but it's messy.  Since my Functions are spread around diferent Classes, I'm forced to use Module-level or Global-level variables.  I was hoping there was an easier way.




0
 
LVL 28

Assisted Solution

by:AzraSound
AzraSound earned 250 total points
ID: 12341964
I would think another variable is required, if not as a global, than as a function parameter, as I am unaware of any ADO connection property that would tell you otherwise.  To keep the classes more "canned", you may opt for passing an optional parameter to your update (and related) functions, e.g.,


Public Function UpdateTable2(Optional TransStarted As Boolean = False) As Boolean
    '...
End Function
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Drifter88zxtW
ID: 12342153
Depending on your design if you have your DB code encapsulated in DB classes then just add a TransStarted property to the appropiate DB class. Then check or set that property when you need to.

Now if your DB code is is sprawled out in many different unrealted classes, the suggestion is no different than using a global variable.

But if you are following say a 3-teir approach then the property suggestion is appropiate.






0
 

Author Comment

by:vrivera22
ID: 12342241

Drifter88zxtW - It's all sprawled out.  

I appreciate everyone's response, although I was hoping for something different.  Oh well, I'll just have to try to convince my company to spend the money to upgrade to SQL 2000 since it supports Nested Transactions!  :)
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 12342433
Can you declare your connection object WithEvents in an object module?  If so, you should be able to use the events BeginTransComplete, CommitTransComplete, and RollbackTransComplete to keep track of the state of any transaction being executed on that connection.
0
 

Author Comment

by:vrivera22
ID: 12358064
Thanks for all you help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 47
When i run adoquery my application freezes 26 150
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 47
Problem to With line 4 42
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now