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

Basic question about database connection in ASP.NET

Hello,

Actually I think I should have had asked it long before, but somehow
I haven't.

Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to the database(Does not really matter but,
in case you need to know, either MSSQL or ODBC ones) a few times when it
loads to do the CURD things. How there's 3 ways proposed to do so:

1) Store the connection object in a class as static object. Each
time the page loads, it opens(in case connection disconnected) and wait
until the connection is avaliable. Not closing it and use it through the
session.

2) Store the connection object in the Page, open the connection when
the page loads, and close as the page renders.

3) New the connection object on "per use" basis. Declare the object
in the function blocks, Open and Close within try{}finally{} control block,
and let the GC frees it when the procedure exits.

Of the above designs, which one is most sound to you? And will that
impose heavier burden to the database? I don't have clear idea about it and
wish to clarify that before carrying on the next project. (Currently (3) is
selected as it seems neater)

Thanks for any inputs.

Regards,
Lau Lei Cheong
Jan 13 '06 #1
7 2450
Hi Lau Lei,

why not make a base class and create/"store" there the connection?

Regards, Roland

Lau Lei Cheong schrieb:
Hello,

Actually I think I should have had asked it long before, but somehow
I haven't.

Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to the database(Does not really matter but,
in case you need to know, either MSSQL or ODBC ones) a few times when it
loads to do the CURD things. How there's 3 ways proposed to do so:

1) Store the connection object in a class as static object. Each
time the page loads, it opens(in case connection disconnected) and wait
until the connection is avaliable. Not closing it and use it through the
session.

2) Store the connection object in the Page, open the connection when
the page loads, and close as the page renders.

3) New the connection object on "per use" basis. Declare the object
in the function blocks, Open and Close within try{}finally{} control block,
and let the GC frees it when the procedure exits.

Of the above designs, which one is most sound to you? And will that
impose heavier burden to the database? I don't have clear idea about it and
wish to clarify that before carrying on the next project. (Currently (3) is
selected as it seems neater)

Thanks for any inputs.

Regards,
Lau Lei Cheong

Jan 13 '06 #2
So you supports method 1?

All I've concerned is about server loading, and because the database is
running on the same server, I wish to strike the balance between web and
database server's resources conservation.

For method 1, it's good. But I have no idea
a) open a connection a lot of time;
b) have one connection persist through the session;
which of the above will place more burden to the server?

Since I don't know enough, I can't make a sure choice.

"Roland Müller" <ro************@flad.de>
???????:Oe**************@TK2MSFTNGP09.phx.gbl...
Hi Lau Lei,

why not make a base class and create/"store" there the connection?

Regards, Roland

Lau Lei Cheong schrieb:
Hello,

Actually I think I should have had asked it long before, but
somehow I haven't.

Here's the scenerio: Say we have a few pages in an ASP.NET
project, each of them needs to connect to the database(Does not really
matter but, in case you need to know, either MSSQL or ODBC ones) a few
times when it loads to do the CURD things. How there's 3 ways proposed to
do so:

1) Store the connection object in a class as static object. Each
time the page loads, it opens(in case connection disconnected) and wait
until the connection is avaliable. Not closing it and use it through the
session.

2) Store the connection object in the Page, open the connection
when the page loads, and close as the page renders.

3) New the connection object on "per use" basis. Declare the
object in the function blocks, Open and Close within try{}finally{}
control block, and let the GC frees it when the procedure exits.

Of the above designs, which one is most sound to you? And will
that impose heavier burden to the database? I don't have clear idea about
it and wish to clarify that before carrying on the next project.
(Currently (3) is selected as it seems neater)

Thanks for any inputs.

Regards,
Lau Lei Cheong


Jan 13 '06 #3
> Hello,

Actually I think I should have had asked it long before, but somehow
I haven't.

Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to the database(Does not really matter but, in
case you need to know, either MSSQL or ODBC ones) a few times when it loads
to do the CURD things. How there's 3 ways proposed to do so:

1) Store the connection object in a class as static object. Each time
the page loads, it opens(in case connection disconnected) and wait until the
connection is avaliable. Not closing it and use it through the session.
When you use a static connection object, you effectively have just a
single connection for all your requests (of *all* users). This will be
a bottleneck!
2) Store the connection object in the Page, open the connection when
the page loads, and close as the page renders.

In this scenario it's difficult to make sure the connection is closed
even when the normal program-flow is not followed.
3) New the connection object on "per use" basis. Declare the object
in the function blocks, Open and Close within try{}finally{} control block,
and let the GC frees it when the procedure exits.

This is how we do it. .Net has built-in caching for the "real"
connections, so there is no performance penalty in using Open and Close
a lot. And because the connection is closed soon, it's quickly
available for other requests, so you lower the total number of
concurrent connections to the database.
Here you can use finally to make sure the connection is closed as soon
as you are done with it (even on crashes).
Of the above designs, which one is most sound to you? And will that
impose heavier burden to the database? I don't have clear idea about it and
wish to clarify that before carrying on the next project. (Currently (3) is
selected as it seems neater)

Thanks for any inputs.

Regards,
Lau Lei Cheong

Jan 13 '06 #4
Hans Kesting wrote:
Hello,

Actually I think I should have had asked it long before, but somehow
I haven't.

Here's the scenerio: Say we have a few pages in an ASP.NET project,
each of them needs to connect to the database(Does not really matter but, in
case you need to know, either MSSQL or ODBC ones) a few times when it loads
to do the CURD things. How there's 3 ways proposed to do so:

1) Store the connection object in a class as static object. Each time
the page loads, it opens(in case connection disconnected) and wait until the
connection is avaliable. Not closing it and use it through the session.

When you use a static connection object, you effectively have just a
single connection for all your requests (of *all* users). This will be
a bottleneck!
2) Store the connection object in the Page, open the connection when
the page loads, and close as the page renders.


In this scenario it's difficult to make sure the connection is closed
even when the normal program-flow is not followed.
3) New the connection object on "per use" basis. Declare the object
in the function blocks, Open and Close within try{}finally{} control block,
and let the GC frees it when the procedure exits.


This is how we do it. .Net has built-in caching for the "real"
connections, so there is no performance penalty in using Open and Close
a lot. And because the connection is closed soon, it's quickly
available for other requests, so you lower the total number of
concurrent connections to the database.
Here you can use finally to make sure the connection is closed as soon
as you are done with it (even on crashes).
Of the above designs, which one is most sound to you? And will that
impose heavier burden to the database? I don't have clear idea about it and
wish to clarify that before carrying on the next project. (Currently (3) is
selected as it seems neater)

Thanks for any inputs.

Regards,
Lau Lei Cheong


I can only agree to that. We used to share a single connection (with
Synclock protection around all uses). One test project had twenty
concurrent threads all working against the database as fast as they
could (they were using the underlying business objects which talked to
the database). Took them four hours to complete all of the work.
Switched to opening database connections for each seperate operation.
The twenty threads now take ~7 minutes to complete exactly the same
work. And it does simplify matters - you don't have to know whether the
connection has already been opened, whether it's in use, etc. You just
grab a connection, use it and close it.

I would however recommend that you load the connection string once and
share that around, if obtaining your connection string may be a slow
operation (e.g. if it's stored encrypted in your config file). In our
solution, we have a single global function (called CloneConnection)
that just returns you a connection to the database. The connection
isn't open when it's returned, so that the time for which the
connection is opened is minimized (i.e. in cases where the connection
is attached to the command several statements before the command is
executed). Typical use would be (VB):

Dim cmd As New SqlClient.SqlCommand
cmd.Connection = Data.CloneConnection()
cmd.CommandText = "DMS.NewDataAvailable"
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandTimeout = 0
cmd.Parameters.Add(New
SqlClient.SqlParameter("@DataAvailable", SqlDbType.Bit, 1,
ParameterDirection.Output, False, 0, 0, "DataAvailable",
DataRowVersion.Current, False))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Damien

Jan 13 '06 #5
Dan
I also go with 3, its the way i do it. Although maybe you guys can help me
too, and hopefully this will help as well.

I approached this by making a database class, that class holds a string for
the sqlconnection, the connection info etc.

Then to make a dbconnection i just do something like

dbObj _db = new dbObj();

string sql = "select * from myTable";
_db.sql = sql;

_db.run();

//Then inside my dbObj class i hold a temporary store of the database info
that was returned and access it via
// a 2 dimensional array that acts like a map of a table, so the first part
relates to the row and the second to the column

string _myVar = _db.getData(0,1); //this would return a string of the data
in row 0, column 1 of my table that was returned

or to cycle through i would do this

for(int i=0; i<_db.MaxRows; i++)
{
Response.Write( _dbObj.getData(i,1); //this would loop through
row to row to row printing out just column 1 for example
}

And so my conenction opens, closes nice and quick and i have one central
place to manage all my database functionality.

Anyway my issue i have had is to do with concurrent users, doing this my way
it should not be possible that one user can effect another, however when 2
people were changing some data using the same form one of them set a var
that overrided the others.

So how did the connections somehow cross with the methodology above? As i
always make a new instance surely this is not possible? Have i missed
something?
Jan 13 '06 #6
Dan wrote:
I also go with 3, its the way i do it. Although maybe you guys can help me
too, and hopefully this will help as well.

I approached this by making a database class, that class holds a string for
the sqlconnection, the connection info etc.

Then to make a dbconnection i just do something like

dbObj _db = new dbObj();

string sql = "select * from myTable";
_db.sql = sql;

_db.run();

//Then inside my dbObj class i hold a temporary store of the database info
that was returned and access it via
// a 2 dimensional array that acts like a map of a table, so the first part
relates to the row and the second to the column

string _myVar = _db.getData(0,1); //this would return a string of the data
in row 0, column 1 of my table that was returned

or to cycle through i would do this

for(int i=0; i<_db.MaxRows; i++)
{
Response.Write( _dbObj.getData(i,1); //this would loop through
row to row to row printing out just column 1 for example
}

And so my conenction opens, closes nice and quick and i have one central
place to manage all my database functionality.

Anyway my issue i have had is to do with concurrent users, doing this my way
it should not be possible that one user can effect another, however when 2
people were changing some data using the same form one of them set a var
that overrided the others.

So how did the connections somehow cross with the methodology above? As i
always make a new instance surely this is not possible? Have i missed
something?


Hi Dan,

Without seeing the code for the dbObj class, it's going to be quite
tricky to debug this one. By the way, what benefits do you perceive
this method has over using, say, a dataset?

Damien

Jan 13 '06 #7
Thanks Hans and Damien. :)

The information provided by both of you is very helpful.

"Damien" <Da*******************@hotmail.com>
???????:11**********************@g47g2000cwa.googl egroups.com...
Hans Kesting wrote:
> Hello,
>
> Actually I think I should have had asked it long before, but
> somehow
> I haven't.
>
> Here's the scenerio: Say we have a few pages in an ASP.NET
> project,
> each of them needs to connect to the database(Does not really matter
> but, in
> case you need to know, either MSSQL or ODBC ones) a few times when it
> loads
> to do the CURD things. How there's 3 ways proposed to do so:
>
> 1) Store the connection object in a class as static object.
> Each time
> the page loads, it opens(in case connection disconnected) and wait
> until the
> connection is avaliable. Not closing it and use it through the session.
>

When you use a static connection object, you effectively have just a
single connection for all your requests (of *all* users). This will be
a bottleneck!
> 2) Store the connection object in the Page, open the connection
> when
> the page loads, and close as the page renders.
>


In this scenario it's difficult to make sure the connection is closed
even when the normal program-flow is not followed.
> 3) New the connection object on "per use" basis. Declare the
> object
> in the function blocks, Open and Close within try{}finally{} control
> block,
> and let the GC frees it when the procedure exits.
>


This is how we do it. .Net has built-in caching for the "real"
connections, so there is no performance penalty in using Open and Close
a lot. And because the connection is closed soon, it's quickly
available for other requests, so you lower the total number of
concurrent connections to the database.
Here you can use finally to make sure the connection is closed as soon
as you are done with it (even on crashes).
> Of the above designs, which one is most sound to you? And will
> that
> impose heavier burden to the database? I don't have clear idea about it
> and
> wish to clarify that before carrying on the next project. (Currently
> (3) is
> selected as it seems neater)
>
> Thanks for any inputs.
>
> Regards,
> Lau Lei Cheong


I can only agree to that. We used to share a single connection (with
Synclock protection around all uses). One test project had twenty
concurrent threads all working against the database as fast as they
could (they were using the underlying business objects which talked to
the database). Took them four hours to complete all of the work.
Switched to opening database connections for each seperate operation.
The twenty threads now take ~7 minutes to complete exactly the same
work. And it does simplify matters - you don't have to know whether the
connection has already been opened, whether it's in use, etc. You just
grab a connection, use it and close it.

I would however recommend that you load the connection string once and
share that around, if obtaining your connection string may be a slow
operation (e.g. if it's stored encrypted in your config file). In our
solution, we have a single global function (called CloneConnection)
that just returns you a connection to the database. The connection
isn't open when it's returned, so that the time for which the
connection is opened is minimized (i.e. in cases where the connection
is attached to the command several statements before the command is
executed). Typical use would be (VB):

Dim cmd As New SqlClient.SqlCommand
cmd.Connection = Data.CloneConnection()
cmd.CommandText = "DMS.NewDataAvailable"
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandTimeout = 0
cmd.Parameters.Add(New
SqlClient.SqlParameter("@DataAvailable", SqlDbType.Bit, 1,
ParameterDirection.Output, False, 0, 0, "DataAvailable",
DataRowVersion.Current, False))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Damien

Jan 14 '06 #8

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

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
0
by: Jim S. | last post by:
I'm having a horrible time simply inserting the date in a MySQL database through a Visual Basic program. I have a table, called "ORDERS"; "ID" is the primary key. I'm trying the insert the date,...
3
by: FC | last post by:
Hello all: I have a very basic question about oracle database structures. I am coming from MS SQLServer where I have three "databases", each database has three "tables". I can access any table...
1
by: Zvonko Tusek | last post by:
Is there a way to use access database on web host (I have a web hosting account) in visual basic. I want to make Visual basic client applications that connect to internet and work with access...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
5
by: Microsoft | last post by:
Hi, I have Visual Basic .net 2003 (Standard Edition) & SQL Server 2000 Developer Edition. When trying to create a connection in the server explorer from the .net IDE I get a number of problems;...
3
by: Miro | last post by:
Ok im a newbie to VB, and am having some trouble reading msdn help / or how it all links together. My books are either missing a step / or assume everything is set up correctly. I know about...
2
by: jayantim | last post by:
Hi ! I want to know the connection code for connecting visual basic and microsoft access. plese give the detail code for this connection. Actually I tried one code which is as follows: Dim cn...
0
by: tgallina | last post by:
I have just finished creating a simple fax program - that can transmit, connect to the SQL DB, and query the customers table. How would I go about looping throught the list of phone numbers to...
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,...
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,...

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.