Skip to content

Instantly share code, notes, and snippets.

@PeterLehmann
Created July 10, 2013 09:24
Show Gist options
  • Save PeterLehmann/5964829 to your computer and use it in GitHub Desktop.
Save PeterLehmann/5964829 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Transactions;
using NHibernate;
using NUnit.Framework;
using IsolationLevel = System.Transactions.IsolationLevel;
namespace Tests
{
[TestFixture]
public class ScratchPad
{
private void RunTrans(Action action)
{
using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromMinutes(30)
}))
{
var bytes = TransactionInterop.GetTransmitterPropagationToken(Transaction.Current);
action();
ts.Complete();
}
}
private void UseingAdo()
{
var list = new List<Tuple<IDbConnection, IDbTransaction>>();
try
{
var tx = CreateConnection("StoreDb");
list.Add(tx);
tx = CreateConnection("TrioPosLogStagingDb");
list.Add(tx);
throw null;
}
catch (Exception e)
{
Transaction.Current.Rollback(e);
throw;
}
finally
{
foreach (var t in list)
{
t.Item2.Dispose();
t.Item1.Dispose();
}
list.Clear();
}
}
private static Tuple<IDbConnection, IDbTransaction> CreateConnection(string connectionString)
{
IDbConnection con1 =new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[connectionString].ConnectionString);
con1.Open();
var tx = con1.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
return Tuple.Create(con1, tx);
}
[Test]
public void Fun1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("running for time {0}", i + 1);
try
{
RunTrans(UseingAdo);
}
catch (NullReferenceException) { }
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
Assert.Pass();
}
}
}
@PeterLehmann
Copy link
Author

i knwon that Trancation.Current.Rollback is not need ts does this for me, but it give the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment