Last active
August 29, 2015 14:01
-
-
Save pawelpabich/6cb1acd9369ba810ba81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Transactions; | |
using IsolationLevel = System.Transactions.IsolationLevel; | |
namespace ConsoleApplication15 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
LogTrasactionInfo(1); | |
using (var scope = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions() {IsolationLevel = IsolationLevel.ReadCommitted})) | |
{ | |
var connection = new SqlConnection("Data Source=.;Initial Catalog=ServicesPortal;Integrated Security=True"); | |
LogTrasactionInfo(2); | |
ExecuteSelect(connection); | |
LogTrasactionInfo(3); | |
using (var scope1 = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() {IsolationLevel = IsolationLevel.Serializable})) | |
{ | |
LogTrasactionInfo(4); | |
ExecuteSelect(connection); | |
scope1.Complete(); | |
} | |
LogTrasactionInfo(5); | |
scope.Complete(); | |
} | |
LogTrasactionInfo(6); | |
Console.ReadLine(); | |
} | |
private static void LogTrasactionInfo(int step) | |
{ | |
var current = Transaction.Current; | |
if (current != null) Console.WriteLine("{1} Level: {0}", current.IsolationLevel, step); | |
else Console.WriteLine("{0} No Tx", step); | |
} | |
private static int ExecuteSelect(SqlConnection connection) | |
{ | |
{ | |
if (connection.State != ConnectionState.Open ) connection.Open(); | |
var command = new SqlCommand("Select Count(*) From dbo.Users", connection); | |
var result = (int)command.ExecuteScalar(); | |
return result; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment