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
Solved

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

Posted on 2004-10-18
8
263 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
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…

856 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