Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

时间:2023-03-08 23:04:05
Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

最近在开发一个 App 的时候用到了数据库,可是在使用数据库的时候就出现了一些问题,在我查询表中的一些信息时出现了一下问题:

Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:677)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)

刚开始遇到的时候也是比较懵,不知道为什么应用就闪退了,刚开始也没看到错误提示,这看了之后就想会不会是因为我在某个地方把数据库链接关了所以才出现这样的情况,然后就发现还真是,我在一个异步任务中执行了好多次的插入表操作,在每次结束之后都把数据库链接给关闭了,这才导致我在下面访问数据库的时候,刚开始链接没断开,可以访问,在访问过程中,由于那个异步任务在后台把链接给关了,所以就导致了以上的错误。

解决办法:在异步的数据库操作中,在执行完毕之后,不要执行数据库的 close()函数,这样就不会出现这样的问题了~

PS:自己比较粗心,为了避免错过火车,就开发了一个应用,有需要的朋友可以试试。 火车票提醒助手

Android开发中使用数据库时出现java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.