You need to set the isolation level for your select too.
Before your SELECT * FROM [Table]
, add SET TRANSACTION ISOLATION LEVEL SNAPSHOT
.
Read old data from the table, while some transaction is writing to the same table
-
15-01-2022 - |
Domanda
What I want is being able to read old data from the table, while some transaction is writing to the same table. When such atomic writing is completed, I want to replace the old data with the new one.
Transaction I'm coping with is long and I don't want enable dirty reads nor block reading possibility.
I've turned on snapshot isolation level on my database:
SET ALLOW_SNAPSHOT_ISOLATION ON
and used code like this to write data in transaction:
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions
{ IsolationLevel = IsolationLevel.Snapshot }))
{
update();
scope.Complete();
}
Unfortunately while I'm trying to read some data using SELECT * FROM [Table]
in Microsoft SQL Server Management Studio, query execution is waiting with message Executing query...
, and retrieves data after transaction is completed. What I've missed ?
EDIT: code for update() - basically simple inserts within a loop, mixed with no db related stuff connected with WCF which slows down the process:
var val = Web.Download();
using (var connection = new SqlConnection(connection))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO [table]([value]) VALUES (@val)";
command.Parameters.Add(CreateParam(command, "@val", val, DbType.String));
command.ExecuteNonQuery();
}
}
Soluzione