[nhibernate-development] leaking connections when using transactionscope without NH transaction

47 views
Skip to first unread message

Davy Brion

unread,
May 6, 2010, 8:19:43 AM5/6/10
to nhibernate-...@googlegroups.com
Hey guys,

i just ran into a weird issue with leaking connections due to using a TransactionScope without using an NH transaction...  i've described the problem here:

the thing is: is this a bug in NH or not? I mean... it might be due to bad usage, but if the usage scenario is bad, then NHibernate should probably warn against it instead of playing along and failing silently in some cases...

thoughts?

Richard Brown

unread,
May 6, 2010, 8:37:30 AM5/6/10
to nhibernate-...@googlegroups.com

IMHO, if the usage is not allowed, we should change the API to disallow session without a tx.  If it is allowed we should fix it.

Sent from my Android phone.

Davy Brion

unread,
May 6, 2010, 8:42:23 AM5/6/10
to nhibernate-...@googlegroups.com
yup, i feel the same way

Ayende Rahien

unread,
May 6, 2010, 8:59:18 AM5/6/10
to nhibernate-...@googlegroups.com
Yes, it is a bug.
We should handle this scenario. What I don't understand is _why_.
I am pretty sure that this should generate the same logic as a rollback does, and we are clearing things on rollback.

John Davidson

unread,
May 6, 2010, 8:59:41 AM5/6/10
to nhibernate-...@googlegroups.com
It may seem to be a good idea to have a mandatory transaction within a session, but you are forgetting that a session may use many transactions, which makes it difficult to force a transaction creation within a sesssion. I.E. when you commit a transaction and and the session is not yet closed do you automatically create another transaction, just in case the session is going to do more transactional work?

I think many of the problems are a result of changes from v1.2 to 2.x where it was recognized that a transaction was necessary for any read or write activity with the database. However, there probably is a substantial body of applications started in v1.2 that were upgraded to v2.x where the upgrade to wrap all reads did not happen.

I don't think this can easily be fixed by changing the session semantics.

John Davidson

Fabio Maulo

unread,
May 6, 2010, 10:03:45 AM5/6/10
to nhibernate-...@googlegroups.com
IMO is the moment to write something in out official reference.
We should dedicate a section on : How work with TransactionScope 

I have never found a problem because I'm opening the NH'session and begin, always, the NH's transaction inside the transaction scope. All problem I have seen is about ppl who don't want use the NH's transaction... with which reason ? so far I saw only "style" reason... for me the "style" is a matter for FashionTV.
--
Fabio Maulo

Davy Brion

unread,
May 6, 2010, 10:12:50 AM5/6/10
to nhibernate-...@googlegroups.com
Well for starters, it just feels pointless to use an NHibernate Transaction if TransactionScope is supposed to take care of that for you.  And let's be honest here, that's just how it works with probably every other data layer out there in the .NET world.  Why should we be different?  

But if usage of an NH transaction is indeed absolutely required, then we sure shouldn't put up with the current behavior either.  It's way too easy to make a mistake with it and just putting a notice in the official reference about it feels like a cop-out instead of actually dealing with a real problem.

Fabio Maulo

unread,
May 6, 2010, 10:33:51 AM5/6/10
to nhibernate-...@googlegroups.com
On Thu, May 6, 2010 at 11:12 AM, Davy Brion <ral...@davybrion.com> wrote:
Well for starters, it just feels pointless to use an NHibernate Transaction if TransactionScope is supposed to take care of that for you.  And let's be honest here, that's just how it works with probably every other data layer out there in the .NET world.  Why should we be different?  


Well... we are different in many cases and probably our differences are the reason of NH success.
Perhaps in this case we can make an exception and try to be equals to others.
Opening a session we can check if there is an Ambient Transaction and, if so, begin a NH's transaction by default; this behavior will have a performance issue when the application does not use TransactionScope everywhere.
How we will manage the need of "nested" transactions will be another matter.

The other possibility is try to fix the actual behavior.

In any case we need a specific section in our official reference.

--
Fabio Maulo

Diego Mijelshon

unread,
May 6, 2010, 10:36:28 AM5/6/10
to nhibernate-...@googlegroups.com
I feel a déjà vu... we discussed this same issue last month (but I think it was on the users group).
There's even an open jira: http://216.121.112.228/browse/NH-2181

If you ask me... we do have a violation of the principle of least surprise.
I can't count how many mails in the users group and questions in Stackoverflow I've answered with "use a transaction".

While I agree with Fabio in that an official reference on TransactionScope is needed (in the main docs), this should be more intuitive for new users. Maybe NH 3 is an opportunity to introduce breaking changes...

   Diego

Carlos cubas

unread,
May 6, 2010, 11:09:47 AM5/6/10
to nhibernate-...@googlegroups.com
Oh wow my feature request got some spot light :).

 If I get a chance today I'll open a Jira feature request for a "Transaction Scope" section on the official reference documentation.


-Carlos
 
Practice makes perfect, but if no one is perfect, why practice?





Date: Thu, 6 May 2010 11:36:28 -0300
Subject: Re: [nhibernate-development] leaking connections when using transactionscope without NH transaction
From: di...@mijelshon.com.ar
To: nhibernate-...@googlegroups.com

John Davidson

unread,
May 6, 2010, 12:19:03 PM5/6/10
to nhibernate-...@googlegroups.com
TransactionScope is meant to work with a System.Transactions.Transaction. NHibernate does not use this, but instead uses System.Data.IDBTransaction. It does not appear that there is any way to get the 2 kinds of Transaction to interoperate.

Why does Microsoft create this kind of problem?

John Davidson

Matthijs ter Woord

unread,
May 6, 2010, 12:21:27 PM5/6/10
to nhibernate-...@googlegroups.com
You'll need to make a resource manager then, so you can hook into System.Transactions. (DTC)

John Davidson

unread,
May 6, 2010, 12:39:58 PM5/6/10
to nhibernate-...@googlegroups.com
There appear to be some classes to work with DTC, but only for explicit transactions, not for implicit transactions, and no way to detect if TransactionScope has created an implied transaction.

My UnitOfWork implementation detects whether or not the application has created a transaction and if it has not then creates one (for database writes only - but should include queries as well...).

John Davidson
Reply all
Reply to author
Forward
0 new messages