5

In the following code the connection string password is intentionally incorrect to cause a SqlException on opening the connection.

        while (true)
        {
            try
            {
                SqlConnection conn = new SqlConnection("Server=localhost;Database=northwind;UID=sa;PWD=b;");
                conn.Open();
            }
            catch (Exception ex)
            {
                try
                {
                    Console.Write(ex.GetHashCode() + " : ");
                    ex.Data.Add("MyKey", "LogData");
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex2)
                {
                    Console.WriteLine(ex2.Message);
                }
            }
            Thread.Sleep(500);
        }

For logging purposes, I want to add values to the Exception's Data dictionary. After the first exception, I find that most subsequent exceptions are the exact same instance of the SqlException class (verified with GetHashCode()) and already have my Data dictionary entries. This causes a ArgumentException to be thrown due to the key already existing.

34826618 : Login failed for user 'sa'.
34826618 : Item has already been added. Key in dictionary: 'MyKey'  Key being added: 'MyKey'
34826618 : Item has already been added. Key in dictionary: 'MyKey'  Key being added: 'MyKey'
34826618 : Item has already been added. Key in dictionary: 'MyKey'  Key being added: 'MyKey'

I realize that I can change it to use the keyed setter on the Data property, which would add or replace the entry, but my concern is that reuse could result in invalid Data entries being logged. Also, it seems odd that an exception being thrown from a new code execution would be the same instance as a previous exception thrown.

Any insight? Can you reproduce this?

P.S. If you don't think adding entries to the Exception's Data dictionary is appropriate, I'd be glad to hear your thoughts, but it doesn't change the core of the question about why the SqlException instance is being reused.

Joshua Marble
  • 540
  • 3
  • 9
  • 2
    Why _can't_ it be the same instance? Did you read anywhere that it's guaranteed to be a new instance each time? – John Saunders Nov 03 '11 at 14:57
  • I never said it can't be, and I have not found any documentation about its uniqueness, but I am interested in the reason that it is the same or if there are any situational causes of it. I don't think it's very intuitive or expected since all the developers I've talked to about it think it's strange. – Joshua Marble Nov 03 '11 at 15:05
  • That which isn't promised - is not promised. It's the same instance because they felt like it, and because your code depends on it. If your code didn't depend on it, then you would never have noticed. – John Saunders Nov 03 '11 at 15:12
  • 2
    Fine, you don't like my question. The fact that I noticed the behavior is irrelevant and the question still stands. I never said it was wrong or that I deserved it to function differently. I noticed behavior that I (and others) deemed strange and asked for insight on SO. Perhaps there is a logical reason for this behavior that someone can provide. – Joshua Marble Nov 03 '11 at 15:23
  • 2
    You're welcome to ask, of course (I haven't downvoted or voted to close). I just assume that the answer would be "because we felt like it", or maybe even "because we felt it might improve performance". Neither answer is of much practical value, nor does it inform you about whether other parts of .NET might not do the same thing. – John Saunders Nov 03 '11 at 15:51
  • 1
    +1. I think this is a valuable observation, because it reveals some inner behaviour that is not obvious, if not unexpected, from seeing the code. A *new* connection that re-throws an *old* exception? Bizarre! – Gert Arnold Nov 03 '11 at 21:26
  • @GertArnold - Part of my motivation in posting the question was merely pointing out the observation for the interest of others, and discovering if this was something that everyone already knew and I just missed the memo. So far it seems that regardless of the reason, it is unexpected and doesn't seem to be common knowledge. – Joshua Marble Nov 04 '11 at 16:26
  • 2
    BTW, just found https://connect.microsoft.com/VisualStudio/feedback/details/522506/connection-pool-returns-same-exception-instance-from-two-threads-using-identical-bad-connection-string. It seems this was reported some time ago. – John Saunders Nov 05 '11 at 21:21
  • Good find. I had searched for similar problems but didn't find anything. – Joshua Marble Nov 09 '11 at 15:14

1 Answers1

0

As said by others, it's probably all about performance.

To solve your problem, I suggest you clear the dictionary when you're done with logging its contents or when you stop rethrowing it.

Medinoc
  • 6,577
  • 20
  • 42