What happens if you don't commit a transaction to a database (say, SQL Server)?

前端 未结 9 2231
礼貌的吻别
礼貌的吻别 2020-11-27 03:08

Suppose I have a query:

begin tran
-- some other sql code

And then I forget to commit or roll back.

If another client tries to exec

相关标签:
9条回答
  • 2020-11-27 03:10

    Transactions are intended to run completely or not at all. The only way to complete a transaction is to commit, any other way will result in a rollback.

    Therefore, if you begin and then not commit, it will be rolled back on connection close (as the transaction was broken off without marking as complete).

    0 讨论(0)
  • 2020-11-27 03:12

    Any uncomitted transaction will leave the server locked and other queries won't execute on the server. You either need to rollback the transaction or commit it. Closing out of SSMS will also terminate the transaction which will allow other queries to execute.

    0 讨论(0)
  • 2020-11-27 03:20

    In addition to the potential locking problems you might cause you will also find that your transaction logs begin to grow as they can not be truncated past the minimum LSN for an active transaction and if you are using snapshot isolation your version store in tempdb will grow for similar reasons.

    You can use dbcc opentran to see details of the oldest open transaction.

    0 讨论(0)
  • 2020-11-27 03:21

    When you open a transaction nothing gets locked by itself. But if you execute some queries inside that transaction, depending on the isolation level, some rows, tables or pages get locked so it will affect other queries that try to access them from other transactions.

    0 讨论(0)
  • 2020-11-27 03:22

    As long as you don't COMMIT or ROLLBACK a transaction, it's still "running" and potentially holding locks.

    If your client (application or user) closes the connection to the database before committing, any still running transactions will be rolled back and terminated.

    0 讨论(0)
  • 2020-11-27 03:29

    depends on the isolation level of the incomming transaction.

    Sql transaction isolation explained

    0 讨论(0)
提交回复
热议问题