Results 1 to 4 of 4

Thread: behavior of beginTrans and commitTrans

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Question behavior of beginTrans and commitTrans

    Dear all,

    I would like to make sure my concept of beginTrans and CommitTrans of ADODB.connection is correct, please comment on my question. Thanks in advance.

    I have the following codes:

    Code:
    Public gConnect_Main As ADODB.Connection  'this is the connection object
    Code:
      
    Dim astrsql As String
    Dim v_remark As String
    
    gConnect_Main.BeginTrans
    
    'assume the remark of ID="A1" has the original values = "lok"
    
    astrsql = "update header set remark='lok1234' where ID='A1' "
    gConnect_Main.Execute astrsql
    
    astrsql = "select remark from header where ID='A1' "
    
    v_remark = Fn_Select(gConnect_Main, astrsql)
    
    Debug.Print v_remark
    
    gConnect_Main.CommitTrans
    Code:
    Public Function Fn_Select(pConnect As ADODB.Connection, ByVal aSQLline As String) As Variant
    On Error GoTo Err
    Dim aRs As New ADODB.Recordset
    If fn_SqlOpenRS(aRs, aSQLline, adCmdText, pConnect, adUseServer, adOpenForwardOnly) Then
        If Not aRs.EOF Then
            Fn_Select = IIf(IsNull(aRs.Fields(0).Value), 0, aRs.Fields(0).Value)
        End If
        Call fn_closeADOrs(aRs)
    End If
    Exit Function
    Err:
        MsgBox Err.Description, vbCritical, Err.Source
        Fn_Select = 0
        Call fn_closeADOrs(aRs)
    End Function
    As far as I know, begintrans would only update the value if and only if committrans occurs. I would like to know, why "debug.print v_remark" prints the new value: "lok1234" ? I suppose this value is not yet updated in the database as commitTrans is not yet executed.

    Is this related to the fact that in the function: "fn_Select" i use "adUseServer" and "adOpenForwardOnly"?
    I can still live in my current job because I am here

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: behavior of beginTrans and commitTrans

    Quote Originally Posted by lok1234 View Post

    As far as I know, begintrans would only update the value if and only if committrans occurs. I would like to know, why "debug.print v_remark" prints the new value: "lok1234" ? I suppose this value is not yet updated in the database as commitTrans is not yet executed.

    Is this related to the fact that in the function: "fn_Select" i use "adUseServer" and "adOpenForwardOnly"?
    A transaction occurs in the database. So it will always give you the new updated values when you query it from the same connection. However if you close the connection without calling the UpdateTrans method, everything will be rolled back.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    255

    Re: behavior of beginTrans and commitTrans

    Quote Originally Posted by Pradeep1210 View Post
    A transaction occurs in the database. So it will always give you the new updated values when you query it from the same connection. However if you close the connection without calling the UpdateTrans method, everything will be rolled back.
    i see. Do you mean within the same connection, every query can "see" the updated data? No matter I am using adOpenForwardOnly and aduseServer as the cursortype and cursorlocation?

    Furthermore, is it related to close the recordset?
    I can still live in my current job because I am here

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: behavior of beginTrans and commitTrans

    A transaction relates to a connection. You can open as many recordsets as you want on that connection and you will see the changed values. As long as the connection is open, you won't feel the difference. Once you close the connection, the transaction will be rolled back if it has not been explicitly committed.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

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



Click Here to Expand Forum to Full Width