Allow to set query transaction isolation level
Most of our queries' transaction isolation level is ReadUncommitted. This is very important on our production db to minimize the impact.
I find myself surrounding my queries with TransactionScope all the time.
In addition, some of the queries requries Snapshot isolation's, etc.
It would be very helpful to allow to set the isolation level in the advanced query properties or something similar.
Thanks.
-
Mark Burke commented
I have just been adding these two lines (comment and command) to any linqpad script querying production databases as the first command to execute prior to running the query. Usually at the start of Main().
// Required to prevent concurrent locking of transactions
this.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;"); -
Mark Hurd commented
Further My Extensions:
http://stackoverflow.com/a/26668233/256431
Based upon a single extension in another answer, but essentially identical to Hallur Holm Johannessen's comment.
-
Dave Shaw commented
"When I change the TransactionScope, should the generated SQL have "WITH (NOLOCK)"??"
No, transaction isolation levels work differently, they do not cause the code generators to query hints, they are just set on the current connection.
-
Anonymous commented
When I change the TransactionScope, should the generated SQL have "WITH (NOLOCK)"??
At this point, I don't see the SQL results displaying the hint.
-
Ben Peare commented
This option could also be take in to consideration if the current connection is marked as "Production" and apply the transaction scope automatically.
-
mike commented
Also expose the ability to set Application Intent in connections to SQL,
-
Shlomi commented
Hallur - nice idea :) tnx
-
Hallur Holm Johannessen commented
I have been using the following extension method.
You need to add it to the My Extensions file, which you can see in the My Queries tab.public static class MyExtensions
{
public static IQueryable<T> DumpNoLock<T>(this IQueryable<T> query)
{
using (new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew,
new TransactionOptions
{
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
}))
{
return query.Dump();
}
}
}So now you can dump your query using the following syntax:
Table1.Where(t1 => t1.Enabled).DumpNoLock()
Which is equivalent to including the NOLOCK option on your sql query.
-
Jacob commented
Simple to implement but huge time saver for us users.
-
Ryan Sorensen commented
especially if you want it to replace SSMS
-
Jason DiOrio commented
It would also be nice to have support for "set deadlock_priority low;" since any ad hoc queries we do don't want to get in production's way. I'm currently working on a workaround for this, but it would be nice to have it build into the context in some way.
-
Shlomi commented
Hi Bill,
If you want I've created a small snippet for TransactionScope - 'tscope':
(can used also as surround-with)<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>TransactionScope</Title>
<Shortcut>tscope</Shortcut>
<Description></Description>
<Author>Shlomi</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
$selected$ $end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets> -
Bill Sorensen commented
A big +3 here. We are required to use nolock hints when running ad hoc queries against production. This makes it difficult to use LINQPad as often as I'd like.