开发者

Data Access object: Singleton or many small ones?

开发者 https://www.devze.com 2022-12-12 11:22 出处:网络
When developing an application (web, win, whatever) which does alot of data access, is it better to keep your data access object open for the length of the request (i.e. do many things in a row, then

When developing an application (web, win, whatever) which does alot of data access, is it better to keep your data access object open for the length of the request (i.e. do many things in a row, then close it when you finish), or keep opening and closing new ones?

protected aDataContext dc = new aDataContext();
开发者_高级运维

vs

private aObject GetInfo(...) {...}

I would think the former would be better for performance; but it seems like a bad practice.


Better use a connection pool. keep a limited number of connections open as much as you can. Opening a connection is pricey, yet having million open connection might kill your server. you should benchmark your scenario for best results....


Typically you should open a new connection for every unit of work, use it and then close it as soon as possible. Internally .NET (or ADO or ODBC or whatever) will pool connections for you, so long as the connection string is the same, therefore it's actually very efficient. There are other issues to take into account - particularly transactions - but in general it's best to follow this pattern of open-do-close and let .NET manage the connection pool.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号