Does anyone see any problem with the loading a SqlConnection into the
System.Web.Http ContextCurrent. 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.Http Context.Current .Items["currentConnect ion"] = new
SqlConnection() ;
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
onnectionString = "connection_str ing_here";
}
... Object calls here to SELECT/INSERT/UPDATE data using the common
connection
override protected void OnUnload(EventA rgs args) {
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
lose();
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).D
ispose();
base.OnUnload(a rgs);
}
}
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.Http Context.Current .Items)
public SaveData() {
System.Web.Http Context.Current .Items["currentTransac tion"]
=
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).B
eginTransaction ();
try {
ClientObject.Sa ve();
AddressObject.S ave(ClientObjec t.ID);
PhoneObject.Sav e(ClientObject. ID);
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Co mmit();
} catch (Exception exp) {
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Ro llback();
throw(exp);
} finally {
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Di spose();
}
}
Does anyone see any major performance issues or other potential problems
with this? Thanks for any input!
Nayt Grochowski
System.Web.Http ContextCurrent. 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.Http Context.Current .Items["currentConnect ion"] = new
SqlConnection() ;
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
onnectionString = "connection_str ing_here";
}
... Object calls here to SELECT/INSERT/UPDATE data using the common
connection
override protected void OnUnload(EventA rgs args) {
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).C
lose();
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).D
ispose();
base.OnUnload(a rgs);
}
}
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.Http Context.Current .Items)
public SaveData() {
System.Web.Http Context.Current .Items["currentTransac tion"]
=
((SqlConnection )System.Web.Htt pContext.Curren t.Items["currentConnect ion"]).B
eginTransaction ();
try {
ClientObject.Sa ve();
AddressObject.S ave(ClientObjec t.ID);
PhoneObject.Sav e(ClientObject. ID);
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Co mmit();
} catch (Exception exp) {
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Ro llback();
throw(exp);
} finally {
((SqlTransactio n)System.Web.Ht tpContext.Curre nt.Items["currentTransac tion"].
Transaction).Di spose();
}
}
Does anyone see any major performance issues or other potential problems
with this? Thanks for any input!
Nayt Grochowski