它是安全,保持数据库连接打开很长一段时间它是、很长、数据库连接、安全

2023-09-02 20:45:28 作者:降妞十巴掌

我有一个连接到远程数据库的.NET客户端应用程序。 它是安全,以保持一个连接开放客户端的寿命(小时)?

I have a .net client application which is connected to a remote database. Is it safe to keep a single connection open for the lifetime of the client (hours)?

其答案是否持有,如果我有运行多个(10或100)的客户呢?

Does the answer hold if I have multiple (10 or 100) clients running?

感谢

推荐答案

绝对是安全的做到这一点。这是客户端 - 服务器应用程序是如何工作的。如果您使用的是三层应用程序,该应用程序服务器将保持连接开放的游泳池呢。

Absolutely it is safe to do this. This is how client-server applications work. If you are using a three-tier application, the application server will keep a pool of connections open anyway.

可扩展性是一个问题,或者至少曾经是在未来的日子该机器拥有比现代套件内存更少。随着两层(客户端 - 服务器)应用程序,每个客户端打开一个连接并保持打开。这有几个方面的影响:

Scalability is an issue, or at least used to be in the days that machines had less memory than modern kit. With a two-tier (client-server) application, each client opened a connection and held it open. This had several effects:

内存是每个连接使用,所以 大量的(相对)空闲 连接将占用机 记忆。然而,现代的64位 服务器可以有几十或几百 GB的内存,因此它可以支持 非常大量的这种 连接。

Memory was used per-connection, so large numbers of (relatively) idle connections would use up machine memory. However, a modern 64-bit server can have tens or hundreds of GB of memory, so it could support a very large number of such connections.

如果交易被留 未提交的客户端机器上, 锁将举行开放的 只要交易是开放的。 这导致了一类问题时 有人可能会启动一个事务, 走下车来午饭,忘记他们 留下的东西打开。这会 锁定由引用任何记录 交易在一个小时的时间。

If a transaction was left uncommitted on the client machine, the locks would be held open for as long as the transaction was open. This led to a class of problems when someone could start a transaction, go off to lunch and forget they had left something open. This would lock any records referenced by the transaction for hours at a time.

交易可以,但是容易 覆盖多个访问非常的 数据库,这是很难做到与 一个连接错误游泳池。

Transactions could, however easily cover multiple acceses to the database, which is harder to do with a conneciton pool.

有一个共用的架构,这是常见的3层架构具有应用服务器和数据库之间的连接数量有限。查询简单地使用下一个可用的连接和更新立即提交。这将使用更少的资源,因为你只连接打开的数量有限,以及(结合的乐观并发策略)将消除大量的潜在的应用并发问题。

A pooled architecture, which is common on 3-tier architectures has a finite number of connections between the application server and the database. Queries simply use the next available connection and updates are committed immediately. This uses less resources as you only have a finite number of connections open, and (in conjunction with an optimistic concurrency strategy) will eliminate a large of potential application concurrency issues.

为了使用长事务(涵盖多个调用数据库,即交易)一个人到去耦从连接的事务。这是一个TP监控的基本架构和有诸如 XA 或的 OLE事务来支持这一点。如果这种类型的外部管理的事务是不可用的应用程序构造补偿事务来还原应用程序的事务中所做的更改。这种架构通常被工作流管理系统。

In order to use long transactions (i.e. transactions that cover more than one call to the database) one has to de-couple the transaction from the connection. This is the basic architecture of a TP monitor and there are some standard protocols such as XA or OLE Transactions to support this. If this type of externally managed transaction is unavailable the application has to construct a compensating transaction that undoes the changes made by the application's transaction. This type of architecture is often used by workflow management systems.