Should I commit or rollback a read transaction?

后端 未结 12 1176
臣服心动
臣服心动 2020-12-02 10:42

I have a read query that I execute within a transaction so that I can specify the isolation level. Once the query is complete, what should I do?

  • Commit the tr
12条回答
  •  爱一瞬间的悲伤
    2020-12-02 11:40

    Do you need to block others from reading the same data? Why use a transaction?

    @Joel - My question would be better phrased as "Why use a transaction on a read query?"

    @Stefan - If you are going to use AdHoc SQL and not a stored proc, then just add the WITH (NOLOCK) after the tables in the query. This way you dont incur the overhead (albeit minimal) in the application and the database for a transaction.

    SELECT * FROM SomeTable WITH (NOLOCK)
    

    EDIT @ Comment 3: Since you had "sqlserver" in the question tags, I had assumed MSSQLServer was the target product. Now that that point has been clarified, I have edited the tags to remove the specific product reference.

    I am still not sure of why you want to make a transaction on a read op in the first place.

提交回复
热议问题