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

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default .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


    Nayt Grochowski Guest

  2. Similar Questions and Discussions

    1. HttpContext.Current.User.IsInRole
      Hi, I have a problem when I am using the HttpContext.Current.User.IsInRole... This is my code: if (HttpContext.Current.User.IsInRole("Admin...
    2. How secure is HttpContext.Current.User.Identity.Name ?
      How secure it is to authorize access to an ASP.NET application based on the value of the HttpContext.Current.User.Identity.Name propery? I...
    3. Web.HttpContext.Current.User.Identity.Name is blank
      I am using an application which is a modification of IBuySpy Portal. It is using Forms authentication. Users login and their name is added to...
    4. HttpContext.Current.User vs. Thread.CurrentPrincipal
      How are HttpConext.Current.User and Thread.CurrentPrincipal different? It seems that they can be set differently in different places. Why would...
    5. HttpContext.Current
      Now how in the wolrd does that work? How does it know that some static method I called on some library was called by a page that had that context?...
  3. #2

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

    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" <thismail@notexist.com> wrote in message
    news:%23glBfe5VDHA.1984@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
    >
    >

    Nayt Grochowski Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139