Solved

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

Posted on 2004-10-18
8
250 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
Comment Utility
0
 
LVL 2

Accepted Solution

by:
Drifter88zxtW earned 250 total points
Comment Utility
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
Comment Utility

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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:Drifter88zxtW
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks for all you help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now