android上的法拉盛和SQLite数据库

时间:2021-09-12 05:33:54

I have an android application using an SQLite database. I open the database when the application starts, but never close it as it is in constant use.

我有一个使用SQLite数据库的android应用程序。当应用程序启动时,我打开数据库,但永远不要关闭它,因为它经常使用。

What is the best way to tell the database to flush all its changes to permanent storage? Do I need to just close it and re-open or is there a more efficient way?

告诉数据库将其所有更改刷新到永久存储的最佳方法是什么?我需要关闭它重新打开还是有更有效的方法?

My problem is that when testing on a phone, turning the phone off after a number of writes sometimes causes the database to loose the most recent updates when the application is restarted, which is obviously unacceptable for a database system.

我的问题是,在对手机进行测试时,在多次写操作之后关闭手机,有时会导致数据库在重新启动应用程序时丢失最新的更新,这对于数据库系统来说显然是不可接受的。

Since I cannot find out how to capture the application close event I cannot know when to manually close the database.

由于我无法找到如何捕获应用程序关闭事件,所以我不知道何时手动关闭数据库。

3 个解决方案

#1


8  

I open the database when the application starts, but never close it as it is in constant use.

当应用程序启动时,我打开数据库,但永远不要关闭它,因为它经常使用。

Don't do that. You are apparently ignoring all the errors that are appearing in LogCat complaining that you are leaking database connections.

不要这样做。您显然忽略了LogCat中出现的所有错误,抱怨您正在泄漏数据库连接。

What is the best way to tell the database to flush all its changes to permanent storage?

告诉数据库将其所有更改刷新到永久存储的最佳方法是什么?

It will do that automatically at the end of a transaction. By default, each individual SQL operation (e.g., insert) is a transaction.

它将在事务结束时自动执行此操作。默认情况下,每个单独的SQL操作(例如,insert)都是一个事务。

Do I need to just close it and re-open or is there a more efficient way?

我需要关闭它重新打开还是有更有效的方法?

You should close your database at some point (e.g., onDestroy() of the service that is mediating your database).

您应该在某个时刻关闭您的数据库(例如,中介数据库的服务onDestroy()))。

My problem is that when testing on a phone, turning the phone off after a number of writes sometimes causes the database to loose the most recent updates when the application is restarted, which is obviously unacceptable for a database system.

我的问题是,在对手机进行测试时,在多次写操作之后关闭手机,有时会导致数据库在重新启动应用程序时丢失最新的更新,这对于数据库系统来说显然是不可接受的。

If you can create a sample project that demonstrates the problem even after you are properly closing your database connection, post it to the Android issue tracker.

如果您可以创建一个示例项目,在正确地关闭数据库连接之后演示这个问题,那么将它发布到Android问题跟踪器。

Since I cannot find out how to capture the application close event I cannot know when to manually close the database.

由于我无法找到如何捕获应用程序关闭事件,所以我不知道何时手动关闭数据库。

Close it when all activities have unbound from the service that is mediating the database connection, triggering that service's onDestroy() method.

当所有活动都从中介数据库连接的服务断开绑定时关闭它,触发该服务的onDestroy()方法。

Or, open a fresh connection in each component that needs access to the database, and use Java synchronization to ensure two threads do not try to simultaneously use the database (if needed).

或者,在需要访问数据库的每个组件中打开一个新的连接,并使用Java同步确保两个线程不会同时使用数据库(如果需要的话)。

#2


2  

If you use any transactions they are nested. Meaning if you break out of one before ending it. You'll end up with some writes rolled back when the phone/app is restarted since you never close the connection.

如果使用任何事务,它们都是嵌套的。意思是,如果你在结束之前打破了一个。当重新启动电话/应用程序时,您将会回滚一些写操作,因为您永远不会关闭连接。

db.beginTransaction();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }

#3


-1  

Is that on a Droid? And how exactly do you turn the phone off? Unless you pull the battery out, shutting down the phone will slowly wind down the operating system, at which point it will commit everything to memory.

那是机器人吗?你到底是怎么关掉电话的?如果你不拔掉电池,关掉手机就会慢慢地关闭操作系统,这时它就会把所有东西都存储到内存中。

When exactly do you lose your database? After every app restart, or just randomly? If it's after every restart, it sounds more like you're destroying the database on startup.

您何时丢失数据库?每次应用重启后,还是随机启动?如果是在每次重启之后,听起来更像是在启动时破坏数据库。

#1


8  

I open the database when the application starts, but never close it as it is in constant use.

当应用程序启动时,我打开数据库,但永远不要关闭它,因为它经常使用。

Don't do that. You are apparently ignoring all the errors that are appearing in LogCat complaining that you are leaking database connections.

不要这样做。您显然忽略了LogCat中出现的所有错误,抱怨您正在泄漏数据库连接。

What is the best way to tell the database to flush all its changes to permanent storage?

告诉数据库将其所有更改刷新到永久存储的最佳方法是什么?

It will do that automatically at the end of a transaction. By default, each individual SQL operation (e.g., insert) is a transaction.

它将在事务结束时自动执行此操作。默认情况下,每个单独的SQL操作(例如,insert)都是一个事务。

Do I need to just close it and re-open or is there a more efficient way?

我需要关闭它重新打开还是有更有效的方法?

You should close your database at some point (e.g., onDestroy() of the service that is mediating your database).

您应该在某个时刻关闭您的数据库(例如,中介数据库的服务onDestroy()))。

My problem is that when testing on a phone, turning the phone off after a number of writes sometimes causes the database to loose the most recent updates when the application is restarted, which is obviously unacceptable for a database system.

我的问题是,在对手机进行测试时,在多次写操作之后关闭手机,有时会导致数据库在重新启动应用程序时丢失最新的更新,这对于数据库系统来说显然是不可接受的。

If you can create a sample project that demonstrates the problem even after you are properly closing your database connection, post it to the Android issue tracker.

如果您可以创建一个示例项目,在正确地关闭数据库连接之后演示这个问题,那么将它发布到Android问题跟踪器。

Since I cannot find out how to capture the application close event I cannot know when to manually close the database.

由于我无法找到如何捕获应用程序关闭事件,所以我不知道何时手动关闭数据库。

Close it when all activities have unbound from the service that is mediating the database connection, triggering that service's onDestroy() method.

当所有活动都从中介数据库连接的服务断开绑定时关闭它,触发该服务的onDestroy()方法。

Or, open a fresh connection in each component that needs access to the database, and use Java synchronization to ensure two threads do not try to simultaneously use the database (if needed).

或者,在需要访问数据库的每个组件中打开一个新的连接,并使用Java同步确保两个线程不会同时使用数据库(如果需要的话)。

#2


2  

If you use any transactions they are nested. Meaning if you break out of one before ending it. You'll end up with some writes rolled back when the phone/app is restarted since you never close the connection.

如果使用任何事务,它们都是嵌套的。意思是,如果你在结束之前打破了一个。当重新启动电话/应用程序时,您将会回滚一些写操作,因为您永远不会关闭连接。

db.beginTransaction();
   try {
     ...
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }

#3


-1  

Is that on a Droid? And how exactly do you turn the phone off? Unless you pull the battery out, shutting down the phone will slowly wind down the operating system, at which point it will commit everything to memory.

那是机器人吗?你到底是怎么关掉电话的?如果你不拔掉电池,关掉手机就会慢慢地关闭操作系统,这时它就会把所有东西都存储到内存中。

When exactly do you lose your database? After every app restart, or just randomly? If it's after every restart, it sounds more like you're destroying the database on startup.

您何时丢失数据库?每次应用重启后,还是随机启动?如果是在每次重启之后,听起来更像是在启动时破坏数据库。