locked
Share SQL connection for all Subs RRS feed

  • Question

  • User363578564 posted

    is it ok to put it on top of code page and share the sql connection for all subs?

    Shared SQLCS As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ABCConnectionString").ConnectionString)

    it seems not good to type it again and again in a same code page.

    Any use if different sql connection names are defined for different Subs?  Just for clarification?

    Thanks a lot for your advice. 

    Thursday, January 11, 2018 8:44 AM

Answers

  • User753101303 posted

    Hi,

    Don't use Shared. for this. You would then use a single connection for ALL users. As usual you can place repetitive code in your own class/methods to build a thin data access layer.

    I don't see which benefit would bring using another connection name depending on the sub ? Seems it would be just more complex than clearer. Do things if you see a real benefit.

    For example if you have an architecture with a read/write db and read only replicas you could have a read/write connection and a read/only connection to spread load (and if read needs are significant compared with read/write needs)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 11, 2018 9:48 AM

All replies

  • User753101303 posted

    Hi,

    Don't use Shared. for this. You would then use a single connection for ALL users. As usual you can place repetitive code in your own class/methods to build a thin data access layer.

    I don't see which benefit would bring using another connection name depending on the sub ? Seems it would be just more complex than clearer. Do things if you see a real benefit.

    For example if you have an architecture with a read/write db and read only replicas you could have a read/write connection and a read/only connection to spread load (and if read needs are significant compared with read/write needs)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 11, 2018 9:48 AM
  • User363578564 posted

    Thanks a lot for your clear explanation.

    Thursday, January 11, 2018 12:21 PM