在WCF服务中管理SQL连接

时间:2022-04-12 13:51:19

I am currently building a WCF Web Service which may have 5 endpoints or even more. Each of these endpoints' methods will need to access a SQL Server database (for different reasons, though), and, of course, these endpoints may be called by several clients at the same time. In this scenario, what is the best way to manage connections to the database?

我目前正在构建一个可能有5个端点甚至更多端点的WCF Web服务。这些端点的每个方法都需要访问SQL Server数据库(但出于不同的原因),当然,这些端点可能同时被多个客户端调用。在这种情况下,管理数据库连接的最佳方法是什么?

1) Have each method open and close a SQLConnection every time it is accessed?
2) Have a static ConnectionManager object with a SQLConnection property permanently open?
3) None of the above. Which, then?

1)每次访问时,每个方法是否打开和关闭SQLConnection? 2)永久打开一个SQLConnection属性的静态ConnectionManager对象? 3)以上都不是。哪个呢?

Thank you very much!

非常感谢你!

1 个解决方案

#1


3  

The correct approach is 1) + connection pooling which is used by default if all your connections connect to database under single account (pool is per unique connection string which contains user login). Connection pooling ensures that connections are reused for multiple operations but it is absolutely transparent to development process.

正确的方法是1)+连接池,如果所有连接都连接到单个帐户下的数据库(池是每个包含用户登录的唯一连接字符串),则默认使用该连接池。连接池确保连接可以重复用于多个操作,但它对开发过程绝对透明。

#1


3  

The correct approach is 1) + connection pooling which is used by default if all your connections connect to database under single account (pool is per unique connection string which contains user login). Connection pooling ensures that connections are reused for multiple operations but it is absolutely transparent to development process.

正确的方法是1)+连接池,如果所有连接都连接到单个帐户下的数据库(池是每个包含用户登录的唯一连接字符串),则默认使用该连接池。连接池确保连接可以重复用于多个操作,但它对开发过程绝对透明。