Results 1 to 3 of 3

Thread: Clarification about how transactions works

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    214

    Question Clarification about how transactions works

    Hi.I have a silly question but I need a clarification:I have a .NET 3.5 application where data is loaded from saver in some datatables

    something like this


    PHP Code:
    Using conn As New FbConnection(...)
                
    conn.Open()
                        
    Using cmd As New FbCommand()
                            
    cmd.Connection conn
                            cmd
    .Transaction tr
                            cmd
    .CommandText "SELECT .."
                            
    Using ad As New FbDataAdapter(cmd)
                                
    ad.Fill(table1)
                            
    End Using

                           
    For Each r As DataRow In table1.Rows
                                     
    .....
                            
    Next

                        End Using

                   

                        Using cmd2 
    As New FbCommand()
                            
    cmd2.Connection conn
                            cmd2
    .Transaction tr
                            cmd2
    .CommandText "SELECT .."
                            
    Using ad As New FbDataAdapter(cmd2)
                                
    ad.Fill(table2)
                            
    End Using

                            
                           
    For Each r As DataRow In table2.Rows
                                     
    .....
                            
    Next


                        End Using

    ..................
                
    End Using 
    there is no transaction defined for above connection,but adding one and measuring execution time,the query works faster:

    HTML Code:
    Using conn As New FbConnection()
                conn.Open()
                Using tr As FbTransaction = conn.BeginTransaction
                        Using cmd As New FbCommand()
                     .............
                    tr.Commit()
                End Using

    the silly question is: why transaction give a better performance in this case as log the above querys could not be execute on the server at the same time,as code above after first query execution data is locally used and then query2 is executed
    thanks for any clarification

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Clarification about how transactions works

    If you're getting better execution on a select query in a transaction one of two thing is happening:
    1) it's an illusion
    2) you're accessing data in an existing transaction

    your comments lead me to believe that #1 is more likely.

    Unless you're accessing data that was written to tables in a transaction that hasn't been committed yet, there is no reason to use a transaction when executing a select statement. It's creating overhead and locks that are going to potentially block other users during the read cycles. Actually, that's probably why it is faster, it's creating a lock on the data, so it doesn't need to worry about someone else writing to it at the same time, so it's able to read it faster. But that's also part of the problem... that means it's going to be locked for any one else to read or write. Generally best practice is to only lock data just before you write to it and unlock it as soon as you're done. Since there isn't a need to lock it when reading it, that would be considered bad form.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    214

    Re: Clarification about how transactions works

    thanks!

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