我应该开()和close()我的SQL数据库不断或将其打开?我的、将其、数据库、close

2023-09-06 00:08:24 作者:恍如隔世梦里见

我创建一个使用SQL数据库用于存储数据的应用程序。该应用被设计它会被更新,每3分钟或方式,以便用新的数据,这取决于用户的操作应用程序正在运行时

I am creating an app that uses an SQL database for storing data. The way the app is designed it will be updated every 3 minutes or so with new data depending on user actions while the app is running.

在我所见过的教程,他们建议你改变它(这是在资源方面的贵)后关闭数据库。

In the tutorials I have seen, they recommend that you close the database after changing it (it is an "expensive" in terms of resources).

是更好地把它打开我的应用程序的时间,因为它正在更新一个相当频繁或者我应该每次更改后立即运行close()方法?

Is it better to leave it open for the duration of my App since it is being updated on a fairly frequent basis or should I run the close() method immediately after every change?

我想我担心的是,它打开和关闭它不断地将吸引更多的资源会比离开它打开的整个时间。

I guess my worry is that opening it and closing it constantly will draw more resources than it would to leave it open the entire time.

推荐答案

我试图离开连接打开一次 - 我用它的东西中继器或东西 - 现在不能记得

I tried to leave a connection open once -- I used it to stuff a repeater or something -- can't remember now.

在稍后的节目,我还有另外一个需要使用的连接 - 我想我是有这样当用户在转发点击一个父项目,一个细节的div会弹出与该项目的更多信息。这产生一个错误 - 这一点对无法打开()在一个开放的连接的效果

Later in the program, I had another need to use the connection -- I think I had it so that when a user clicked a parent item in the repeater, a detail div would pop-up with more information for that item. This generated an error -- something to the effect of 'cannot open() on an open connection'.

我认为该错误可能是可以避免的另一种方式(如检查,看看是否我试图打开连接已经打开),但我想过这个问题,我意识到我不得不做出一个标准的做法在我的应用程序,而且好像太辛苦了,所以我只是做了它标准的做法,每次使用后应关闭我的连接。

I think the error might have been avoidable another way (like checking to see if the connection I was trying to open was already open), but as I thought about it, I realized I'd have to make that a standard practice throughout my app, and that seemed like too much work, so I just made it a standard practice to always close my connections after each use.

连接留在连接池 - 我在没有高手 - 但如果感到好奇的表现,我想我会记住这一点,在它的成本要打开一个连接多次术语 - - 无论你的情况是无论如何需要

Connections stay in a connection pool -- I'm no whiz on that -- but if was curious about performance, I guess I'd keep that in mind, in terms of what it costs to open a connection multiple times -- whatever your situation is requiring anyway.

另一个想法是,你的数据库管理员可能能够强行关闭所有打开的连接,或者数据库可能会关闭某些其他原因。如果你不是DBA,你可以给不同的东西,你没有长期控制像保持连接开放的思想的风险/效益。

Another thought is that your DB admin may be able to force-close all open connections, or the db may close for some other reason. If you're not the dba, you might give a thought to risk/benefit of depending on something that you don't have long-term control over like keeping the connection open.