Perhaps, you are missing the point. Try to read my initial post again and see you if you can pick out the phrase where I say the code works, and has worked for years, correctly in many parts of the application. If it were simply a question of never closing the data reader it would have leaked connections from day one. Or am I missing something here? --- It's amazing how rude some people are to people that they don't know
Jason Pease
Posts
-
Inconsistent Connection Pooling Behavior -
Inconsistent Connection Pooling BehaviorGuffa wrote:
Of course the behaviour depends on what else is happening in the code. You are never closing the data reader, so it will be depending on garbage collections to finalize it. If there doesn't happen to be a garbage collection soon, it will be leaking the connection.
The garbage collection doesn't seems to be the issue based on the counters in perfmon, but I'm not sure how reliable they are.
Guffa wrote:
Perhaps even like a sieve?
No, more like a siv. Very very leaky.
Guffa wrote:
So much work for something that didn't end up very good anyway... Use the pre tag instead.
Nice tip. Can you tell I don't post that often? Thanks, Jason
-
Inconsistent Connection Pooling BehaviorI couldn't agree more, in fact that's precisely what I did to correct the problem. But I also need to explain why the previous code behaved as it did, both for formal justifications as well as my own curiosity.
-
Inconsistent Connection Pooling BehaviorI'm not sure if anyone will be able to help me with this problem without actually debugging the code, but I thought I'd give it a shot. Here's the situation. I have a fairly mature and stable C#/ASP.NET application that I inherited a few years back. I'm familiar with nearly all of the code, but there are few classes here and there that I've never had to deal with; I know they work and that's it. Late last night we came across a bug in which connections were getting leaked until the pool was saturated with the default 100 connections. It turned out that the connections were getting orphaned in a class that hasn't been touched in about five years. As strange as that is, the part that I really can't explain is that the method is called an several places in the application and it works correctly. However, when it is called in a page that was recently added to the application it leaks connections like a siv. If it can help, here's the offending method (underscores added for legibility).
public int GetUserID(String UserName) { ________// Create Instance of Connection and Command Object ________SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionStringUSR"]); ________SqlCommand myCommand = new SqlCommand("GetSingleUserID", myConnection); ________// Mark the Command as a SPROC ________myCommand.CommandType = CommandType.StoredProcedure; ________// Add Parameters to SPROC ________SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 100); ________parameterEmail.Value = UserName; ________myCommand.Parameters.Add(parameterEmail); ________if ((parameterEmail.Value != null) && (parameterEmail.Value != System.DBNull.Value)) ________{ ________________// Open the database connection and execute the command ________________myConnection.Open(); ________________SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); ________________if(dr.Read()==true) ________________{ ________________________if(dr.IsDBNull(0)) ________________________________return -1; ________________________else ________________________________return dr.GetInt32(0); ________________} ________________else ________________{ ________________________return -1; ________________} ________} ________else ________{ ________________return -1; ________} }
ThemyConnection.Close()
is missing, but theCommandBehavior
is set toCloseConnection
so it should be getting closed c -
Freah Meat =)This thread is a great example of why the message boards at code project stink. Bad. A new programmer asks for some advice about getting started and 1) he gets useless information like C++ is a superset C and std::string is better than char*, then 2) he gets caught between two experienced programmers trying to prove they're smarter than the next guy. I'm pretty sure that when aqzman_ originally posted his question he wasn't interested in finding out that Christan Graus - Microsoft MVP - C++ thinks that fat_boy is an idiot. Let's do our best to bring some value to these boards, not defend our egos.
-
Freah Meat =)In my opinion George's advice is the best. Most of the other posts were "Language X is the one I use, so you should learn it too". You would be well advised to ignore those types of suggestions. The only suggestion I would add to George's post is that if you want to learn application programming it may be better to start with a non strongly typed language like old VB (not VB.NET) or php or something like that. That way you can learn more rudimentary programming concepts first, then you can move onto types when you feel comfortable.
-
Problem changing class field from string to ClassI am maintaining an application that uses the XmlSerializer to import and export data. I would like to make some modifications to the internals of the application, but I need to maintain backward compatibility with older XMLs that customers may have. Essentially I'm trying to change [XmlAttribute] public string CashFlowType; , which contains the name of the type, to this [XmlAttribute] public CashFlowType CashFlowType; , which would be an object that encapsulates all of the details of the object instead of just the name. The problem is that the Serializer can't put the complex type CashFlowType into an XmlAttribute, and rightly so. The ideal solution would be to use [XmlElement] and write out the fields that I need to deserialize, but for the sake of maintaining backward compatibility, I cannot change the attribute to an element. I have tried setting all of the fields of CashFlowType to [XmlIgnore] and I overrode ToString() in the hopes that the serializer would put the result of ToString in the attribute but it throws an exception just the same. If anyone has any ideas it would be greatly appreciated. Thanks, Jason
-
C# Memory Leak MysteryS. Senthil Kumar wrote: IIRC, the underlying connection is not closed and the managed SqlConnection Maybe it's because my profiler (devPartner Studio) doesn't allow me to see it, but this space only shows up as "Unreachable Objects"; it doesn't show me the type of object. I would have expected to see a pooled connection with some type of indicative name, not Unreachable Objects. This brings up the question though of where a pooled connection is kept. Seems I can utilize the same pooled connection from two different applications (right?), I would have expected it to not be included in the memory of my application space, but instead somewhere else. Maybe I need to study this more thoroughly. S. Senthil Kumar wrote: How did you measure? Task Manager? DevPartner tells me how much memory it is profiling. I've verified this number with the .NET counters and it seems to be reliable. Thanks for your help, Jason.
-
C# Memory Leak MysteryI'm trying to understand how the .NET framework manages memory and I've been using various profiling tools but there are still a few things that don't add up that maybe someone here can help me with. I've been using primarily devPartner studio with a sample project just to test a few things out. Let's start with the simplest of examples. If I create a loop that calls a dummy function that does nothing, something just to occupy memory, after the garbage collector runs a small amount of memory remains occupied. Granted it's only 324 bytes, but it should be 0 bytes and I need to figure out why. DevPartner Studio tells me I have 7 unreachable objects for a total of 208 bytes. My question is that if the garbage collector just ran, why are there any unreachable objects left? The rest of the memory (116 bytes) is occupied by an object array named "String Table" that I presume .NET was using to keep track of the strings I was allocating to consume memory. Seems the string array is no longer needed it would make sense if it were garbage collected, but if .NET wants to keep it around it really not a problem. At least that behavior is explainable. The 208 bytes of unreachable objects is a bigger problem. The problem becomes much more significant when I create an instance of a System.Data.SqlClient.SqlConnection and open it. After opening the connection, closing it, and garbage collection, I'm left with about 185K of memory that couldn't be cleaned up, about 150K of which is unreachable objects. I haven't yet tested the impact of opening multiple connections or repeatedly opening and closing the same connection. Any ideas will be greatly appreciated. Thanks, Jason
-
VS hangs when loading web projectI found out what was happening but not why. The web.config file contains a connection string to a SQL Server database that was not reachable from the laptop and the project was hanging because it was waiting for a response from SQL Server. I corrected the connetion string and VS was able to open the project. The question remains however why VS was even trying to open the connection in the first place. The connection string is accessed at different points in the application, but none of that code should have been executed while VS was opening the project. If anyone has any ideas I'd be interested in hearing them. Thanks, Jason.
-
VS hangs when loading web projectI have a simple web project that has VSS integrated. The project opens and runs fine on all of our development machines, but when I download it from VSS to our laptop VS.NET often hangs when trying to open the only web project in the solution. I say "often" because at times it opens normally. The csproj solution is where the solution expects it to be, //localhost/WebProject/WebProject.csproj, but IIS never serves the file. In fact, if I put the web path of the project file show above into IE, IE also hangs. There seems to be a connection because if I restart IIS, both VS.NET and IE immediately return messages that the web server is not running. And ideas would be greatly appreciated, Jason
-
ViewState vs local variableDoes anyone know how expensive calls to extract objects from the ViewState are? I'm wondering if it is worthwhile to cache the objects I extract from it into local variables in my code behind. To give you an example, this is what I currently do to store the previous sort column of a datagrid. In order to shield the rest of the page from the implementation I use to store the value I use a private property.
private string _previousSortExpression; private string previousSortExpression { _____get _____{ __________if( this._previousSortExpression == null ) __________{ _______________this._previousSortExpression = (string)ViewState[ "_previousSortExpression" ]; __________} __________return this._previousSortExpression; _____} _____set _____{ __________this._previousSortExpression = value; __________ViewState[ "_previousSortExpression" ] = value; _____} }
This method prevents multiple calls to get something from the ViewState, but I'm wondering if it's worth the time and space to add this extra logic. Any thoughts are appreciated. Jason -
Yet another fun C-puzzleDoes this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason
-
Easy User Control Postback QuestionThanks for the tip! That was my problem. The only change I made was that if the combos already have an element I skip the loading process. In this case this is acceptable because the content is static (0-23 and 0-59). Thanks again for the help!
-
Easy User Control Postback QuestionI hadn't touched the EnableViewState because it's supposed to default to true, but even after explicitly setting it to true it still does not work correctly.
-
Easy User Control Postback QuestionI created a simple User Control that consists of a few DropDownLists and Labels for a user to select a specific hour and minute. The control loads and functions in the page correctly, but I'm not able to get it's value on postback. The control is used in the EditItemTemplate of a DataGrid and in the DataGrid_UpdateCommand event I'm trying to get the value the user selected. I perform a FindControl like this. DataGrid dg = (DataGrid)source; int editItemIndex = dg.EditItemIndex; DataGridItem dataGridItem= dg.Items[ editItemIndex ]; MyUserControl myUserControl = (MyUserControl) dataGridItem.FindControl( "myUserControl" ); However, when I try to access the properties of the user control, they are always the initialized values, not the ones the user selected. Does anyone see what I'm doing wrong here? Thanks, Jason