Example
The following example shows the Snapshot isolation behavior when modifying data. TheCodePerform the following operations:
-
Connect to the adventureworks sample database and enable Snapshot isolation.
-
Create a table named testsnapshotupdate and insert three rows of sample data.
-
Use snapshot to isolate sqltransaction1. Select three rows of data in the transaction.
-
Create the second sqlconnection with adventureworks, use Read committed isolation level to create the second transaction, and update the value of one row selected in sqltransaction1.
-
Submit sqltransaction2.
-
Return sqltransaction1 and try to update the same row submitted by sqltransaction2. The error 3960 is returned, and sqltransaction1 is automatically rolled back. Sqlexception. Number and sqlexception. Message are displayed in the console window.
-
Run the cleanup code to disable Snapshot isolation in adventureworks and delete the testsnapshotupdate table.
View code
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Data. sqlclient; Using System. Data; Namespace Snapshot2 { Class Program { Static Void Main (String [] ARGs ){ String Connectionstring = @" Data Source = xuwei/sqlexpress; initial catalog = adventureworks; user id = sa; Password = sa12345 " ; Using (Sqlconnection connection1 = New Sqlconnection (connectionstring) {connection1.open (); sqlcommand command1 = Connection1.createcommand (); // Enable Snapshot isolation in adventureworks, which sets the database to support Snapshot isolation technology. Command1.commandtext = " Alter database adventureworks set allow_snapshot_isolation on " ; Try {Command1.executenonquery (); console. writeline ( " Snapshot isolation turned on in adventureworks. " );} Catch (Exception ex) {console. writeline ( " Allow_snapshot_isolation on failed: {0} " , Ex. Message );} // Create a table: Create a table. Command1.commandtext = " If exists " +" (Select * From SYS. Tables " + " Where name = n' testsnapshotupdate ') " + " Drop table testsnapshotupdate " ; Command1.executenonquery (); command1.commandtext = " Create Table testsnapshotupdate " + " (ID int primary key, charcol nvarchar (100 )); " ; Try {Command1.executenonquery (); console. writeline ( " Testsnapshotupdate table created. " );} Catch (Exception ex) {console. writeline ( " Create Table failed: {0} " , Ex. Message );} // Insert some data to create data in the table. Command1.commandtext = " Insert into testsnapshotupdate values (1, n 'abcdefg '); " + " Insert into testsnapshotupdate values (2, N 'hijklmn '); " +" Insert into testsnapshotupdate values (3, N 'opqrstuv '); " ; Try {Command1.executenonquery (); console. writeline ( " Data inserted testsnapshotupdate table. " );} Catch (Exception ex) {console. writeline (ex. Message );} // Begin, but do not complete, a transaction // Using the Snapshot isolation level. /* Starting but not finishing a transaction at the Snapshot isolation level, */ Sqltransaction transaction1 = Null ; Try {Transaction1 =Connection1.begintransaction (isolationlevel. snapshot); command1.commandtext = " Select * From testsnapshotupdate where ID between 1 and 3 " ; Command1.transaction = Transaction1; command1.executenonquery (); console. writeline ( " Snapshot transaction1 started. " ); // Open a second connection/transaction to update data // Using readcommitted. This transaction shoshould succeed. /* Open the second database connection and create a transaction for updating data on readcommitted. * The transaction is successful. */ Using (Sqlconnection connection2 = New Sqlconnection (connectionstring) {connection2.open (); sqlcommand command2 =Connection2.createcommand (); command2.commandtext = " Update testsnapshotupdate set charcol = " + " N'new value from connection2' where id = 1 " ; Sqltransaction TRANSACTION2 = Connection2.begintransaction (isolationlevel. readcommitted); command2.transaction = TRANSACTION2; Try {Command2.executenonquery (); transaction2.commit (); // Commit transaction 2 Console. writeline ( " TRANSACTION2 has modified data and committed. " );} Catch (Sqlexception ex) {console. writeline (ex. Message); transaction2.rollback ();} Finally {Transaction2.dispose ();}} // Now try to update a row in connection1/transaction1. // This transaction shocould fail because TRANSACTION2 // Succeeded in modifying the data. /* Attempts to update a row of data in connection 1 and transaction 1. This transaction failed. * This is because the data has been successfully modified in transaction 2. * Transaction 1 is automatically rolled back. */ Command1.commandtext = " Update testsnapshotupdate set charcol = " + " N'new value from connection1' where id = 1 " ; Command1.transaction =Transaction1; command1.executenonquery (); transaction1.commit (); console. writeline ( " You shoshould never see this. " ); // Normally, this sentence should not be output in the console. } Catch (Sqlexception ex) {console. writeline ( " Expected failure for transaction1: " ); Console. writeline ( " {0 }:{ 1} " , Ex. Number, Ex. Message );} Finally {Transaction1.dispose ();}} // Cleanup: cleanupProgram // Turn Off Snapshot isolation and delete the table Using (Sqlconnection connection3 =New Sqlconnection (connectionstring) {connection3.open (); sqlcommand command3 = Connection3.createcommand (); command3.commandtext = " Alter database adventureworks set allow_snapshot_isolation off " ; Try {Command3.executenonquery (); console. writeline ( " Cleanup: Snapshot isolation turned off in adventureworks. " );} Catch (Exception ex) {console. writeline ( " Cleanup failed: {0} " , Ex. Message);} command3.commandtext = " Drop table testsnapshotupdate " ; Try {Command3.executenonquery (); console. writeline ( " Cleanup: testsnapshotupdate table deleted. " );} Catch (Exception ex) {console. writeline ( " Cleanup failed: {0} " , Ex. Message );}}}}}