简体   繁体   English

我需要确保应用程序使用最少数量的数据库连接

[英]I need to ensure that the application uses the minimum number of connections to the database

Question from certification exam: 认证考试的问题:

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. 您使用Microsoft Visual Studio 2010和Microsoft .NET Framework 4创建应用程序。 The application contains the following code segment. 该应用程序包含以下代码段。 (Line numbers are included for reference only.) (包括行号仅供参考。)

01  class DataAccessLayer
02  {
03    private static string connString;
04
05    ...
06    public static DataTable GetDataTable(string command){
07
08      ...
09    }
10  }

You need to define the connection life cycle of the DataAccessLayer class. 您需要定义DataAccessLayer类的连接生命周期。 You also need to ensure that the application uses the minimum number of connections to the database. 您还需要确保应用程序使用最少数量的数据库连接。 What should you do? 你该怎么办?

[A] Insert the following code segment at line 04. [A]在第04行插入以下代码段。

private static SqlConnection conn = new SqlConnection(connString);
public static void Open(){
  conn.Open();
}
public static void Close(){
  conn.Close();
}

[B] Insert the following code segment at line 04. [B]在第04行插入以下代码段。

private SqlConnection conn = new SqlConnection(connString);
public void Open(){
  conn.Open();
}
public void Close(){
  conn.Close();
}    

[C] Replace line 01 with the following code segment. [C]用以下代码段替换行01。

class DataAccessLayer : IDisposable 

Insert the following code segment to line 04. 将以下代码段插入第04行。

private SqlConnection conn = new SqlConnection(connString);
public void Open(){
  conn.Open();
}
public void Dispose(){
  conn.Close();
}

[D] Insert the following code segment at line 07. [D]在第07行插入以下代码段。

using (SqlConnection conn = new SqlConnection(connString)){
  conn.Open();
}    

Some people are arguing that the correct answer is [D], but from my point of view it is no sense, because the connection is being opened and immediately closed after the "using" block. 有人认为正确答案是[D],但从我的角度来看,这是没有意义的,因为在“使用”块之后连接被打开并立即关闭。

Could someone point the correct answer and explain why? 有人可以指出正确答案并解释原因吗?

Thanks!!! 谢谢!!!

None are the correct answer. 没有一个是正确的答案。

  • AC are wrong because they don't handle exceptions. AC是错误的,因为它们不处理异常。
  • C is also wrong because you want to encapsulate the data access from within your method that returns a DataSet. C也是错误的,因为您要封装返回数据集的方法中的数据访问。 Datasets are disconnected, and there is no indication that you are doi anything that requires the class to hold open connections between method calls, so there is no reason to make the entire class hold a connection. 数据集已断开连接,并且没有迹象表明您是需要该类在方法调用之间保持打开连接的任何对象,因此没有理由使整个类都保持连接。 Just do it within each method that makes DB calls. 只需在进行数据库调用的每种方法中执行此操作即可。
  • D is close, but wrong. D很近,但是错了。 To fix it, add the data access code after the conn.Open() call, inside of the using() {...} block. 要解决此问题,请在using(){...}块内部的conn.Open()调用之后添加数据访问代码。

Note: I'm not sure if you didn't put the data access code after the .Open() call in D. If you assumed that to be understood, then D is actually the correct answer. 注意:我不确定您是否没有在D中的.Open()调用之后放置数据访问代码。如果您假设可以理解,那么D实际上是正确的答案。 When the connection is disposed it is released back into the connection pool. 处置连接后,它将释放回连接池。 The connection pool will help you minimize the # of open connections. 连接池将帮助您最小化打开的连接数。 If you need to literally close the connections even when they are inactive you need to start looking into configuring your use of Connection Pooling. 如果即使连接处于非活动状态也需要从字面上关闭连接,则需要开始研究配置连接池的使用。

D implement using keyword which scope section of code and will be disposed implicity. D using关键字实现代码的作用域部分,并将其隐式设置。

A, B, C are still in the life cycle of class/programs level so each instance will span another connections. A,B,C仍处于类/程序级别的生命周期中,因此每个实例将跨越另一个连接。

D is the correct one. D是正确的。 You'll only use one connection. 您将只使用一个连接。 At least, you hope that. 至少,您希望如此。 ADO.NET features Connection Pooling, you can't be absolutely sure what's going on unless you use solution A (one statically allocated connection). ADO.NET具有连接池功能,除非使用解决方案A(一个静态分配的连接),否则您不能绝对确定发生了什么。

Since you should use the connection pool as much as possible, D is still the correct one. 由于您应该尽可能多地使用连接池,因此D仍然是正确的连接池。

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

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