Posts Tagged ‘clr in sqlserver’

A .NET Framework error occurred during execution of user-defined routine or aggregate “XXX”: System.InvalidOperationException: The context connection is already in use.

September 2nd, 2010

If you are working with CLR in SQLServer you might run into

A .NET Framework error occurred during execution of user-defined routine or aggregate "MyCLRMethod":   System.InvalidOperationException: The context connection is already in use.

I cannot say this is the solution every time but it worked for me.  Look down to the top of the stack (furthest down in the text) and you get some more clues.

Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "MyCLRMethod":
System.InvalidOperationException: The context connection is already in use.
System.InvalidOperationException:
 at System.Data.SqlClient.SqlInternalConnectionSmi.Activate()
 at System.Data.SqlClient.SqlConnectionFactory.GetContextConnection(SqlConnectionString options, Object providerInfo, DbConnection owningConnection)
 at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
 at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
 at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
 at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
 at System.Data.SqlClient.SqlConnection.Open()
 at Elvis.DatabaseCLR.UserDefinedFunctions.GetNextSampleID(DateTime readDate, Int32 meterID, Boolean useNextMeter)
 at Elvis.DatabaseCLR.UserDefinedFunctions.ConsumptionForNewMeter(Int32 sampleID, DateTime readDate, Int32 meterID, Decimal meterCoefficient, Int32 previousMeterConsumption, DateTime presentationDateOfSample)
 at Elvis.DatabaseCLR.UserDefinedFunctions.MyCLRMethod(Int32 sampleID, DateTime presentationDate, DateTime readDate, Int32 meterID, Decimal meterCoefficient, Boolean newMeter)

.

The MyCLRMethod calls two more methods until Open is called.  Playing around I found that I couldn’t open a connection if one already was open.  It sounds right since one of the deals with CLR in SQLServer is that we already have a connection.  The resolution is to tidy up the code so we only open the connection once, or open/closes it over and over again.

Tricks I used:
– When you get the error in your applicaiton copy the SQL string to your query editor and run it from there.
– Press F5 in Visual studio and have Visual studio deploy the CLR for you.  But use the query tool to run the query.  The error message is the same.
– Debug-through-printf, or “throw exception” in this case.  Drop in a throw-exception to find out exactly which Open call is failing.  The F5-and-wait and then run query takes a while.  If you have a logging possibility it is handy now.