C# - Should I use static database connection

后端 未结 2 880
[愿得一人]
[愿得一人] 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

    In general, no, you shouldn't use a single connection - all of the .NET ADO.NET providers support connection pooling, and the normal pattern is to open/close connections as you need them (in a using or try/finally block to ensure the connection is closed in the event of an exception).

    In a single-threaded client application you could get away with using a shared static connection, but it's unlikely to give you any measurable performance benefit - so don't do it.

    In any other application, you definitely shouldn't use a shared static connection, as it is not thread-safe.

提交回复
热议问题