4

I am tracing a bug in a library I did not write myself. When using this library with ASP.NET, I get db connection errors because the SQLConnection seems to get closed when a second Connection is being opened with the same connection string.

Is this documented behaviour? Can opening a new SQLConnection with the same connection string close another SQLConnection object?

From debugging that seems to be the most likely cause for my problems, but I could not find anything on the web to support my theory.

4
  • simple way to find this is do a search on the word new and it's probably being created again.. this connection can be a property or static string that's opened and closed by your code.. only create new once.. or add code that will check the Connection.State.. post code where you are creating it.. and the code where it's being closed or the Connection State is closed Commented Dec 19, 2011 at 14:56
  • 1
    That should work fine. The issue is more likely to be re-using connections across requests. Commented Dec 19, 2011 at 14:58
  • @DJ KRAZE: I already found the part where a second connection is being opened with the same connection string. As I wrote, I want to know if its normal that this closes the first connection. Commented Dec 19, 2011 at 14:59
  • lynn has stated exactly what I was trying to say.. great short and sweet answer @Lynn UPvote definitely.. Commented Dec 19, 2011 at 15:01

3 Answers 3

3

No, what will happen is that when you call the SqlConnection.Open() method, even with the same connection string parameters, it will do one of two things: either reuse an unused connection from the pool, or create a new connection. Either way, you will result in non-conflicting SPIDs for SQL Server.

Sign up to request clarification or add additional context in comments.

Comments

3

Is this documented behaviour?

No.

Can opening a new SQLConnection with the same connection string close another SQLConnection object?

No.

Note that unless you've modified the settings, SQL Server permits 32,767 simultaneous connections. But even then, that would not explain the behavior that you are seeing.

Comments

0

In short, opening a new connection with the same connection string will not close your existing connection. However... reusing the already existing object by creating a new reference to a new connection will destroy the connection.

3 Comments

Sorry, I don't understand the last sentence, can you please explain what you mean? How can you reuse an object by creating a new reference to a new object?
Yes, if I have: SqlConnection conn = new SqlConnection(someconnstring); conn = new SqlConnection(someconnstring); ... then my original connection object will be overwritten with the new one.
@Lynn Crumbling: That is not reusing the existing object. Additionally, that won't necessarily terminate the connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.