Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > To connect to a database.

Reply
Thread Tools

To connect to a database.

 
 
jc
Guest
Posts: n/a
 
      01-11-2008
Hi there:

Reading from internet i found that the best way to use a database is
to connect and disconnect on every single database request, so if you
are loading a listbox manually from a database and on the same page,
loading a grid from the database, then the best way is to connect to
database, load the listbox, disconnect, connect again, load the grid
and disconnect.

what is the cause because to reconnect many times is more
efficient?.

I tried running some test on about it,connecting once on page request
is almost 20% more fast that connecting many times.

Also, i checked the number of session open on oracle and in both
method did keep the same number of session. In any case, aspnet will
keep many connections opens (persistent?). May be this method of open
and close for request is efficient with sqlserver but other database.

 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      01-11-2008
20 % from what ? Even 100 % wouldn't mean anything. If it's small enough
this is anyway non visible to the end user...

This is not really "more efficient". It just much simpler and it guarantees
that connection are released as soon as possible...

If you keep the connection opn :
- any failure to close the connection could cause problem
- it could be closed later than needed making this connection not available
even though not used
- if you have some processing between those openings, this particular
connection is not available even though not used at this time...

As most often this is a matter of trade off... The thing to check is if
your DB supports connection pooling...

--
Patrice

"jc" <(E-Mail Removed)> a écrit dans le message de news:
b5a3f06b-7f58-429a-95ff-e3b3dca3cff5...oglegroups.com...
> Hi there:
>
> Reading from internet i found that the best way to use a database is
> to connect and disconnect on every single database request, so if you
> are loading a listbox manually from a database and on the same page,
> loading a grid from the database, then the best way is to connect to
> database, load the listbox, disconnect, connect again, load the grid
> and disconnect.
>
> what is the cause because to reconnect many times is more
> efficient?.
>
> I tried running some test on about it,connecting once on page request
> is almost 20% more fast that connecting many times.
>
> Also, i checked the number of session open on oracle and in both
> method did keep the same number of session. In any case, aspnet will
> keep many connections opens (persistent?). May be this method of open
> and close for request is efficient with sqlserver but other database.
>



 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      01-11-2008
"jc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Reading from internet i found that the best way to use a database is
> to connect and disconnect on every single database request, so if you
> are loading a listbox manually from a database and on the same page,
> loading a grid from the database, then the best way is to connect to
> database, load the listbox, disconnect, connect again, load the grid
> and disconnect.


That's the generally accepted wisdom, yes...

> what is the cause because to reconnect many times is more
> efficient?.


Yes, because of a feature of ADO.NET called connection pooling. With web
applications, "little and often" is definitely the way to go...

> I tried running some test on about it,connecting once on page request
> is almost 20% more fast that connecting many times.


How are you creating / destroying the connections...?

> In any case, ASP.NET will keep many connections opens (persistent?).


If your web app has persistent connections with your RDBMS, then you need to
rethink your connectivity method...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      01-11-2008
"Patrice" <http://www.chez.com/scribe/> wrote in message
news:(E-Mail Removed)...

> The thing to check is if your DB supports connection pooling...


The OP is using Oracle which definitely does support connection pooling, but
only if accessed correctly:
http://weblogs.asp.net/ngur/archive/.../21/61207.aspx


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Leon Mayne
Guest
Posts: n/a
 
      01-11-2008
"jc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Reading from internet i found that the best way to use a database is
> to connect and disconnect on every single database request, so if you
> are loading a listbox manually from a database and on the same page,
> loading a grid from the database, then the best way is to connect to
> database, load the listbox, disconnect, connect again, load the grid
> and disconnect.


Depends on the situation. In most circumstances it is better to just pull
back what you need then close the connection and reopen to get any
additional info. I would certainly close the connection after each query,
but you could have a Populate() method in your code which calls one stored
procedure that returns multiple resultsets for all the information required
for the page, close the connection, and then populate all the controls from
the data. Then you are using the connection for the least amount of time and
not opening and closing connections:

CREATE PROCEDURE uspPopulateOrderPage
@pintOrderId INT
AS
-- Get the listbox data
SELECT
Id, Description
FROM
tblListItems

-- Get the order details
SELECT
CustomerName,
MoreInformation
FROM
tblOrder
WHERE
OrderId = @pintOrderId

 
Reply With Quote
 
jc
Guest
Posts: n/a
 
      01-17-2008
On 11 ene, 10:11, "Leon Mayne" <leon@rmv_me.mvps.org> wrote:
> "jc" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > Reading from internet i found that the best way to use a database is
> > to connect and disconnect on every single database request, so if you
> > are loading a listbox manually from a database and on the same page,
> > loading a grid from the database, then the best way is to connect to
> > database, load the listbox, disconnect, connect again, load the grid
> > and disconnect.

>
> Depends on the situation. In most circumstances it is better to just pull
> back what you need then close the connection and reopen to get any
> additional info. I would certainly close the connection after each query,
> but you could have a Populate() method in your code which calls one stored
> procedure that returns multiple resultsets for all the information required
> for the page, close the connection, and then populate all the controls from
> the data. Then you are using the connection for the least amount of time and
> not opening and closing connections:
>
> CREATE PROCEDURE uspPopulateOrderPage
> @pintOrderId INT
> AS
> -- Get the listbox data
> SELECT
> * * Id, Description
> FROM
> * * tblListItems
>
> -- Get the order details
> SELECT
> * * CustomerName,
> * * MoreInformation
> FROM
> * * tblOrder
> WHERE
> * * OrderId = @pintOrderId


But with this method the code will become a bit spaguetti.

I checked that close() and dispose() in fact don't close the
connection but a consecutive open() will not open a new connection.
So, in both cases, connection are spooled and from the database
viewpoint (oracle), the number of connections is stable.

I think that there aren't a simple method but depend on the specific
case, in my case i will open and close once per page request but still
will left open the option to open/close connection by request basis,
just in case.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can connect to wireless but can not connect to internet Lucy Wireless Networking 2 09-23-2006 11:47 AM
Can connect to wireless but can not connect to internet Lucy Wireless Networking 0 09-22-2006 11:52 AM
Can connect to wireless but can not connect to internet Lucy Wireless Networking 0 09-22-2006 11:03 AM
Could Not Connect To SMTP Host Connection Failed -- Software caused connection abort: connect kishore@novelteam.com Java 3 11-16-2005 03:33 PM
Can't connect to local machine with anything except IE Other browsers can't connect without password Otis Mukinfus ASP .Net 3 07-08-2005 03:54 AM



Advertisments