In Microsoft SQL Server, I create a test table with
CREATE TABLE [Test]
(
[BookID] [int] NOT NULL,
[Name] [varchar](512) NOT NULL,
CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ([BookID] ASC)
) ON [PRIMARY]
Then when I run:
BEGIN TRAN;
INSERT INTO Test (BookID, Name) Values (1,'one');
INSERT INTO Test (BookID, Name) Values (2,'Two');
INSERT INTO Test (BookID, Name) Values (1,'Three');
INSERT INTO Test (BookID, Name) Values (4,'Four');
COMMIT TRAN;
I expect to have nothing in Test as insert (1, 'Three') generates an error
Violation of PRIMARY KEY constraint 'PK_Test'
But actually rows with BookId = 1, 2, 4 are in the table.
If I SET XACT_ABORT ON, then I get the expected behaviour.
Then for another piece of code when the error is like
The transaction log for database 'MyDatabase' is full due to 'ACTIVE_TRANSACTION'
The transaction rollback works
To make sure I get rollback I should include the statement in a TRY ... COMMIT CATCH ROLLBACK statement.
But I am still wondering why the BEGIN TRAN without ROLLBACK does not work all the time. Does it really depend on the type of error as I guess?
