More unexpected transactionscope behaviour

9 views
Skip to first unread message

acl123

unread,
Oct 18, 2009, 9:59:53 PM10/18/09
to nhusers
In this case, the second ADO command is committed to the database,
whilst the first is rolled back. What is going on? This seems very
weird to me.

This is kind of a follow up question to my previous post here:
http://groups.google.com/group/nhusers/browse_thread/thread/241cc04309744321


[TestFixture]
public class TestFixture
{
[Test]
public void Test()
{
using(var factory = _config.BuildSessionFactory())
{
using (var session = factory.OpenSession())
{
CallSessionContext.Bind(session);

using(new TransactionScope())
{
using (session.BeginTransaction())
{
// Do something with an NHibernate
mapped entity
}

using(var connection = new SqlConnection
(_connectionString))
{
// Execute an insert statement
}
}

CallSessionContext.Unbind(factory);
}
}
}
}

acl123

unread,
Oct 19, 2009, 8:17:58 PM10/19/09
to nhusers
By the way I am using Session-Per-Web-Request as this appears to be
the recommend way to use NHibernate with Asp .NET.

This means that I definitely don't want to be wrapping the entire
session in a transaction (the method that Ayende uses on his blog). I
want to be using TransactionScope
like you would use an NHibernate transaction - i.e. multiple
transaction per session.

Clearly NHibernate breaks when using nested transactions though, so
I'm kind of stuck.


On Oct 19, 12:59 pm, acl123 <andrewclawre...@gmail.com> wrote:
> In this case, the second ADO command is committed to the database,
> whilst the first is rolled back. What is going on? This seems very
> weird to me.
>
> This is kind of a follow up question to my previous post here:http://groups.google.com/group/nhusers/browse_thread/thread/241cc0430...

Fabio Maulo

unread,
Oct 19, 2009, 10:43:56 PM10/19/09
to nhu...@googlegroups.com
Personally I don't know what you are talking about.
You can have x transactions inside the same nh-session.
session-per-request is a pattern well documented and with thousands of examples.

In your code the first transaction is rolled-back because the transaction is disposed without commit that mean rollback it.

When you don't know how manage transactions the most easy way to solve the problem is saying:
"Clearly NHibernate breaks when using nested transactions though, so I'm kind of stuck."

2009/10/19 acl123 <andrewc...@gmail.com>



--
Fabio Maulo

webpaul

unread,
Oct 20, 2009, 11:56:55 AM10/20/09
to nhusers
Ah, your problem is that you are using a different connection for your
2nd statement. You need to share the transaction on both of those
operations. Here is what I do to mix NHibernate and ADO, no guarantees
but works for me.

/// <summary>
/// Use a workaround to retrieve actual transaction in use so
we can send direct ADO commands
/// in same transaction as nhibernate repositories. WARNING:
Current implementation of SqlDataAccess
/// helper methods does not reuse connection for parameter
discovery so if you are using SQL auth
/// (which you probably should be for NHibernate) then
directly specify SqlParameters to get around this
/// </summary>
/// <returns></returns>
public System.Data.SqlClient.SqlTransaction GetSqlTransaction
()
{
//use workaround to get the transaction for this
nhibernate repository so we can submit direct ADO commands on same
transaction
//http://www.lostechies.com/blogs/joshua_lockwood/archive/
2007/04/10/how-to-enlist-ado-commands-into-an-nhibernate-
transaction.aspx
if (_SqlTransaction == null)
{
using (var cmd = new System.Data.SqlClient.SqlCommand
())
{
cmd.Connection =
(System.Data.SqlClient.SqlConnection)Session.Connection;
Transaction.Enlist(cmd);
_SqlTransaction = cmd.Transaction;
}
}

return _SqlTransaction;
}
System.Data.SqlClient.SqlTransaction _SqlTransaction = null;


On Oct 19, 9:43 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> Personally I don't know what you are talking about.You can have x
> transactions inside the same nh-session.
> session-per-request is a pattern well documented and with thousands of
> examples.
>
> In your code the first transaction is rolled-back because the transaction is
> disposed without commit that mean rollback it.
>
> When you don't know how manage transactions the most easy way to solve the
> problem is saying:
> "Clearly NHibernate breaks when using nested transactions though, so I'm
> kind of stuck."
>
> 2009/10/19 acl123 <andrewclawre...@gmail.com>
> Fabio Maulo- Hide quoted text -
>
> - Show quoted text -

acl123

unread,
Oct 22, 2009, 9:26:38 PM10/22/09
to nhusers
Hi Fabio,
The issue is that the second transaction is committed, even though I
never call transaction.commit.

On Oct 20, 1:43 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> Personally I don't know what you are talking about.You can have x
> transactions inside the same nh-session.
> session-per-request is a pattern well documented and with thousands of
> examples.
>
> In your code the first transaction is rolled-back because the transaction is
> disposed without commit that mean rollback it.
>
> When you don't know how manage transactions the most easy way to solve the
> problem is saying:
> "Clearly NHibernate breaks when using nested transactions though, so I'm
> kind of stuck."
>
> 2009/10/19 acl123 <andrewclawre...@gmail.com>

acl123

unread,
Oct 22, 2009, 9:33:20 PM10/22/09
to nhusers
Sorry, that wasn't very clear - I mean that operations performed
within the outer transaction scope are committed to the database.
It is like the inner transaction is rolling back the outer transaction
scope prematurely.

This is a contrived example by the way, to demonstrate the issue.
Reply all
Reply to author
Forward
0 new messages