Solved

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

Posted on 2004-10-18
8
259 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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