简体   繁体   English

在.NET中管理数据库连接的最佳实践是什么?

[英]What are best practices on managing database connections in .NET?

Regarding best practice for managing database connections in a .NET application -- I know that, in general, it's bad to pass around a connection object. 关于在.NET应用程序中管理数据库连接的最佳实践 - 我知道,通常,传递连接对象是不好的。

However, I have some specific curiosities: 但是,我有一些特定的好奇心:


1. I have two instances of business objects, of different classes, in a parent-child relationship (the child is private.) Which of the following is best? 1.我有两个业务对象的实例,不同的类,父母 - 孩子关系(孩子是私人的。)以下哪项是最好的?

  • Keep one private static connection open and shared, used by both objects, and left open until the parent is disposed. 保持一个私有静态连接打开和共享,由两个对象使用,并保持打开状态直到父级被释放。

  • Keep two private static connections open, one for each object, not to be closed until the object is disposed. 保持两个私有静态连接打开,每个对象一个,在处理对象之前不要关闭。

  • Do not keep static connections; 不要保持静态连接; open and subsequently close a new connection for every method that requires it. 打开并随后关闭每个需要它的方法的新连接。 However, most of my methods only run 1-3 queries, so this seems inefficient... ? 但是,我的大多数方法只运行1-3个查询,所以这看起来效率低......


2. My second question is essentially the same, but for a single form. 我的第二个问题基本上是相同的,但对于一个单一的形式。 What's best here? 这里最好的是什么?

  • Keep one private static connection open and shared for the lifetime of the form. 保持一个私有静态连接在表单的生命周期内打开和共享。

  • Do not keep a static connection; 不要保持静态连接; open and subsequently close a connection for every method in the form that requires it (again, a mere 1-3 queries per method.) 打开并随后为需要它的表单中的每个方法关闭一个连接(同样,每个方法只有1-3个查询。)

(Was a comment)... (是评论)......

The theory is you should not be accessing the database from your business logic - it should be in a separate data-access class. 理论上,您不应该从业务逻辑中访问数据库 - 它应该位于单独的数据访问类中。 (Say for example in the future you need to store them offline in XML, or use Oracle rather than SQL Server... you don't want to re-write your business logic!) (例如,将来你需要将它们以XML格式离线存储,或者使用Oracle而不是SQL Server ......你不想重写你的业务逻辑!)

Your business objects should not have database connections associated with them. 您的业​​务对象不应具有与之关联的数据库连接。 The connection should be opened in some DAL factory-type method, the object retrieved/built, then the connection closed and the object returned. 应该在某个DAL工厂类型方法中打开连接,检索/构建对象,然后关闭连接并返回对象。

The business objects themselves should contain business logic fields and methods, which might call back to the Data Access Layer, which should create a new database connection for each DAL method. 业务对象本身应包含业务逻辑字段和方法,这些字段和方法可能会回调数据访问层,数据访问层应为每个DAL方法创建新的数据库连接。

Your inefficiency fears can be put to rest by using Connection Pooling, which means that if you open and close a connection hundreds of times, chances are they will all use the same one. 您可以通过使用连接池来缓解您的低效率恐惧,这意味着如果您打开和关闭连接数百次,则他们可能都会使用相同的连接。 But you should not be keeping database connections hanging around at all - especially not as members on a class. 但是你根本不应该保持数据库连接 - 尤其不是作为类的成员。

Hope that helps! 希望有所帮助!

My understanding is that connections should only stay open as long as needed. 我的理解是,只要需要,连接应该保持开放。 Most of the time I've seen connections in Using statements, similar to 大多数时候我在使用语句中看到过连接,类似于

using (DBConnection db = new DBConnection(connectString))
{
    //do stuff
}

This link may be helpful: Best Practices for Using ADO.NET 此链接可能会有所帮助: 使用ADO.NET的最佳实践

Here's an interesting excerpt. 这是一个有趣的摘录。

For best performance, keep connections to the database open only when required. 为获得最佳性能,请仅在需要时保持与数据库的连接。 Also, reduce the number of times you open and close a connection for multiple operations. 此外,减少打开和关闭多个操作的连接的次数。

I've always followed the practice of opening connections in a using block, so that the Dispose method (and hence the Close method) is always called without my worrying about it. 我一直遵循在使用块中打开连接的做法,因此总是调用Dispose方法(因此也就是Close方法)而不用担心它。 Using this approach I've never encountered a situation where poor performance was linked either to excessive concurrent connections or excessive setup or tear down operations. 使用这种方法,我从来没有遇到过这样的情况:性能不佳与过多的并发连接或过度的设置或拆除操作有关。

In answer to both questions, if you are using something that has connection pooling, like ADO.NET, you should code your queries to keep the connection open as short as possible . 在回答这两个问题时,如果您使用的是具有连接池的东西,例如ADO.NET,则应编写查询代码以尽可能缩短连接。 Ie open and subsequently close a new connection for every method that requires it. open and subsequently close a new connection for every method that requires it. . When you close the connection it will be returned to the connection pool and reused on a subsequent query and thus you will not incur a performance penalty by opening and closing a bunch of connections. 当您关闭连接时,它将返回到连接池并在后续查询中重用,因此您不会因打开和关闭一堆连接而导致性能下降。 The advantage is that you won't risk leaking connections that you forgot to close and in the long run, you'll have fewer simultaneous connections open than if you keep connections open for long periods of time. 优点是您不会冒泄漏您忘记关闭的连接的风险,从长远来看,与长时间保持连接打开相比,您打开的同时连接数会更少。 It doesn't matter whether the application is a Windows form instead of a Web form: keep connections open as short as possible. 应用程序是Windows窗体而不是Web窗体并不重要:保持连接尽可能短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM