473,387 Members | 1,687 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,387 software developers and data experts.

SqlConnection opens tons of connections

We've got a serious problem. For invoicing I get the ID for a group of
assignments. I open one(!) SqlConnection, select all data rows of that
group, start one(!) transaction, and then I update the data rows of
every assignment in two tables. The strange thing is, that, even though
I've got only one SqlConnection object, a new connection is opened for
every single assignment, but neither the transaction nor the
SqlConnection gets closed or (re)opened. "using" doesn't help a bit.

Could someone please tell me what's wrong?

Oh, BTW: we still work with the .net framework 1.0, and we can't switch
to 1.1 for several reasons.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
6 1569
Jochen,

I would think that the connections are being opened as a result of
connection pooling. It's also possible that you are doing something wrong
in the code.

Can you show the code you are using that is doing this?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jochen Berger" <be*****@web.de> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl...
We've got a serious problem. For invoicing I get the ID for a group of
assignments. I open one(!) SqlConnection, select all data rows of that
group, start one(!) transaction, and then I update the data rows of
every assignment in two tables. The strange thing is, that, even though
I've got only one SqlConnection object, a new connection is opened for
every single assignment, but neither the transaction nor the
SqlConnection gets closed or (re)opened. "using" doesn't help a bit.

Could someone please tell me what's wrong?

Oh, BTW: we still work with the .net framework 1.0, and we can't switch
to 1.1 for several reasons.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Okay, here are the two main methods involved in this drama.

<code>
public void CalculatePreproduction(int vpKey)
{
// a database helper which contains the SqlConnection
DataCon dcon = new DataCon(helper.ConnectionString);
StringBuilder selectAssignments = new StringBuilder();
selectAssignments.Append("select * from PPTB1003 where VP_LNR =
").Append(vpKey);
// helper.LoadTable() loads the data from the DB into a DataTable
that's returned by the SQL statement
DataTable allAssignments = helper.LoadTable("PPTB1003",
dcon.Connection, selectAssignments.ToString(), false);
SqlTransaction trans = dcon.Connection.BeginTransaction();

try
{
// any assignment for that id found?
if (allAssignments != null)
{

Invoice invoice = new Invoice(FetchSalaryTable(DateTime.Now),
allDiscounts);

foreach (DataRow row in allAssignments.Rows)
{
// DataInContainer puts all data from the row in an object.
Invoice.Calculate() then calculates the prices.
// WriteInput finally writes the results to 2 different tables
WriteInput(invoice.Calculate(DataInContainer(row)) , dcon, trans);
}

trans.Commit();
}
}
catch(Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
// this Dispoce() contains a call of the SqlConnection.Close() method
dcon.Dispose();
}
}

private void WriteInput(AssignmentContainer data, DataCon dcon,
SqlTransaction trans)
{
// has this assignment got a valid assignment ID?
if (data.AssignmentKey > 0)
{
StringBuilder update1003 = new StringBuilder();
update1003.Append("update PPTB1003 set");
// [...]
// several fields, that need to be updated
// [...]
update1003.Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
SqlCommand cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1003.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;

try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing the data of assignment " +
data.AssignmentNo + ":\n" + exc.ToString(), exc);
}

StringBuilder update1004 = new StringBuilder();
update1004.Append("update PPTB1004 set SUPERVISOR_INST_SL =
").Append(data.SupervisorInstitute).Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1004.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;

try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing for consistency of appraisal
for assignment " + data.AssignmentNo + ":\n" + exc.ToString(), exc);
}
}
}
</code>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Please describe how you know there are multiple
connections being opened.

Are you mistaking multiple 'sessions' in SQL for
connections? I've seen MS SQL processing as many as 6
transactions simultaniously off of 1 connection.
-----Original Message-----
We've got a serious problem. For invoicing I get the ID for a group ofassignments. I open one(!) SqlConnection, select all data rows of thatgroup, start one(!) transaction, and then I update the data rows ofevery assignment in two tables. The strange thing is, that, even thoughI've got only one SqlConnection object, a new connection is opened forevery single assignment, but neither the transaction nor theSqlConnection gets closed or (re)opened. "using" doesn't help a bit.
Could someone please tell me what's wrong?

Oh, BTW: we still work with the .net framework 1.0, and we can't switchto 1.1 for several reasons.

*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
.

Nov 16 '05 #4
I had something like this going on with one of our internal apps, until I
decided
to close and dispose the command and it's connection explicitly:

cmd.Connection.Close(); // Explicit
cmd.Connection.Dispose(); // Implicit close

cmd.Dispose(); // Explicit, .NET 1.0 is No-op, 1.1? 2.0?

This might be overkill but worked. It cannot be translated directly
to your case, but might give some ideas. For example,
in private void WriteInput(AssignmentContainer data, DataCon dcon, ..,
it might help to call cmd.Dispose() or use using(cmd).
Also, it might be useful to be more explicit with closing the data objects.
Instead of relying on Dispose(), do trans.Close() - trans.Dispose(), and
dcon.Close() - dcon.Dispose(). They do not incure much overhead,
but the code is a bit more readable.

You might check also
http://blogs.msdn.com/angelsb/archiv...15/184479.aspx
for fake MARS.

Laura

"Jochen Berger" <be*****@web.de> wrote in message
news:Os****************@TK2MSFTNGP14.phx.gbl...
Okay, here are the two main methods involved in this drama.

<code>
public void CalculatePreproduction(int vpKey)
{
// a database helper which contains the SqlConnection
DataCon dcon = new DataCon(helper.ConnectionString);
StringBuilder selectAssignments = new StringBuilder();
selectAssignments.Append("select * from PPTB1003 where VP_LNR =
").Append(vpKey);
// helper.LoadTable() loads the data from the DB into a DataTable
that's returned by the SQL statement
DataTable allAssignments = helper.LoadTable("PPTB1003",
dcon.Connection, selectAssignments.ToString(), false);
SqlTransaction trans = dcon.Connection.BeginTransaction();

try
{
// any assignment for that id found?
if (allAssignments != null)
{

Invoice invoice = new Invoice(FetchSalaryTable(DateTime.Now),
allDiscounts);

foreach (DataRow row in allAssignments.Rows)
{
// DataInContainer puts all data from the row in an object.
Invoice.Calculate() then calculates the prices.
// WriteInput finally writes the results to 2 different tables
WriteInput(invoice.Calculate(DataInContainer(row)) , dcon, trans);
}

trans.Commit();
}
}
catch(Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
// this Dispoce() contains a call of the SqlConnection.Close() method
dcon.Dispose();
}
}

private void WriteInput(AssignmentContainer data, DataCon dcon,
SqlTransaction trans)
{
// has this assignment got a valid assignment ID?
if (data.AssignmentKey > 0)
{
StringBuilder update1003 = new StringBuilder();
update1003.Append("update PPTB1003 set");
// [...]
// several fields, that need to be updated
// [...]
update1003.Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
SqlCommand cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1003.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;

try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing the data of assignment " +
data.AssignmentNo + ":\n" + exc.ToString(), exc);
}

StringBuilder update1004 = new StringBuilder();
update1004.Append("update PPTB1004 set SUPERVISOR_INST_SL =
").Append(data.SupervisorInstitute).Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1004.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;

try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing for consistency of appraisal
for assignment " + data.AssignmentNo + ":\n" + exc.ToString(), exc);
}
}
}
</code>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5
The Problem is, that this method (CalculatePreproduction) is called only
ONCE. During the foreach loop one connection for each raw is opened (we
traced that on the SQLServer DB), and right after 100 the pool is empty,
and we get a time out. We never reach trans.Dispose().

I don't understand, why it opens a new connection, even though the ONE
transaction runs in the source with ONE SqlConnection object. If we
could tell the max size of the data set, we would set the pool size up
to that size. The only thing we know is, that there might be a few
thousand assignments in the set.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Well, after a few seconds we receive a timeout exception, and it tells
us, that there are no more connections left in the connection pool.

We also traced the whole action in the SQLServer (after each
update-group for one assignment there is an "audit login" entry, and
then a new connection gets opened), and we can see it in the process
info list of the Enterprise Manager.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #7

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

Similar topics

2
by: arran.pearce | last post by:
Hi, I am using .NET 2 (beta2) and are having problems with using a SqlConnection. I have created a test application that opens a sql connections, gets some basic data and then closes the...
4
by: arran.pearce | last post by:
Hi, I am using .NET 2 (beta2) and are having problems with using a SqlConnection. I have created a test application that opens a sql connections, gets some basic data and then closes the...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
16
by: ed_p | last post by:
Hello, I have implemented the singleton pattern for a class to hold a SqlConnection object that will be used thruout the application to create commands. My application is a simple Windows Form...
11
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...

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.