连接池无法向线程授予连接

问题描述 投票:0回答:4

我在 Android 中使用 GreenDAO 进行数据库处理。当执行许多数据库更改 (> 15.000) 时,我收到此错误消息:

数据库“/data/data/...”的连接池在 30.000002 秒内无法向标志为 0x1 的线程 312 (Thread-312) 授予连接。

一切都陷入困境。为什么会出现这个错误?

android sqlite greendao
4个回答
36
投票

当我想在之前没有结束事务的事务上使用的表上选择查询时,我收到此消息。在事务的finally块上执行

endTransaction()
后问题得到解决。


12
投票

我不能确定这个特定的实现,但是有一个连接池通常支持 ORM。连接池打开一定数量的数据库连接,并在您关闭它们并打开新连接时回收它们。该错误告诉您它可能已达到极限。发生这种情况的原因有很多种,其中之一是数据库中可能存在死锁,因为您正在更新两个表,并且两个不同的事务持有不同的表等待另一个表释放。或者只是因为打开的连接太多,数据库或连接池变得混乱。

抱歉,这并不是一个真正的答案,但您需要查看 GreenDAO 的文档以了解这是如何发生的。


0
投票

通过 DBFlow FlowQueryList 创建过多与 SQLite 的连接时,我收到此消息。我的解决方案是确保完成查询列表后,先调用

endTransactionAndNotify()
,然后再调用查询列表上的
close()

单独拨打

endTransactionAndNotify()
并不能解决问题。 我希望这有帮助,这个帖子确实对我有帮助。


0
投票

遇到了上述类似的场景,但它纯粹基于查询本身,而不是调用的 API。 当视图错误地覆盖表名(诚实的错误)时,就会发生这种情况,这会使数据库和所有工作线程陷入某种死锁。该视图不返回预期的表结构。不清楚为什么没有抛出错误并且程序失败,而是陷入了这种卡住状态。

12-19 19:39:28.819 14644 17079 W SQLiteConnectionPool: Connections: 1 active, 0 idle, 0 available.
12-19 19:39:28.819 14644 17079 W SQLiteConnectionPool:
12-19 19:39:28.819 14644 17079 W SQLiteConnectionPool: Requests in progress:
12-19 19:39:28.819 14644 17079 W SQLiteConnectionPool:   executeForCursorWindow started 1056586ms ago - running, sql="select * from some_table", tid=15209
12-19 19:39:29.531 14644 17040 W SQLiteConnectionPool: The connection pool for database '/data/user/0/com.google.app/databases/234827384.db' has been unable to grant a connection to thread 549 (ExecutorFactory$1) with flags 0x1 for 51.018 seconds.
12-19 19:39:29.531 14644 17040 W SQLiteConnectionPool: Connections: 1 active, 0 idle, 0 available.
12-19 19:39:29.531 14644 17040 W SQLiteConnectionPool:
12-19 19:39:29.531 14644 17040 W SQLiteConnectionPool: Requests in progress:
12-19 19:39:29.531 14644 17040 W SQLiteConnectionPool:   executeForCursorWindow started 1057297ms ago - running, sql="select * from some_table", tid=15209
12-19 19:39:29.585 14644 17056 W SQLiteConnectionPool: The connection pool for database '/data/user/0/com.google.app/databases/234827384.db' has been unable to grant a connection to thread 562 (ExecutorFactory$1) with flags 0x1 for 51.018 seconds.
12-19 19:39:29.585 14644 17056 W SQLiteConnectionPool: Connections: 1 active, 0 idle, 0 available.
12-19 19:39:29.585 14644 17056 W SQLiteConnectionPool:
12-19 19:39:29.585 14644 17056 W SQLiteConnectionPool: Requests in progress:
12-19 19:39:29.585 14644 17056 W SQLiteConnectionPool:   executeForCursorWindow started 1057351ms ago - running, sql="select * from some_table", tid=15209
12-19 19:39:29.832 14644 14736 W SQLiteConnectionPool: The connection pool for database '/data/user/0/com.google.app/databases/234827384.db' has been unable to grant a connection to thread 100 (DefaultDispatcher-worker-1) with flags 0x1 for 42.014004 seconds.

希望这对任何人都有帮助,因为这是一个很难调试的场景。

© www.soinside.com 2019 - 2024. All rights reserved.