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

.NET Question: Any problems with loading a SqlConnection into the current HttpContext?

Does anyone see any problem with the loading a SqlConnection into the
System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then
Closing and Disposing of it the OnUnload method?

Connection would not be "Opened" until it was actually used (this is handled
by a common "Helper" class - similar to Microsoft's SqlHelper Application
Block), Ie:

public class MyPage : Page {
public MyPage() {
System.Web.HttpContext.Current.Items["currentConnection"] = new
SqlConnection();

((SqlConnection)System.Web.HttpContext.Current.Ite ms["currentConnection"]).C
onnectionString = "connection_string_here";
}

... Object calls here to SELECT/INSERT/UPDATE data using the common
connection

override protected void OnUnload(EventArgs args) {

((SqlConnection)System.Web.HttpContext.Current.Ite ms["currentConnection"]).C
lose();

((SqlConnection)System.Web.HttpContext.Current.Ite ms["currentConnection"]).D
ispose();

base.OnUnload(args);
}
}

This could allow other objects to use this connection and would greatly
simplify connection usage.
Could also use the same exact concept for a Transaction that is used in
multiple objects that know nothing about each other, ie:
(each Save method below uses the Connection and Transaction objects stored
in System.Web.HttpContext.Current.Items)

public SaveData() {
System.Web.HttpContext.Current.Items["currentTransaction"]
=
((SqlConnection)System.Web.HttpContext.Current.Ite ms["currentConnection"]).B
eginTransaction();

try {
ClientObject.Save();

AddressObject.Save(ClientObject.ID);
PhoneObject.Save(ClientObject.ID);
((SqlTransaction)System.Web.HttpContext.Current.It ems["currentTransaction"].
Transaction).Commit();
} catch (Exception exp) {

((SqlTransaction)System.Web.HttpContext.Current.It ems["currentTransaction"].
Transaction).Rollback();
throw(exp);
} finally {

((SqlTransaction)System.Web.HttpContext.Current.It ems["currentTransaction"].
Transaction).Dispose();
}
}
Does anyone see any major performance issues or other potential problems
with this? Thanks for any input!

Nayt Grochowski
Nov 17 '05 #1
1 2266
a Shared (or Static in C#) property is not the way to go - I don't a single
connection shared between everyone, that will create a bottle-neck.
And if I share a Transaction that will blow everything up too. Only way
around it is to have the object availble to the single thread that is
currently running.

And your not actually putting the object in the Context - I belive its just
a memory pointer that is stored there... not a big deal I would think. But
that is what I am curious to find out from the community.

And its not always a matter of create and closing again and again - its also
sharing a Transaction between all of the objects that may potentially use
it.

Thank you for the thought - but I need something more...
"Mamcx" <th******@notexist.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Is bad design put heavy objects inside any thing that act like a sesion in a web server..if you want to share the conexion exist a easy way, create a
empty module or class and put a shared property:

Public Shared

Public Shared Property or function sqlCon:SqlConnection
get

In this way, you can use a clear code:

THelper.sqlCon......

get the idea?

Anyway, creating and closing a sql connection not cause a notorio
degradation in performance...in some ways can improve that

Nov 17 '05 #2

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

Similar topics

4
by: Asbjørn Ulsberg | last post by:
Sorry for posting to a lot of groups, but I'm not sure what this problem relates to, so I thought it was better to be safe than sorry. Please feel free to set FUT to the proper group when...
0
by: Nayt Grochowski | last post by:
Does anyone see any problem with the loading a SqlConnection into the System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then Closing and Disposing of it the OnUnload method? ...
2
by: Paul M | last post by:
hi there, i have an <asp:button> control on my webpage that when clicked, i want to redirect the HTTP request to an EXE file, but instead of actually directing the browser and address bar to...
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
0
by: ujjc001 | last post by:
Here's the layout. Aspx has a repeater on the left, which will call a function to change a session var that holds the usercontrol name to add (i.e. "uc_myusercontrol.ascx") and then calls...
0
by: Rick Hein | last post by:
I've got a problem with an app I've been working on, the Caching object and events not firing correctly. In a nutshell: When I'm debugging, and I set a breakpoint in the removed item call back, the...
5
by: Stan SR | last post by:
Hi, Some newbie questions.. :-) First, what is the namespace to use for the Cache class ? When I use this bit of code I get an error if (Cache==null) Cache.Insert("myUserList",userlist);...
4
by: =?iso-8859-1?B?Sm9oYW4gU2r2c3Ry9m0=?= | last post by:
It seems that many people experience problems with mix-ups and non-intentional sharing of sessions in ASP.NET. This is often tracked down to the use of static variables for user data, which are...
5
by: ThunderMusic | last post by:
Hi, I always refer to this page to know the order of events in a page : http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx but this time, I'm mystified... I have a Control called...
3
by: Brett R. Wesoloski | last post by:
I am having problems using HttpContext.Current.Request.Url.Host. I have some code that does this... if (HttpContext.Current != null) { subdomain = HttpContext.Current.Request.Url.Host; }
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:
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.