473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

C# not closing database connections(?)

Hi all, I have just inherited a framework written in C# which is used
to store objects into a database via ADO.NET.

When I did a bulk insert the database started to run slow I logged into

MSSQL enterprise manager and looked at the running processes under
Management-->Current activity-->Process Info. The resulting screen shot

is shown below (sorry about the popups from the image hosting page!)

http://img153.imageshack.us/my.php?image=errorxp3.jpg

If I view the last command on each of these it is "COMMIT TRANSACTION".
What usually causes these sorts of problems???
TIA
Markus

Oct 27 '06 #1
5 2069
Ma*******@gmail.com wrote:
Hi all, I have just inherited a framework written in C# which is used
to store objects into a database via ADO.NET.

When I did a bulk insert the database started to run slow I logged into

MSSQL enterprise manager and looked at the running processes under
Management-->Current activity-->Process Info. The resulting screen shot

is shown below (sorry about the popups from the image hosting page!)

http://img153.imageshack.us/my.php?image=errorxp3.jpg

If I view the last command on each of these it is "COMMIT TRANSACTION".

What usually causes these sorts of problems???
It is not just the standard connection pool ?

Arne
Oct 27 '06 #2
Hi Arne, first of all to the group, sorry about the multiple posts;
Google news was timing out when I hit the submit button.

Re the connection pooling, these processes seem to stay for a very long
time *read 13 hours*. Also, as these appear our MSSQL server seems to
get more and more slow :(
thanks again
Markus

Arne Vajhøj wrote:
Ma*******@gmail.com wrote:
Hi all, I have just inherited a framework written in C# which is used
to store objects into a database via ADO.NET.

When I did a bulk insert the database started to run slow I logged into

MSSQL enterprise manager and looked at the running processes under
Management-->Current activity-->Process Info. The resulting screen shot

is shown below (sorry about the popups from the image hosting page!)

http://img153.imageshack.us/my.php?image=errorxp3.jpg

If I view the last command on each of these it is "COMMIT TRANSACTION".

What usually causes these sorts of problems???
It is not just the standard connection pool ?

Arne
Oct 27 '06 #3
Hi Markus, you could try to reindex the needed tables to speed them up (or
create indexes if none exists)
Here's the command
DBCC DBREINDEX('TableName')
Also, check for existing locks on the table using sp_lock
Best of luck,
Patrick

<Ma*******@gmail.comwrote in message
news:11********************@i3g2000cwc.googlegroup s.com...
Hi Arne, first of all to the group, sorry about the multiple posts;
Google news was timing out when I hit the submit button.

Re the connection pooling, these processes seem to stay for a very long
time *read 13 hours*. Also, as these appear our MSSQL server seems to
get more and more slow :(
thanks again
Markus

Arne Vajhøj wrote:
Ma*******@gmail.com wrote:
Hi all, I have just inherited a framework written in C# which is used
to store objects into a database via ADO.NET.

When I did a bulk insert the database started to run slow I logged into

MSSQL enterprise manager and looked at the running processes under
Management-->Current activity-->Process Info. The resulting screen shot

is shown below (sorry about the popups from the image hosting page!)

http://img153.imageshack.us/my.php?image=errorxp3.jpg

If I view the last command on each of these it is "COMMIT TRANSACTION".

What usually causes these sorts of problems???

It is not just the standard connection pool ?

Arne

Oct 27 '06 #4
How many clients were involved here? 1? 100? This seems a large number
if it was only the 1 client...

Connection pooling does (as suggested by the other poster) keep
connections hanging around... but it shouldn't cuase them to sit there
forever. The idea is that a [single | low number of ] connection(s)
is/are constantly re-used (per client)... but note that this can only
happen if each usage does it correctly.

I would be looking in the C# data code for some way in which:
a: the connection is not closed and disposed [contrary to popular
belief, this merely returns the connection to the thread pool for
re-use; connections *should* be closed when complete]
b: the connection is somehow made accessible outside of that code block
c: do you use different connection string / identity [context] settings
on each call?

For b, I really mean things like:
* does the data code give a reference of the connection to some (for
instance) static collection?
* does the connection reference get persisted on a form or similar?

Basically, if the connection isn't closed, and a reference is left "in
scope", then it will never get garbage collected. So it will never get
closed. This means that every time your app needs a connection it will
have to negotiate a new one with the server. The better solution is to
explicitely close / dispose the connection after *every* usage; when
you next open a connection with the same connection string you will
*probably* get the same physical connection back. If you don't "close",
but do let the reference go out of scope, you can often find a handful
of connections - which is where the previous .Net connection hasn't
been garbage collected yet, so still appears "in use"; this would
explain a low number - maybe into the tens for aggressive usage
[although /possibly/ (at a stretch) higher in a tight loop]

For hundreds of connections on a single client, I'd expect fubar.

Final comment: for a bulk insert, you can also use the specific bulk
insert tasks (descended from "bcp" if that means something...); this is
far more efficient; I would /generally/ use a dedicated bulk insert (of
sculpted, not raw, data), followed by a single stored-procedue to merge
the bulk table with the core data. Of course, not always an option.

Marc

Oct 27 '06 #5
Thanks Marc

Regards
Markus
Marc Gravell wrote:
How many clients were involved here? 1? 100? This seems a large number
if it was only the 1 client...

Connection pooling does (as suggested by the other poster) keep
connections hanging around... but it shouldn't cuase them to sit there
forever. The idea is that a [single | low number of ] connection(s)
is/are constantly re-used (per client)... but note that this can only
happen if each usage does it correctly.

I would be looking in the C# data code for some way in which:
a: the connection is not closed and disposed [contrary to popular
belief, this merely returns the connection to the thread pool for
re-use; connections *should* be closed when complete]
b: the connection is somehow made accessible outside of that code block
c: do you use different connection string / identity [context] settings
on each call?

For b, I really mean things like:
* does the data code give a reference of the connection to some (for
instance) static collection?
* does the connection reference get persisted on a form or similar?

Basically, if the connection isn't closed, and a reference is left "in
scope", then it will never get garbage collected. So it will never get
closed. This means that every time your app needs a connection it will
have to negotiate a new one with the server. The better solution is to
explicitely close / dispose the connection after *every* usage; when
you next open a connection with the same connection string you will
*probably* get the same physical connection back. If you don't "close",
but do let the reference go out of scope, you can often find a handful
of connections - which is where the previous .Net connection hasn't
been garbage collected yet, so still appears "in use"; this would
explain a low number - maybe into the tens for aggressive usage
[although /possibly/ (at a stretch) higher in a tight loop]

For hundreds of connections on a single client, I'd expect fubar.

Final comment: for a bulk insert, you can also use the specific bulk
insert tasks (descended from "bcp" if that means something...); this is
far more efficient; I would /generally/ use a dedicated bulk insert (of
sculpted, not raw, data), followed by a single stored-procedue to merge
the bulk table with the core data. Of course, not always an option.

Marc
Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written...
6
by: MAB71 | last post by:
There should be a way to copy an access database ( .mdb file ) without closing connections to it. Unfortunately the FileCopy statement in VB gives an error if users are connected to the db. But I...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
2
by: Do | last post by:
Hi, Is it better to objConn = Nothing or objConn.Close. I'm starting to get an error message that
7
by: darrel | last post by:
We're running into a problem on our new site. Once a week or so, our site goes down with an 'out of memory error'. Rebooting the web server fixes things. Googling the error doesn't return many...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
3
by: Andy G | last post by:
I have some code that hits a DB2 database from my ASP.NET application. I haven't been able to exactly nail it down but my connections are not getting closed for some reason. I look at the...
7
by: Martien van Wanrooij | last post by:
I have been faced a couple of times with the situation that I wanted to write a script and was worried about a too frequent opening and closing mysql connections. To give some examples: 1)I...
5
by: zacks | last post by:
If I close and dispose an ODBCConnection object, shouldn't the connection actually close? I have found that even after closing and disposing an ODBCConnection, the database it was connected to...
1
by: lee2732 | last post by:
I am running a website at GoDaddy where I have their least expensive plan that supports Linux/ColdFusion/mySQL. Apparently this plan supports a small number of active database connections (50). I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.