Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 10, 2012 19:40
Show Gist options
  • Save davybrion/3693313 to your computer and use it in GitHub Desktop.
Save davybrion/3693313 to your computer and use it in GitHub Desktop.
code snippets for "Avoiding Leaking Connections With NHibernate And TransactionScope" post
[TestFixture]
public class ConnectionLeakTest
{
private static ISessionFactory sessionFactory;
static ConnectionLeakTest()
{
sessionFactory = new Configuration()
.Configure()
.AddAssembly("MyAssembly")
.BuildSessionFactory();
}
[Test]
public void LeaksConnection()
{
var counter = 11;
for (int i = 0; i < counter; i++)
{
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
{
var blah = session.CreateCriteria<User>().List<User>();
}
}
}
}
}
[Test]
public void LeaksConnection()
{
var counter = 11;
for (int i = 0; i < counter; i++)
{
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var blah = session.CreateCriteria<User>().List<User>();
transaction.Rollback();
}
}
}
}
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
transaction.Commit();
}
scope.Complete();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment