The setup
We have a .net mvc 4 web api service layer, that is accessed via a web app, which gets data from a SQL Server 2008 database.
The problem
When more than 5 GET requests are being made to the web service at the same time, a 500 Interal Server Error
is returned to the web app. When I look closer at the .net error I see the following:
An error has occurred.
The connection was not closed. The connection's current state is open
The C# service code where the exception is being thrown:
public class ImageLinkRepository : IImageLinkRepository
{
private SqlConnection cnnSQL = new SqlConnection(...);
public IEnumerable<ImageLink> Get(String userId)
{
ImageLinks = new List<ImageLink>();
// open connection to DB
cnnSQL.Open();
try
{
SqlCommand cmmSQL = new SqlCommand("nVision_select_lcimagelinks_sp", cnnSQL);
cmmSQL.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter prmSQL = cmmSQL.Parameters.Add(new SqlParameter
{
ParameterName = "@LCIMGLINKUSERID",
Value = userId
});
SqlDataReader rdrSQL = cmmSQL.ExecuteReader();
if (rdrSQL.HasRows)
{
while (rdrSQL.Read())
{
ImageLinks.Add(new ImageLink
{
// set new ImageLink object's properties
imageTopicId = DBReader.SQLString(rdrSQL, "LCIMGLINKIMGTOPICID"),
id = DBReader.SQLInt(rdrSQL, "LCIMGLINKPK"),
recordId = DBReader.SQLString(rdrSQL, "LCIMGLINKRECORDID"),
text = DBReader.SQLString(rdrSQL, "LCIMGLINKTEXT"),
topicId = DBReader.SQLString(rdrSQL, "LCIMGLINKTOPICID"),
topicItem = DBReader.SQLString(rdrSQL, "LCIMGLINKTOPICITEM"),
url = DBReader.SQLString(rdrSQL, "LCIMGLINKURL"),
user = DBReader.SQLString(rdrSQL, "LCIMGLINKUSERID")
});
}
}
}
// close connection to DB
cnnSQL.Close();
return ImageLinks;
}
}
I have tried to manually close the connection if it is open and I have tried to only open the connection if its closed, neither of them work.
I must be doing something wrong, surely, the service should be able to handle simultaneous requests. What am I missing?