一个 SQLite DB 和两个远程服务:android.database.sqlite.DatabaseObjectNotClosedException:

发布于 2024-12-07 18:17:46 字数 1115 浏览 0 评论 0原文

我遇到过这样的情况:我有两个远程服务访问 SQLite DB。每个服务都打开与数据库的单独连接并执行插入/读取/删除操作。这些服务工作正常,除非它们停止并重新启动。

我收到以下异常。

“android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭此处打开的游标或数据库对象”

我两次、三次检查了所有数据库打开/关闭和游标打开/关闭语句。我仍然收到此错误。

我似乎无法指出问题所在,但看起来它与两个服务同时访问数据库并同时打开连接有关。

有什么建议/指示吗?

我有单独的打开/关闭方法。

private void open() {
    m_database = m_helper.getWritableDatabase();
}

private void close() {
    if (m_database != null) {
        m_database.close();
    }
}

对于每个增删改查操作,我都像这样显式打开和关闭。

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;
}

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.getWritableDatabase();
}

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;
}

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

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

发布评论

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

评论(2

孤独陪着我 2024-12-14 18:17:46

使用编译语句时,您需要在插入完成后调用 close 方法。如果忘记这样做,这些 DatabaseObjectNotClosedException 在 android 2.2 上会经常发生。

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;
}

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;
}
偷得浮生 2024-12-14 18:17:46

可能是因为您的服务是同时发生的。一个可能会在另一个之前完成,而另一个仍然打开时,第一个完成服务可能会遇到异常,因为数据库仍在使用中,但不是由它使用。

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.

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