calling new SqlConnection() hangs program
Asked Answered
O

7

7

This one has me stumped. I'm not even trying to connect to a database. When this code gets to the line where I instantiate a new SqlConnection object, it just hangs there, not throwing an exception or anything. I've tried compiling it for 2.0. 3.5 and 4.0, and they all hang. Of course it works on my machine and yours, too. But I'm trying to run this code on a Windows Server 2008 x64 server, and it won't budge.

// example.cs
using System;
using System.Data;
using System.Data.SqlClient;

public class MainClass {
    public static void Main(string[] args) {
        Console.WriteLine("start");
        SqlConnection conn = new SqlConnection(); // hangs here
        Console.WriteLine("finish");              // never makes it here.
    }
}

compilation (2.0): c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe example.cs

Omar answered 6/12, 2011 at 23:10 Comment(11)
Yes, it hangs with or without a connection string.Omar
Is the behaviour the same if you target it at 32- or 64-bits?Sandie
try to pass a connection string to SqlConnection class.Ameline
This code does not hang for me.Distribute
@Rob: I tried compiling with the command line options /platform:anycpu, /platform:x64 and /platform:x86. They all hang.Omar
@FGraviton: I tried it with and without a connection string. That's not the problem.Omar
@p.campbell You're right, sorry. I skipped that part.Distribute
Try and find some more information about the environment its running on, Log Environment.Version to the console and report back? Also, does the code have anything else in it besides what you posted?Foreknow
Seems to me like this should just work. Does it matter if you declare conn first, outside Main? Or if you declare it as non static (ie create an instance of MainClass first? Just as an exploration of where it occurs and where not.Thirtytwo
@wal: I added the code from msdn.microsoft.com/en-us/library/system.environment.aspx to dump the system environment (with names changed to protect the guilty). It still hangs at "new SqlConnection()". And strange as it seems, no there is nothing else in this code, what I wrote is what is broken.Omar
@Omar It isnt supposed to stop it hanging, just trying to find out more information. So what was the output of Environment.Version? Should be something like: 2.0.50727.5448Foreknow
A
7

It's probably a broken performance counter which is the issue. I had this problem and I used Procmon to detect what happened. My .NET application hanged when it came to loading the registry keys for the performance monitor ".NET Data Provider for SqlServer"

I unloaded the counter with the following command to get it working:

unlodctr ".NET Data Provider for SqlServer"
Arlo answered 12/2, 2015 at 15:15 Comment(2)
Thank you - this helped me when I came across this problem!Myrlmyrle
".NET Memory Cache 4.0" is another one that may be impacted - unsure how these counters get corrupted.Install
E
2

Your installation have to be broken. The code doesn't really do anything vital at all, so there is no reason to hang there.

The SqlConnection constructor does this:

public SqlConnection() {
  this.ObjectID = Interlocked.Increment(ref SqlConnection._objectTypeCount);
  base();
  GC.SuppressFinalize(this);
  this._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance;
}

So, it increases a variable, copies it into a property, calls the base constructor, removes the object from the finaliser queue, and copies a reference. That's all.

The base (DbConnection) constructor does this:

protected DbConnection() {
}

So, there is nothing in here that actually does anything at all related to an actual database connection. All that is done when you actually open a connection.

Your program might just as well be hanging after the first Console.WriteLine call, and not even get as far as creating the SqlConnection object.

Execrate answered 6/12, 2011 at 23:37 Comment(5)
I doubt it's a broken installation, that would mean that ALL my .NET installations are broken (2.0, 3.0, 3.5 and 4.0). And it's not hanging on the Console.Write line, I tested for that.Omar
Isn't there something happening inside the getter of the SingletonInstance property?Distribute
I agree, a broken installation is my #1 guess. Its fairly quick and easy to re-install .Net I would try that first. Do you need .net2,3.5,and4? If Not, install just the one you need (reduce possible issues as much as possible)Foreknow
Interesting Problem, the other things to look for is the static constructors: SqlConnection, SqlConnectionFactory, DbConnectionClosedNeverOpened,...Tortoni
Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer.Execrate
C
1

Suggest 2 steps:

  • reset IIS to clear any connection pools. (Perhaps restart Windows?)
  • change the code to have a using statement:
  public static void Main(string[] args) { 
    Console.WriteLine("start"); 
    using (SqlConnection conn = new SqlConnection())
    {
          Console.WriteLine("middle");              
    }
    Console.WriteLine("finish");             
} 

Can any other app from that machine make any other SqlConnection objects?

It's obviously an environmental problem, as your posted code will work on any other machine. Suspect that it's gone beyond some tipping point, and the using will help defend against this in the future.

Chloromycetin answered 6/12, 2011 at 23:30 Comment(4)
Interesting. Why do you think "using" helps? Anyway, what does the parameterless ctor exactly do? Someone could check it with .NET Reflector.Distribute
I haven't rebooted the machine yet, it's a production server. But writing it the way you suggest, it never makes it to "middle".Omar
@CrackingWise: how about resetting IIS, was that possible?Chloromycetin
I totally restarted IIS (w3svc and iisadmin) on the server, but no dice. This isn't really surprising, because I'm running this exe from a command line, not an ASP.NET AppPool. And it's not a security issue either, because lots of other code will run, just not "new SqlConnection()".Omar
J
0

I had the same problem and it began to work after I 1. Changed the target framework from 4.0 to 3.5, and 2. changed the debug settings to x64 in visual studio.

Jab answered 18/7, 2012 at 14:29 Comment(0)
M
0

I had the exact same issue after upgrading a number of nuget packages. For me, the solution was:

  1. In Visual Studio go to "Tools" --> "Options"
  2. Search for "Web Projects" (under Projects and Solutions)
  3. Check the "Use the 64 bit version of IIS Express for web sites and projects
Mccleary answered 1/7, 2019 at 13:0 Comment(0)
C
0

I tried to unload the performance counter by executing the following command.

unlodctr ".NET Data Provider for SqlServer"

However, I got a message that the performance counter was not installed. Therefore, I ran the following command to reinstall the performance counter.

lodctr "C:\Windows\INF\.NET Data Provider for SqlServer\_dataperfcounters_shared12_neutral.ini"

This resolved the application hang-up.

Corporal answered 13/8, 2022 at 2:3 Comment(0)
E
-2

Solution found! I had the same problem on many PCs. Framework 4.5.2

Run this command as admin in CMD:

unlodctr ".NET Data Provider for SqlServer"

You may need to type it by hand as copy pasta doesn't work well with quotes.

Source:

http://askproblem.com/question/calling-new-sqlconnection-hangs-program/

I know this thread is old, but maybe someone else will get this error. It’s probably a broken performance counter which is the issue. I had this problem and I used Procmon to detect what happened. My .NET application hanged when it >came to loading the registry keys for the performance monitor “.NET Data Provider for SqlServer” I unloaded the counter with the following command to get it working: C: Windowsinf>unlodctr “.NET Data Provider for SqlServer”

Etymologize answered 2/7, 2015 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.