RSB: NH transaction in the message module

9 views
Skip to first unread message

Vadim Kantorov

unread,
Oct 21, 2009, 1:26:23 PM10/21/09
to Rhino Tools Dev
Keeping in mind this crazy issue:
http://groups.google.ru/group/nhibernate-development/browse_thread/thread/d3b5413d3ddbdfe7
I tried to use native NH transactions everywhere just in case. I
thought creating a message module would be a solution.
Unfortunately, my module doesn't work correctly.

public class NhMessageModule : IMessageModule
{
readonly ISessionFactory factory;

public NhMessageModule(ISessionFactory factory)
{
this.factory = factory;
}

public void Init(ITransport transport)
{
transport.MessageArrived += OpenSession;
transport.MessageProcessingCompleted += DisposeOfSession;
}

public void Stop(ITransport transport)
{
transport.MessageArrived -= OpenSession;
transport.MessageProcessingCompleted -= DisposeOfSession;
}

private bool OpenSession(CurrentMessageInformation arg)
{
var session = factory.OpenSession();
session.BeginTransaction();

CurrentSessionContext.Bind(session);

return false;
}

private void DisposeOfSession(CurrentMessageInformation arg1,
Exception arg2)
{

if (CurrentSessionContext.HasBind(factory))
{
var session = CurrentSessionContext.Unbind(factory);

if (session.Transaction != null && session.Transaction.IsActive)
session.Transaction.Commit();

session.Dispose();
}
}
}


It fails with an SqlException
Message: The COMMIT TRANSACTION request has no corresponding BEGIN
TRANSACTION.
Stack trace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning
(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at
System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest
(Byte[] buffer, TransactionManagerRequestType request, String
transactionName, TransactionManagerIsolationLevel isoLevel, Int32
timeout, SqlInternalTransaction transaction, TdsParserStateObject
stateObj, Boolean isDelegateControlRequest)
at
System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon
(TransactionRequest transactionRequest, String transactionName,
IsolationLevel iso, SqlInternalTransaction internalTransaction,
Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction
(TransactionRequest transactionRequest, String name, IsolationLevel
iso, SqlInternalTransaction internalTransaction, Boolean
isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalTransaction.Commit()
at System.Data.SqlClient.SqlTransaction.Commit()
at NHibernate.Transaction.AdoTransaction.Commit()

As I found out it stems from the fact that the distributed
transaction (in which RSB encloses every message handling) gets
disposed before the MessageProcessionCompleted event is fired.

Any other way to enclose every message handling into an NH
transaction? Do you think this RSB behavior should be changed?

Ayende Rahien

unread,
Oct 21, 2009, 1:29:18 PM10/21/09
to rhino-t...@googlegroups.com
We can probably offer another event, before we commit the DTC

Vadim Kantorov

unread,
Oct 22, 2009, 12:49:25 AM10/22/09
to Rhino Tools Dev
It'd be cool in the light of the NHibernate + DTC issue

On 21 окт, 23:29, Ayende Rahien <aye...@ayende.com> wrote:
> We can probably offer another event, before we commit the DTC
>
> On Wed, Oct 21, 2009 at 7:26 PM, Vadim Kantorov <vadimkanto...@gmail.com>wrote:
>
>
>
>
>
> > Keeping in mind this crazy issue:
>
> >http://groups.google.ru/group/nhibernate-development/browse_thread/th...

Ayende Rahien

unread,
Oct 22, 2009, 7:04:42 AM10/22/09
to rhino-t...@googlegroups.com
Send me a patch for this

Mike Nichols

unread,
Oct 22, 2009, 8:59:53 AM10/22/09
to rhino-t...@googlegroups.com
@Vadim
Have you tried to run this without NH transactions? I pulled out my message module that was doing something similar.
I am only using ambient transactions and things seem to work just fine with the AdoNetWithDistributed transation factory as the default in NH 2.1 +. Initially I thought I would need explicit NH transactions but even using two session factories everything Just Works. Using NH Prof shows me the transactional boundaries are just what I expect.

Vadim Kantorov

unread,
Oct 22, 2009, 3:22:04 PM10/22/09
to Rhino Tools Dev
Sorry guys. My NH transaction factory was switched to the old one
(switched when was trying to figure out NH + DTC issue). With the
default it all seems to work correctly.

On 22 окт, 18:59, Mike Nichols <nichols.mik...@gmail.com> wrote:
> @Vadim
> Have you tried to run this without NH transactions? I pulled out my message
> module that was doing something similar.
> I am only using ambient transactions and things seem to work just fine with
> the AdoNetWithDistributed transation factory as the default in NH 2.1 +.
> Initially I thought I would need explicit NH transactions but even using two
> session factories everything Just Works. Using NH Prof shows me the
> transactional boundaries are just what I expect.
>
>
>
> On Thu, Oct 22, 2009 at 4:04 AM, Ayende Rahien <aye...@ayende.com> wrote:
> > Send me a patch for this
>
> > On Thursday, October 22, 2009, Vadim Kantorov <vadimkanto...@gmail.com>
Reply all
Reply to author
Forward
0 new messages