dcsimg
Results 1 to 5 of 5

Thread: BeginTrans / RollbackTrans

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    430

    BeginTrans / RollbackTrans

    Can the VB6 detect if currently Transaction was begun?
    I want to check it prior to perform Rollbacktrans in error hadling like following.


    Error:
    If (Transaction was begun) Then
    cn.RollbackTrans
    EndIif

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,423

    Re: BeginTrans / RollbackTrans

    It's your application responsibility to track whether the transaction started or not. It's not something ADO/DAO can tell you. The best way to structure this is, for example:
    Code:
    On Error Go To ErrHandler
    
       cn.BeginTrans
       
       '... your code..
    
       cn.CommitTrans
    
    Exit Sub
    ErrHandler:
       cn.RollBackTrans

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    430

    Re: BeginTrans / RollbackTrans

    jcis,

    I did it already but I just wanted to know if there's something to check it.

    Thanks!

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,405

    Re: BeginTrans / RollbackTrans

    Quote Originally Posted by jdy0803 View Post
    I did it already but I just wanted to know if there's something to check it.
    Directly built-into ADO I can't think of something which offers such an
    "I'm in a transaction"-Information.

    With SQLite you could query the named TransactionStack (or simply the
    TransactionStack-Counter) - but that involves other Wrapper-libs than ADO.

    So, in your case I think the simplest thing you could do is, to just
    introduce more than one Error-Marker (one for your normal Errorhandling,
    and one - e.g. named RollBack: - for your Transaction-block.

    Shortly before entering the Transaction-CodeBlock, you would have
    to switch from normal ErrHandling to your additional Marker:

    Code:
    On Error Goto ErrHandler
    
      '...normal code before the Transaction
    
    On Error Goto RollBack 'switch the normal Error-JumpMarker to the one named RollBack
    
      Cn.BeginTrans
         '......DB-Transactions...
      Cn.CommitTrans
    
    On Error Goto ErrHandler 'switch back to the normal Error-JumpMarker 
    
      '...more normal code
    
    Exit Sub
    
    RollBack:
      Cn.RollBackTrans
    
    ErrHandler:
      MsgBox Err.Description
    End Sub
    Olaf

  5. #5
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,667

    Re: BeginTrans / RollbackTrans

    I use this structure coding:

    Code:
    Public Function SaveData(<parameters>) As Boolean
        On Error GoTo ERR_HANDLER
    
        Dim bOnTransaction As Boolean
        CN.BeginTrans
        bOnTransaction = True
    
        '... code...
    
        CN.CommitTrans
        bOnTransaction = False
    
        SaveData = True
        Exit Function
    
    ERR_HANDLER:
        MsgBox "Error : " & Err.Number & " - " & Err.Description 
        If App.LogMode = 0 Then ' I will check for error...
            Stop
            Resume
        End If
        If bOnTransaction = True Then
            CN.RollbackTrans
        End If
    
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width