Cocoa SQLite何时关闭数据库? [英] Cocoa SQLite when to close database?
问题描述
我在写我的第一个使用SQLite(https://github.com/ccgus/fmdb)的MacOS应用程序。
I am writing my first MacOS application that uses SQLite (https://github.com/ccgus/fmdb).
我可以打开/关闭数据库每个事务的连接(CRUD),或者在init / dealloc。什么是最好的方法?
I could either open/close the database connexion for each transaction (CRUD), or on init/dealloc. What is the best way?
推荐答案
我不知道我有最后的答案, ,我见过很多人说可以离开数据库打开。
I'm not sure I have the definitive answer, but having looked into this a bit myself, I've seen numerous people who say it's ok to leave the database open.
此外,如果你看看Sqlite网站,你会看到他们做了许多工作确保数据库不会从崩溃,电源故障等损坏。
Also, if you look at the Sqlite site you'll see they've done a lot of work on ensuring a database will not get corrupted from crashes, power failures etc.
http://www.sqlite.org/testing.html
http://www.sqlite.org/atomiccommit.html
我的使用体验Sqlite和FMDB是好的打开一个连接,只是让它打开。记住,这是一个文件的连接,它在闪存上的本地文件系统上。这是一个非常不同的情况,而不是通过网络的连接。我认为故障的机会非常微小,因为它明显设计用于处理崩溃,电源故障等,即使它们在实际的数据库操作期间发生 - 因此在数据库操作之外,它们不是问题。
My experience using Sqlite and FMDB is it seems to be fine to open a connection and just leave it open. Remember, this is a "connection" to a file, that's on a local file system that's on Flash memory. That's a very different situation than a connection over the network. I think the chances of failure are extremely slim, as it's clearly designed to handle crashes, power failures etc. even if they occur during an actual database operation - so outside of a database operation they are not an issue.
你当然可以认为,不使用时保持数据库连接打开是不好的做法,我不推荐在典型的客户端 - 服务器设置,但在iPhone / iPad我认为这是一个非问题。保持它打开似乎工作正常,是一个小的担心。
You could of course argue that it's bad practice to keep a database connection open when not in use, and I wouldn't recommend it in a typical client-server setup, but on the iPhone/iPad I think it's a non-issue. Keeping it open seems to work fine and is one less thing to worry about.
这篇关于Cocoa SQLite何时关闭数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!