English 中文(简体)
Data Access object: Singleton or many small ones?
原标题:

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.

最佳回答

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.

问题回答

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....





相关问题
XML-RPC Standard and XML Data Type

I was looking at XML-RPC for a project. And correct me if I m wrong, but it seems like XML-RPC has no XML datatype. Are you supposed to pass as a string? or something else? Am I missing something? ...

Is it exists any "rss hosting" with API for creating feeds

I am creating a desktop app that will create some reports. I want to export these reports as RSS or ATOM feeds. I can easily create feeds with Rome lib for Java. But I have no idea how to spread them. ...

Improving Q-Learning

I am currently using Q-Learning to try to teach a bot how to move in a room filled with walls/obstacles. It must start in any place in the room and get to the goal state(this might be, to the tile ...

High-traffic, Highly-secure web API, what language? [closed]

If you were planning on building a high-traffic, very secure site what language would you use? For example, if you were planning on say building an authorize.net-scale site, that had to handle tons ...

Def, Void, Function?

Recently, I ve been learning different programming langages, and come across many different names to initalize a function construct. For instance, ruby and python use the def keyword, and php and ...

热门标签