简体   繁体   English

使用一个或多个上下文更好吗

[英]Is it better to use one or many contexts

We have an application that has 3 contexts for connecting to 3 SQL Server databases, under the same server. 我们有一个具有3个上下文的应用程序,用于连接到同一服务器下的3个SQL Server数据库。

Our DBAs are arguing that these three connections are being costly for the server. 我们的DBA认为这三个连接对于服务器来说是昂贵的。

My question is: is it true? 我的问题是:是真的吗? Are three connections more costly than one? 三个连接比一个连接更昂贵吗? How can I measure it? 我该如何测量?

What is the best practice for dealing with this sittuation on Entity Framework? 在Entity Framework上处理此情况的最佳实践是什么?

Also, we are accessing three databases and our DBAs are arguing we are doing three connections. 另外,我们正在访问三个数据库,而DBA则在争论我们正在建立三个连接。 Is it a coincidence or each context really corresponds to a different connection? 是巧合还是每个上下文确实对应于一个不同的连接?

Entity Framework under the hood uses ADO.Net Connection Pooling . 实体框架使用ADO.Net连接池

Excerpt: 摘抄:

Connection pooling reduces the number of times that new connections must be opened. 连接池减少了必须打开新连接的次数。 The pooler maintains ownership of the physical connection. 池管理者维护物理连接的所有权。 It manages connections by keeping alive a set of active connections for each given connection configuration. 它通过为每个给定的连接配置保留一组活动的连接来管理连接。 Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. 每当用户在连接上调用“打开”时,池管理器就会在池中寻找可用的连接。 If a pooled connection is available, it returns it to the caller instead of opening a new connection. 如果池化连接可用,它将把它返回给调用者,而不是打开一个新连接。 When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. 当应用程序在连接上调用“关闭”时,池化程序将其返回到活动连接的池化集中,而不是将其关闭。 Once the connection is returned to the pool, it is ready to be reused on the next Open call. 一旦将连接返回到池,就可以在下一个Open调用中重用该连接。

Pooling connections can significantly enhance the performance and scalability of your application. 池连接可以显着提高应用程序的性能和可伸缩性。 By default, connection pooling is enabled in ADO.NET. 默认情况下,ADO.NET中启用了连接池。 Unless you explicitly disable it, the pooler optimizes the connections as they are opened and closed in your application. 除非您明确禁用它,否则池化程序会在应用程序中打开和关闭连接时优化连接。 You can also supply several connection string modifiers to control connection pooling behavior. 您还可以提供多个连接字符串修饰符来控制连接池的行为。 For more information, see "Controlling Connection Pooling with Connection String Keywords" later in this topic. 有关更多信息,请参见本主题后面的“使用连接字符串关键字控制连接池”。

.

Our DBAs are arguing that these three connections are being costly for the server. 我们的DBA认为这三个连接对于服务器来说是昂贵的。

He needs to Define costly. 他需要定义昂贵的东西。 If you need 3 connections, you need three connections, you can't just magically reduce the number of connections. 如果需要3个连接,则需要3个连接,您不仅可以神奇地减少连接数量。

Are three connections more costly than one? 三个连接比一个连接更昂贵吗?

Yes. 是。 Everything has a Pro/Con to it. 一切都有Pro / Con。 If it simpler to write, maintain and have better performance with 3, it not as simple as just saying 3 vs 1. 如果使用3来编写,维护和获得更好的性能更简单,那不只是说3 vs 1那样简单。

What is the best practice for dealing with this sittuation on Entity Framework? 在Entity Framework上处理此情况的最佳实践是什么?

I don't think based on your question (no example contexts, why there are 3 databases, if they are all in use at the same time by the same client or not) it can really be answered in your case. 我不认为基于您的问题(没有示例上下文,为什么会有3个数据库,如果同一个客户端同时使用三个数据库),在您的情况下确实可以回答。

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

相关问题 是否可以在两个不同的上下文中使用一个事务? - Is it possible to use one transaction for two different contexts? 最好使用两个多对多表或一个带有三个外键引用的表 - Better to use two many to many tables or one table with three Foreign Key References 一对多选择哪个更好 - Selecting one to many which one is better 如何在两个不同域中的两个不同上下文中使用一个表? - How to use one table for two different contexts in two different domains? 在RavenDb中一对多建模以获得更好的性能 - Model one to many in RavenDb for better performance 使用一个连接执行多个sql命令或每次重新连接是否更好? - Is it better to execute many sql commands with one connection, or reconnect every time? 有没有更好的方法来编写if语句来寻找与多个变量匹配的变量? - Is there a better way to code an if statement looking for a variable to match one of many? 循环还是递归方法? 哪个更好用? - Loop or Recursive method? Which one is better to use? 哪一个更好用,为什么用c# - Which one is better to use and why in c# 如何在一个LINQ请求中使用两个不同的数据上下文? - How can I use two different data contexts in one LINQ request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM