我应该不断地 open() 和 close() 我的 SQL 数据库还是让它保持打开状态?

发布于 2024-11-15 10:33:31 字数 250 浏览 2 评论 0原文

我正在创建一个使用 SQL 数据库来存储数据的应用程序。根据应用程序的设计方式,它将每 3 分钟左右更新一次新数据,具体取决于应用程序运行时的用户操作。

在我看到的教程中,他们建议您在更改数据库后关闭数据库(就资源而言,这是“昂贵的”)。

是否最好在我的应用程序运行期间将其保持打开状态,因为它会相当频繁地更新,或者我应该在每次更改后立即运行 close() 方法?

我想我担心的是,不断地打开和关闭它会比一直保持打开状态消耗更多的资源。

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾忌 2024-11-22 10:33:31

有一次我试图保持连接打开——我用它来填充中继器或其他东西——现在不记得了。

在程序的后面,我还需要使用该连接——我想我有这样的需求,以便当用户单击转发器中的父项目时,会弹出一个详细信息 div,其中包含该项目的更多信息。这会产生一个错误——结果是“无法在打开的连接上打开()”。

我认为该错误可能可以通过另一种方式避免(例如检查我尝试打开的连接是否已经打开),但当我想到这一点时,我意识到我必须在整个应用程序中将其作为标准做法,这似乎工作量太大了,所以我只是将其作为标准做法,在每次使用后始终关闭我的连接。

连接保留在连接池中 - 我对此不是高手 - 但如果对性能感到好奇,我想我会记住这一点,就多次打开连接的成本而言 - 无论您的情况如何无论如何,情况是需要的。

另一个想法是,您的数据库管理员可能能够强制关闭所有打开的连接,或者数据库可能会因其他原因关闭。如果您不是 dba,您可能会考虑依赖于您无法长期控制的事物(例如保持连接打开)的风险/收益。

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

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.

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.

怪我太投入 2024-11-22 10:33:31

对于当今的处理器,甚至手机处理器来说,三分钟就是永恒。我每次都会关闭它并打开它。这比将手柄悬在“以太”中要好。

Three minutes is eternity on todays processors, even phone processors. I would close it and open it each time. It is better than possibly leaving handles hanging out in 'the ether'.

┼── 2024-11-22 10:33:31

假设您有多个数据库操作需要在单个线程中连续执行。我会保持数据库打开来执行这些操作。然而,在完成大量工作后关闭数据库。

Let's say you have several DB operations to do back to back in a single thread. I'd keep the DB open to do those actions. However close the DB after doing a chunk of work.

暮凉 2024-11-22 10:33:31

我认为这个问题的答案还取决于访问数据库的应用程序类型。

如果您多次重新查询数据库
在这种情况下,您可以保持数据库打开。

是否有其他应用程序访问同一数据库?
如果存在并发或阻塞问题的风险,明智的做法是在完成读/写数据库后关闭数据库。

查看以下网址

何时关闭 Android 上的数据库连接?每次操作完成后或应用程序退出后

谢谢 Deepak

I think the answer to this question also depends on what type of application is accessing the database.

If you re-query the database a lot
In this case you can keep the database open.

Are there any other applications accessing the same database?
If there is a risk for concurrency or blocking issues, it might be wise to close the database after finished reading/writing from/to it.

Have a look onthe following url

When to close db connection on android? Every time after your operation finished or after your app exit

Thanks Deepak

撩动你心 2024-11-22 10:33:31

如果你问我到目前为止我读过的有关 Android 的所有内容,一般来说,最好在进行修改后立即将其关闭。

我发现,当你可以的时候,最好的做法是实际创建并注册一个内容提供商。
它有它的价格,但我相信你得到的比你付出的多得多。
您可以通过内容提供程序完成几乎所有操作,并且 Android 门户上的记事本示例非常适合学习如何实现它。它负责数据库同步和打开/关闭它。所以也许调查一下。

if you ask me by everything i have read so far about android and in general it is better to close it right after you make the modifications.

What i find its best to do when you can is actually make and register a content provider.
It has its prices but i believe you gain a lot more than you pay.
You can do pretty much everything with a content provider and the notepad example on the android portal is great for learning how to implement it. it takes care of db synchronization and opening/closing it. so maybe look into that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文