I've a situation where I've two remote services accessing a SQLite DB. Each service is opening a separate connection to the db and are doing a insert/read/delete operations. The services are working fine except when they are stopped and re-started again.
I'm getting the following exception.
"android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here"
I double, tripple checked all my database open/close and cursor open/close statements. I'm still getting this error.
I can't seem to point my finger at the problem but it looks like it has something to do w/ the two services simultaneously accessing the DB w/ connections open at the same time.
Any suggestions / pointers?
I've separate open/close methods.
private void open() {
m_database = m_helper.getWritableD开发者_如何学运维atabase();
}
private void close() {
if (m_database != null) {
m_database.close();
}
}
For every crud operation I explicitly open and close like this.
public long insert(int type, double latitude, double longitude, long timestamp, float speed, double altitude) {
SQLiteStatement insertStmt;
long status;
open();
insertStmt = m_database.compileStatement(INSERT);
insertStmt.bindLong(1, type);
insertStmt.bindDouble(2, latitude);
insertStmt.bindDouble(3, longitude);
insertStmt.bindLong(4, timestamp);
insertStmt.bindDouble(5, speed);
insertStmt.bindDouble(6, altitude);
status = insertStmt.executeInsert();
close();
return status;
}
When using compiled statements, you will want to call the close
method after your insert is complete. These DatabaseObjectNotClosedException will happen a lot on android 2.2 if one forgets to do this.
public long insert(int type, double latitude, double longitude, long timestamp, float speed, double altitude) {
SQLiteStatement insertStmt;
long status;
open();
insertStmt = m_database.compileStatement(INSERT);
insertStmt.bindLong(1, type);
insertStmt.bindDouble(2, latitude);
insertStmt.bindDouble(3, longitude);
insertStmt.bindLong(4, timestamp);
insertStmt.bindDouble(5, speed);
insertStmt.bindDouble(6, altitude);
status = insertStmt.executeInsert();
// call close here
insertStmt.close();
close();
return status;
}
It may be that since your services are occuring simultaneously. One may finish before the other and while the other is still open, the first finishing service may through the exception because the DB is still in use but not by it.
精彩评论