保持数据库连接打开是昂贵的吗? [英] Is it expensive to keep database connections open?

查看:128
本文介绍了保持数据库连接打开是昂贵的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与几个软件开发人员会面,并建议尽快在我的应用程序代码关闭数据库连接?有人可以告诉我在应用程序中保持连接打开的危害。

I was in a meeting with a few software developers, and was recommended to close database connections as soon as possible in my application code? Can somebody please tell me what is the harm of keeping a connection open in an application.

我正在从数据库的单个表中读取数据

I was reading the data from a single table of a database

推荐答案

想象它像公共汽车上的座位。

Think of it like seats on a bus.

填补这些座位 - 最终,公共汽车是满的,不能再接受乘客(或打开更多的数据库连接)。任何时候公共汽车必须拒绝乘客,因为它有容量,该乘客必须等待另一辆公共汽车来。

As you open connections, you fill up those seats - eventually, the bus is full and can no longer accept passengers (or open more database connections). Any time the bus has to refuse a passenger because it's at capacity, that passenger has to wait for another bus to come by.

当你完成后关闭你的连接与他们,你释放空间的更多的连接 - 这意味着更多的程序,需要与数据库交互,可以做他们需要,而不必等待连接释放。不关闭连接意味着数据库需要弄清楚如何处理所有的连接,如果您的数据库不能像打开新连接一样快速关闭连接,可能会导致问题。

By closing your connections when you're done with them, you free up room for more connections - which means more programs that need to interact with the database can do what they need to, without having to wait around for connections to free up. Not closing your connections means the database needs to figure out what to do with all the connections sitting around, which can cause problems if your database isn't closing connections as quickly as you're opening new ones.

当你使用连接池时,这会改变(见下面的注释);在这些情况下,你会希望你的池处理打开和关闭连接为你。如果你没有汇聚你的连接,保持它们打开的时间超过你需要的是浪费资源。

This changes when you're using a connection pool (see comments below); in those situations you'll want your pool to handle opening and closing connections for you. If you're not pooling your connections, keeping them open any longer than you need to is wasting resources.

这篇关于保持数据库连接打开是昂贵的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆