C# - Should I use static database connection

后端 未结 2 909
[愿得一人]
[愿得一人] 2021-01-05 00:44

In my application to connect to Orace database I always create a new connection, open it, execute OracleCommands and finally close it afterwards. Recently I thought implemen

2条回答
  •  遥遥无期
    2021-01-05 01:19

    I'm assuming you're using ODBC here because you haven't stated exactly and it's normally used...

    No, you should use a new connection each time, this is the standard practise that Microsoft recommend. If you're using ODBC etc then windows manages these connections, caching them for re-use and it makes it easier to manage the lifetime of things.

    If you use a static connection, you might dispose of it to early or have closed it without knowing. In general it's just a bit more awkward and a premature optimization.

    To deploy high-performance applications, you frequently must use connection pooling. However, when you use the .NET Framework Data Provider for ODBC, you do not have to enable connection pooling because the provider manages this automatically.

    See OdbcConnection for more info.

提交回复
热议问题