?
Solved

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

Posted on 2004-10-18
8
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Accepted Solution

by:
Drifter88zxtW earned 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Assisted Solution

by:AzraSound
AzraSound earned 1000 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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

762 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