"TransactionScope nested incorrectly." error when executing a query in a different database.

Topics: Core, Customizing Orchard, General, Troubleshooting, Writing modules
Sep 26, 2012 at 2:35 PM

Hello,

I have made a module to have authentication via our DMS system, which runs on an Oracle database. I learned that I need to suppress the ambient Orchard scope by using "using (new TransactionScope(TransactionScopeOption.Suppress))" and putting my code there. 

I have done this before and it has always worked in other modules that I made, but for some reason I get the error "TransactionScope nested incorrectly." this time.

I have been searching for a while now to see what the problem could be, but I can't find much about it. Does anyone have any idea what could be wrong?

This is my code:

using (new TransactionScope(TransactionScopeOption.Suppress)) 
{
	if (string.IsNullOrWhiteSpace(this.ConnectionString))
	{
		// Use normal Orchard authentication when connectionstring is not available
		return null;
	}

	const string Query = "Query here";
	using (var connection = new OracleConnection(this.ConnectionString))
	{
		using (var command = new OracleCommand(Query, connection))
		{
			command.Parameters.Add(new OracleParameter(":username", usernameOrEmail));
			using (var adapter = new OracleDataAdapter(command))
			{
				adapter.Fill(dataTable);

				if (dataTable.Rows.Count == 0)
				{
					return null;
				}

				userRow = dataTable.Rows[0];
			}
		}
	}
}
The error occurs at the last closing bracket, when the Dispose gets called on the transactionscope.

Sep 26, 2012 at 4:17 PM

Hmm... when I add a try and catch around everything, to ignore the error and only log it, I get the following error: "Cannot access a disposed object. Object name: 'TransactionScope'.". The error occurs at Orchard\Data\TransactionManager.cs, Line: 43 (code on that line: "_scope.Complete();"). Did my disposing of a new transactionscope also, somehow, dispose the main Orchard transaction too early (I don't really see how that is possible though)? I don't really understand what is happening here...

Sep 27, 2012 at 11:40 AM

I am also experiencing this problem.

The weird thing is that when I add the Supress-thingie it works. However, weeks later I get complaints the module is broken, then I have to remove the Supress to make it work again.

I don't understand why.

Sep 28, 2012 at 9:23 AM
Edited Sep 28, 2012 at 9:28 AM

Ok, I took another good look at it today and finally figured out the problem.

At first I thought it was because of all the Oracle stuff I do, but when I removed all of it, the problem persisted. The problem was this piece of code:

if (string.IsNullOrWhiteSpace(this.ConnectionString))
{
	// Use normal Orchard authentication when connectionstring is not available
	return null;
}

I had totally forgotten that my property "ConnectionString" does something with Orchard settings. It looks like this:

private string ConnectionString
{
	get
	{
		var settingsPart = this.Services.WorkContext.CurrentSite.As<MyModuleSettingsPart>();
		return settingsPart.ConnectionString;
	}
}

So of course this causes problems, since it needs the ambient Orchard transaction, it won't work properly if I put it inside a new transactionscope with the surppress option.  I moved that if statement outside of the transactionscope block and that fixes my problem.

Sep 28, 2012 at 9:33 AM

Yeah I was thinking about this today. I did the same and it worked.