Created
September 10, 2012 19:40
-
-
Save davybrion/3693313 to your computer and use it in GitHub Desktop.
code snippets for "Avoiding Leaking Connections With NHibernate And TransactionScope" post
This file contains hidden or 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
[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>(); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or 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
[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(); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 (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