public void MixingSessionScopeAndTransactionScopes()
{
ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
Recreate();
Post.DeleteAll();
Blog.DeleteAll();
using(new SessionScope())
{
using(TransactionScope root = new TransactionScope())
{
using(TransactionScope t1 = new TransactionScope()) // Isolated
{
Blog blog = new Blog();
Blog.FindAll(); // Just to force a session association
using(new TransactionScope(TransactionMode.Inherits))
{
Blog.FindAll(); // Just to force a session association
blog.Author = "hammett";
blog.Name = "some name";
blog.Save();
}
using(new TransactionScope(TransactionMode.Inherits))
{
Post post = new Post(blog, "title", "post contents", "Castle");
post.Save();
}
t1.VoteRollBack();
}
Blog.FindAll(); // Cant be locked
using(new TransactionScope())
{
Blog blog = new Blog();
Blog.FindAll(); // Just to force a session association
using(new TransactionScope())
{
Blog.FindAll(); // Just to force a session association
blog.Author = "hammett";
blog.Name = "some name";
blog.Save();
}
using(TransactionScope t1n = new TransactionScope(TransactionMode.Inherits))
{
Post post = new Post(blog, "title", "post contents", "Castle");
try
{
post.SaveWithException();
}
catch(Exception)
{
t1n.VoteRollBack();
}
}
}
root.VoteCommit();
}
}
Blog[] blogs = Blog.FindAll();
Assert.AreEqual(1, blogs.Length);
Post[] posts = Post.FindAll();
Assert.AreEqual(0, posts.Length);
}