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

Optimising Connections & DataAdapters

Hi,

I just read the following in an old NG thread:

"when you use the adapter with a closed connection it will open it, do your
requested database access, and close it immediately. If you use the adapter
with an open connection the connection will continue to be open after the
adapter is done."

My DataLayer makes extensive use of DataAdapters to fill DataSets, and
currently each DataLayer function creates its own, new, SqlConnection, and I
do not explicitly open/close the SqlConnection, as the DataAdapter takes
care of that. As a result of reading the above quote, I think I can improve
performance by better use of SqlConnections.

Specifically, I'm now thinking about manually opening an SqlConnection at
the beginning of a DataLayer function before filling the DataSet, so that I
have the option of passing the still-open connection to another function
where it could be used to fill another DataSet, before passing this
still-open connection back to the original function to be manually closed.

Would this be (much?) more efficient than each separate function creating a
new SqlConnection?
Would such an approach create any new problems?

Thanks,

JON

PS Also: can anyone compare the performance of re-opening an
existing-but-closed SqlConnection with the cost of creating and opening a
brand new one?
Nov 18 '05 #1
4 1251
Jon:

That quote is referring to the actual mechanics of Connection pooling behind
the scenes. The DB will actually hold a connection open even though it's
'closed' in your code so that when you call Open again, it can reuse the
connection if possible.

Anyway, calling Open and Close in your code isn't going to do anything
different then letting the Adapter do it for you, at least in this regard so
there's no need to worry about that.

The only case where you might want to leave it 'open' is if you had a loop
for instance that was firing a ton of queries against the db back to back.
Instead of closing the connection only to immediately open it again, it may
make sense to open the connection at the beginning of the loop and close it
at the end (or in a finally block). Instances where this is a good idea
aren't that common though so as a rule of thumb,letting the adapter open and
close it for you is a good idea, and closing the connection as soon as you
are done with it, is always a good idea.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
Hi,

I just read the following in an old NG thread:

"when you use the adapter with a closed connection it will open it, do your requested database access, and close it immediately. If you use the adapter with an open connection the connection will continue to be open after the
adapter is done."

My DataLayer makes extensive use of DataAdapters to fill DataSets, and
currently each DataLayer function creates its own, new, SqlConnection, and I do not explicitly open/close the SqlConnection, as the DataAdapter takes
care of that. As a result of reading the above quote, I think I can improve performance by better use of SqlConnections.

Specifically, I'm now thinking about manually opening an SqlConnection at
the beginning of a DataLayer function before filling the DataSet, so that I have the option of passing the still-open connection to another function
where it could be used to fill another DataSet, before passing this
still-open connection back to the original function to be manually closed.

Would this be (much?) more efficient than each separate function creating a new SqlConnection?
Would such an approach create any new problems?

Thanks,

JON

PS Also: can anyone compare the performance of re-opening an
existing-but-closed SqlConnection with the cost of creating and opening a
brand new one?

Nov 18 '05 #2
Hi Bill,

Thanks for the reply! Just to check how this affects the code changes I'm
planning, do you think the code below:

(a) will work without explicitly opening and closing the passed
SqlConnection in DoFirstThing()

and

(b) will be more efficient than what I've been doing up to now, ie creating
a new SqlConnection in *every* DataLayer class?

Thanks,

JON
-----------------------------------------------------------
public class ABC
{

public static DataSet DoFirstThing(long articleID)
{
SqlConnection con = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
DataSet ds = new DataSet();
string sql = "SELECT ............";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

DataTable extraInfo = XYZ.DoSecondThing(articleID, ref con);
ds.Tables.Add(extraInfo);

try
{
return ds;
}
catch
{
return null;
}
}

}
-----------------------------------------------------------

public class XYZ

{

public static DataTable DoSecondThing(long articleID, ref SqlConnection con)
{
//NO LONGER NEED TO CREATE NEW SQLCONNECTION HERE
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "SELECT ............";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

try
{
dt = ds.Tables[0];
return dt;
}
catch
{
return null;
}
}

}
Nov 18 '05 #3
The connection object that you declare and instantiate is not the same as
the physical connection to the db, it's an abstraction of it that you can
manipulate. Once you use a connection to connect to the db, you can call
close on it, but it will stay open if you have pooling on from the DB's
perspective. That way if you call it again, it already has it avialable.

So the real issue is closing your client connections as soon as you are done
with them so they go back in the pool. If you open one connection and close
it , there's still one open from the DB's perspective. Say you open another
one, it will use it right. Now let's say you leave it open but then create
and open another one, it'll have to manage another coneection. But if you
closed the first one, it could reuse it.

So where you declare and instantiate them may have other consequences, but
Opening and closing them are probably the biggest and as long as you close
stuff as soon as you are done with it,it's doubtful you are going to run
into a problem. Also, there are a lot of other issues here and having a
connection local to your routine may make a lot more sense in terms of your
design and maintenance

Of the two , I prefer the first implementation for many reasons. Depending
on how you called this, passing in the connection may complicate things if
you made this multithreaded for one thing. You can easily work around this
but it still complicates things.

So in your two methods, the effect on the db is pretty much the same since
you are closing both of them as soon as you are done.

I think you're on the right track either way although my personal preference
is the first.

Good Luck,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_...center_pg1.asp
"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:e$****************@TK2MSFTNGP11.phx.gbl...
Hi Bill,

Thanks for the reply! Just to check how this affects the code changes I'm
planning, do you think the code below:

(a) will work without explicitly opening and closing the passed
SqlConnection in DoFirstThing()

and

(b) will be more efficient than what I've been doing up to now, ie creating a new SqlConnection in *every* DataLayer class?

Thanks,

JON
-----------------------------------------------------------
public class ABC
{

public static DataSet DoFirstThing(long articleID)
{
SqlConnection con = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
DataSet ds = new DataSet();
string sql = "SELECT ............";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

DataTable extraInfo = XYZ.DoSecondThing(articleID, ref con);
ds.Tables.Add(extraInfo);

try
{
return ds;
}
catch
{
return null;
}
}

}
-----------------------------------------------------------

public class XYZ

{

public static DataTable DoSecondThing(long articleID, ref SqlConnection con) {
//NO LONGER NEED TO CREATE NEW SQLCONNECTION HERE
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "SELECT ............";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

try
{
dt = ds.Tables[0];
return dt;
}
catch
{
return null;
}
}

}

Nov 18 '05 #4
That's helped a lot, much appreciated!

JON
Nov 18 '05 #5

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

Similar topics

1
by: flupke | last post by:
Hi, i'm trying to convert my java console app to a python gui. Now, the only problem i seem to have at the moment are the resizers for the layout. It seems that for the purpose of what i'm...
5
by: Mika M | last post by:
Hello! I have Windows Forms application form containing TextBoxes, six ComboBoxes, and DataGrid for details. I have created DataSet with needed tables, and created relations between tables, and...
4
by: Agnes | last post by:
Sorry for my question again. My form got the previous , next , top,bottom button and allow the user to move the records . Now, the table got 500 records, I need to do a features for the user to...
2
by: TD | last post by:
Hello All- We are in the process of converting the back end of our large MS Access application to PostgreSQL. My question today concerns linked tables and how their connections are managed by...
0
by: AC [MVP MCMS] | last post by:
So when you have two Web Parts that you connect that don't pass data using the same interface, you can create a custom transformer that does the translation for you. All the code samples I'm...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
2
by: paul | last post by:
Hi I have a .NET V2.0 Web application that uses Oracle database./ I have started to run load tests and found that 23 Connections to Oracle open (invactive) for 2 concurrent users. I am...
2
by: Stewart | last post by:
Hi People, I have been coding in vb.net and c# for last six months, I am new to coding and we are using 3 tier architecture. In the DAL in each and every method I open the connection and close...
0
MrMancunian
by: MrMancunian | last post by:
How to create a database connection without using wizards Introduction I've seen a lot of questions on the net about getting data from, and saving data to databases. Here's a little insight how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.