This is/was our problem. We are using WCF to manage the TransactionScope,
and are also using custom InstanceProviders to generate the Service's
dependencies - including the NHibernate session.
What would it take to get the session to enlist in the transactionscope
even if the session has been created before the transactionscope has?
To better ask the above question is: What would stop that from happening?
The reason I am looking into this avenue and not changing the rest of our
architecture is that it still does not make sense to have to create the
Session AFTER the transactionscope. If we were not using TransactionScope,
it just wouldn't make sense (nor even be possible) to start a Transaction
before creating the Session - so why so different for a TransactionScope?
Regards,
J.
On Saturday, April 14, 2012 9:16:26 PM UTC+1, Ramon Smits wrote:
>
>
> Make sure that your session is created AFTER the transactionscope.
>
> On Fri, Apr 13, 2012 at 12:56 PM, John T wrote:
>
>> Hi group,
>>
>> so I've discovered that NHibernate does not integrate at all well with
>> the Ambient Transaction. In fact, when using NHibernate within a
>> TransactionScope, one would be forgiven for thinking it doesn't integrate
>> at all.
>>
>> What should be the correct usage:
>>
>> public void Foo()
>> {
>> ISession session = null; // get session from wherever
>>
>> using (var transactionScope = new TransactionScope())
>> {
>> session.Save(new PersistableObject { ArbitraryProperty = "a value"
>> });
>> transactionScope.Complete();
>> }
>> }
>>
>> is completely useless. What you actually have to do is:
>>
>> public void Foo()
>> {
>> ISession session = null; // get session from wherever
>>
>> using (var transactionScope = new TransactionScope())
>> using (var transaction = session.BeginTransaction())
>> {
>> session.Save(new PersistableObject { ArbitraryProperty = "a value"
>> });
>> transaction.Commit();
>> transactionScope.Complete();
>> }
>> }
>>
>> So the fact that NHibernate has any integration with the Ambient
>> Transaction seems completely pointless.
>>
>> Now, I've looked (only cursory thus far) through the NHib src and have
>> noted a few areas of interest wrt to integrating with the Ambient
>> Transaction. But I want to ask if anyone has tried this already, and hit
>> any barriers along the way?
>>
>> Regards,
>> John.
>>
>>
>
>
> --
> Ramon
>
>