Link to home
Create AccountLog in
Microsoft SQL Server 2005

Microsoft SQL Server 2005

--

Questions

--

Followers

Top Experts

Avatar of softwarea
softwarea

CLR: Context connection is already in use
Hi,
the <new SqlConnection("context connection=true")> thing inside of MS SQL 2005 triggers and stored procedures (CLR) is really a mess for me. Here I have another problem:

Suppose you have a trigger which opens an SqlConnection like <SqlConnection con = new SqlConnection("context connection=true")>. Easy, no problem.

Now, inside this trigger you might want to call a stored procdure which needs an SqlConnection for it's purposes too. Problem: You can't open this second SqlConnection, because the one which was opened in the trigger is still open (and for some reason I want to keep it opened).

I tried to figure out the connections status in the stored procedure:
        if (con.State != ConnectionState.Open)
            con.Open();
But con.State is always "close"... And when con.Open() is executed, I receive the known exception (context is alredy in use).

What can you do about that? Maybe there is a way to pass a connection to a stored procedure so it does not have to open one of it's own? Just an idea...

Thanks as always for any help!
Ingmar

SQL 2005 Express, CLR

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of NightmanNightman🇦🇺

Have a look over here: http://www.sqlmonster.com/Uwe/Forum.aspx/sql-server-clr/27/Check-if-connection-is-open

It is actually a support forum, but there is a long, involved and interesting discussion on the subject.

Avatar of softwareasoftwarea

ASKER

Hello Nightman,
thanks for the link.
Unfortunately those guys did not seem to solve the proplem either...:-(

Avatar of NightmanNightman🇦🇺

Except the discussion was interesting - you should be using static methods internally. It also suggests that you can't do this with the same connection internally, and that the context connection cannot be re-used - it is a singleton resource. Since you already have a connection open, it cannot be re-used.

What are you actually trying to do in the code? There could be a workaround.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


I'm not doing anything great.
It's just a trigger that collects a bunch of data rows (therefor needs an open connection) and then calls a stored procedure for each row that was found. The stored procedure needs an open connection for it's own purposes.

Of course there is an easy work around: I could close the connection in the trigger, before calling the stored procedure. The reason why I do not want to do that is that I have a lot of stored procedures (and triggers) that are all connected in some way. I'm just too lazy to make sure that I put a con.Close() at every place where it might be neccessary now or in the future. So I hoped I could solve this problem in the beginning of every trigger/stored procedure by checking if the connection is already open...and then just use it.

ASKER CERTIFIED SOLUTION
Avatar of NightmanNightman🇦🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

I think you are right. Unfortunately I will have to close the connection as soon as I do not really need it any more.

If you do not mind, I leave the question open for one or two days. Maybe someone else has a great idea. If not I will accept your last posting. Hope that is ok with you.

Thanks for your help! And good night man, I mean Nightman ;-)

Avatar of NightmanNightman🇦🇺

The last couple on this site all ended with the same result - I have been googling to see an alternative, but nothing yet. Leave it open if you like - another opinion can never hurt.

Good night, and good luck.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.

Microsoft SQL Server 2005

Microsoft SQL Server 2005

--

Questions

--

Followers

Top Experts

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.