locked
Static Variables RRS feed

  • Question

  • User1368242848 posted

    As static variables are static for the whole web application, is it dangerous to declare sql specific variables as static :

    public static class Database
    {

    public static SqlConnection Sql_Connection;
    public static SqlCommand Sql_Command;
    public static SqlDataReader Sql_DataReader;  etc....

    }

    Because FOR EXAMPLE: suppose 2 users might call SqlDataReader at the same time, the danger is that the result of one user might sent to the second one since the content (value) of the SqlDataReader is UNIQUE for the whole application.  is my assumption right?

    Friday, April 6, 2018 4:57 PM

Answers

  • User753101303 posted

    Yes, as you understood else you are using a single connection/command/reader for ALL users which will cause issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2018 8:13 AM

All replies

  • User753101303 posted

    Hi,

    Correct. Unlike a Windows application, your web application is a single application used by multiple users at the same time.

    As using db connections is very frequent you have a "connection pooling" feature that allows to efficiently create/close connections as needed (rather than being really opened/closed they are taken /given back to a pool of connections which allows basically your app to "auto adjust" the number of available connections to what you need without any work on your side.

    Friday, April 6, 2018 5:12 PM
  • User1368242848 posted

    So, as a reply to my assumption you mean: we should avoid using STATIC sql connection variables. right ?

    Saturday, April 7, 2018 12:43 PM
  • User753101303 posted

    Yes, as you understood else you are using a single connection/command/reader for ALL users which will cause issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2018 8:13 AM