ASP.NET/ADO.NET:处理许多数据库连接.NET对象里面? [英] ASP.NET/ADO.NET: Handling many database connections inside a .NET Object?
问题描述
我们有做了很多的读/写与数据库的.NET对象。贯穿本对象(或asp的页使用它)的生命周期,它可以从1至10倍击中数据库查询/更新的任何地方。
We have a .NET object that does a lot of reading/writing with the database. Throughout the life cycle of this object (or the asp page that uses it), it may hit the database with a query/update anywhere from 1 to 10 times.
,它只是打开实例化时对数据库的连接,然后关闭该对象的终止事件中的连接。那是一个不好的做法?
Instead of opening and closing a database connection every time the object needs to hit the database, it simply opens a connection to the database during instantiation and then closes the connection during the object's termination event. Is that a bad practice?
假设是,由于对象中的所有它的实例化时间(通常是多次)访问数据库,这将是最好只开在对象的生命之初的连接,然后关闭它在最后。
The assumption was that since the object hits the database every time it's instantiated (and usually multiple times), it would be better to just open a connection at the beginning of the object's life and then close it at the end.
另一种方法是,以打开和关闭之前和每个查询/操作后的数据库连接。
The other alternative is to open and close the database connection before and after each query/operation.
什么是最好的做法在这里,以最大限度地提高性能?
What's the best practice here in order to maximize performance?
**更新** 感谢您的提示家伙。任何人都可以讲更开放的方法/关内的对象的实例/解约事件而这样做的后果有关联吗?
**update** Thanks for the tips guys. Can anyone speak more to the method of opening/closing a connection inside an object's instantiation/termination events and the repercussions of doing so?
推荐答案
打开一个紧密的连接。 ADO.NET有内置的连接池的作品。你不会发现,除非你正在做这在成千上万的开环的任何性能问题/关闭。
Open an close the connection as needed. ADO.NET has built in connection pooling that works. You will not notice any performance issues unless you are doing this in a loop with thousands of open/closes.
修改的 请参阅<一href="http://stackoverflow.com/questions/247247/should-i-persist-a-sqlconnection-in-my-data-access-layer">Should我坚持我的数据访问层的SqlConnection?了解更多信息,以连接持久的陷阱。
edit See Should I persist a sqlconnection in my data access layer? for more information as to the pitfalls of connection persistence.
这篇关于ASP.NET/ADO.NET:处理许多数据库连接.NET对象里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!