shubham srivastava
shubham srivastava

Reputation: 319

Why Rollback a Transaction when changes are not reflected into the database until commit?

I'm working on SQL Server 2005 and I see that until the Transaction is not committed the changes made are not visible in the database. My question is that If the changes are not done until commit is executed then why do we rollback the transaction if any error occurs in between the Transaction ??? Even if I don't rollback the transaction i still see the same state of the database as it was before the start of the Transaction so what's the point of rolling back a transaction then ?

Upvotes: 1

Views: 700

Answers (2)

Ricardo Ribeiro
Ricardo Ribeiro

Reputation: 547

Taken from this answer:

If you neither commit nor rollback the transaction, the transaction will continue to exist indefinitely.

And from microsoft docs:

You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction.

Upvotes: 2

Marc Balmer
Marc Balmer

Reputation: 1830

You must rollback so that the server knows you are done with the current transaction. If you don't, all future changes are never writte to the DB.

Upvotes: 2

Related Questions