Hi all,
I'm currently using the service broker in combination with snapshot isolation on the database.
The notification request is executed under read commited isolation. The code looks like this:
SqlDependency dependency = new SqlDependency(command, null, 0); dependency.OnChange += eventHandler; using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlTransaction tran = conn.BeginTransaction(IsolationLevel.ReadCommitted)) { command.Transaction = tran; command.ExecuteNonQuery(); tran.Commit(); } }
The request is successfully created and works fine at the first glance.
Now here is my problem:
I created a notification request that should monitor two objects. The query (executed under read committed) looks something like this:
SELECT Id, DataVersion FROM dbo.MyObjects WHERE Id = 1 OR Id = 2
Afterwards I delete both objects in separate nested transactions. Both of them are running under snapshot isolation. It looks something like this:
using (SqlConnection conn1 = new SqlConnection(connectionString)) { conn1.Open(); using (SqlTransaction tran1 = connection1.BeginTransaction(IsolationLevel.Snapshot)) { using (SqlConnection conn2 = new SqlConnection(connectionString)) { conn2.Open(); using (SqlTransaction tran2 = conn2.BeginTransaction(IsolationLevel.Snapshot)) { SqlCommand command2 = conn2.CreateCommand(); command2.Transaction = tran2; command2.CommandText = "DELETE FROM MyObjects WHERE Id = 2"; command2.ExecuteNonQuery(); tran2.Commit(); } } SqlCommand command1 = conn1.CreateCommand(); command1.CommandText = "DELETE FROM MyObjects WHERE Id = 1"; command1.Transaction = tran1; command1.ExecuteNonQuery(); tran1.Commit(); //-> Conflict exception } }
A conflict exception is raised during the commit of last transaction. The conflict seems to occur in the table "sys.query_notifcations_xxxxxxxxx". This is the exact message:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'sys.query_notification_45295271' directly or indirectly in database 'MyDatabase' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.
Is there any restriction for the service broker that prohibits the usage of snapshot isolation.
Thanks in advance.