Throwing an Exception leaks connections


When an exception is thrown the TransactionManager cancels the transaction which then bypasses the scope.complete. This includes the OrchardSecurityException ( I had mine in a driver for 'members only')

The scope is disposed, but by not calling the complete the connection is leaked. I've tried moving the complete to outside the if (!_cancelled) and this resolves the problem, however I appreciate this will have unwanted effects. I've only been working on Orchard for a few months, so hoping some of you experts out there have an opinion on the matter
    void IDisposable.Dispose() {             if (_scope != null) {                 if (!_cancelled) {                     Logger.Debug("Marking transaction as complete");                 _scope.Complete();                 }                  Logger.Debug("Final work for transaction being performed");                 _scope.Dispose();                 Logger.Debug("Transaction disposed");             }         }
I'm using SQL Server 2005, could this be related to https://nhibernate.jira.com/browse/NH-2107

Thanks in advance


file attachments

Closed Mar 6, 2013 at 7:47 PM by sebastienros
Fixed in 1.x as it's no more using TransactionScopes


sebastienros wrote Oct 6, 2011 at 4:55 PM

Creating an issue for that so we don't miss it

Jeb746966 wrote Oct 6, 2011 at 5:45 PM

I see the impact has been set to low?

Any exception which the TransactionFilter picks up creates a leak.

BertrandLeRoy wrote Oct 6, 2011 at 6:46 PM

The impact has not been set to anything: low is just the default. This bug has not been triaged yet.

sebastienros wrote Oct 6, 2011 at 10:23 PM

If we call Complete() like that, there is no way to cancel the transaction !
What version of Sql Server are you using ?

Jeb746966 wrote Oct 7, 2011 at 4:36 AM

I'm using version 2005, which might relate to https://nhibernate.jira.com/browse/NH-2107

As for not being able to cancel the transaction...that thought had occurred to me,

BertrandLeRoy wrote Oct 12, 2011 at 7:20 PM

This is a nHib+SQL 2005 bug.

Jeb746966 wrote Dec 14, 2011 at 12:53 PM

This problem is bigger than exceptions. I managed to rework some of the code to not through security exceptions and I thought I'd laid this issue to rest settling for the occasional connection leak.

However, I've recently enabled RSS feeds and Searching on the site and now the connection leaks are back with a vengeance.

Having looked through the code, I can see calls to TransactionManager.Cancel() in lots of areas, including the slugify action on the ItemController.

Is there any suggestions for getting this working ? apart from upgrading to SQL Server 2008 ?

I've tried adding an event to the Transaction Manager and passing in a call to rollback an explicit transaction created in the sessionlocator, however I ran into multiple transactions and a request to run the dristributed transaction server.

maxwebster wrote Jan 23, 2012 at 3:58 PM

Update: I found that MsSql2008 is hardcoded in Orchard.Source.1.3.9\src\Orchard\Data\Providers\SqlServerDataServicesProvider.cs

public override IPersistenceConfigurer GetPersistenceConfigurer(bool createDatabase) {
        var persistence = MsSqlConfiguration.MsSql2008;
        if (string.IsNullOrEmpty(_connectionString)) {
            throw new ArgumentException("The connection string is empty");
        persistence = persistence.ConnectionString(_connectionString);
        return persistence;
Could this be a clue?

Jeb746966 wrote Feb 28, 2012 at 11:00 AM


This issue it causing major headaches with the Orchard sites I'm working with. I've tried various approaches but it's feeling like a game of Wack a Mole.

I've attached a file containing 2 classes ( TransactionManager and SessionLocator) which I'm hoping someone could sense check for me.

According to all the Nhibernate posts I've read, this is the proper way to handle the issue, but they're not targetting Orchard's Transaction Manager approach

By not calling commit on the explicit transaction but still calling Scope.Complete, I'm able to not commit those transactions which need to rollback. My only concern is I'm not seeing an explicit rollback in the SQL Profiler.

Grateful for any comments.

Thanks in advance

sebastienros wrote Feb 28, 2012 at 5:10 PM

@Jeb746966 Are you using SQL 2005 ?

Jeb746966 wrote Feb 29, 2012 at 4:55 AM

Hi @sebastienros
Yes I am using SQL Server 2005. Is there any chance you could take a quick look at the file I uploaded which contains the 2 classes ?
I did a @@transcount after generating an exception and it was still showing 0 transactions open, but that might just have been due to no updates inside an uncommitted transaction.

Could really use you or @BertrandLeRoy's opinion on the changes I propose.

Thanks in advance

Jeb746966 wrote Feb 29, 2012 at 4:57 AM

Sorry, I forgot to mention that changing GetPersistenceConfigurer to be 2005 doesn't make any difference.

Jeb746966 wrote Mar 8, 2012 at 2:52 PM

I have a fix for this now which correctly rolls back the transaction without leaking a connection (SQL server 2005).

I'll need to test the fix for SQL 2008 before submitting a change set.

Thanks to all for their help

AimOrchard wrote Dec 8, 2012 at 3:08 PM

I'm running into issues with our main site leaking connections.

We're running Orchard v1.6 and SQL Server 2008R2, could it be related to this issue?