文章目录
背景
Dify 1.7.1 社区版中需要使用数据库插件,执行相关的数据库查询操作。使用的插件如下:database 0.0.6

😱 存在的问题 :执行 SQL 后,数据库连接没有关闭,在生产上甚至提示连接处于 in transaction 状态中,导致其它事务卡住。

排查
在 database 中找到插件的 GitHub 地址:https://github.com/hjlarry/dify-plugin-database
有几个类似的 GitHub issue:
1、https://github.com/hjlarry/dify-plugin-database/issues/44

提到添加 {"pool_recycle":60} 可以缓解问题。查了下 pool_recycle 的参数定义:Engine Configuration --- SQLAlchemy 2.0 Documentation

可以理解为,**这个设置在指定秒数后会回收掉连接。**默认值为-1,表示永远不会回收。例如,设置为 3600,则表示连接将在一小时后回收。如果设置为 60,则表示 60 秒后会回收掉这个连接。
2、https://github.com/hjlarry/dify-plugin-database/issues/107
从代码上看,有点 bug,所以还没有啥解决办法。有的人甚至已经自己弄了 http 请求来执行 SQL。

解决
由于加上 {"pool_recycle":60} 配置只能缓解,不能根治。故考虑更换数据库插件,改为 Database Query。
更换后, 不存在数据库连接未关闭的问题了。😁

参考
1.database
3.https://github.com/hjlarry/dify-plugin-database/issues/44
4.https://github.com/hjlarry/dify-plugin-database/issues/107
相关博文
5.第 5 篇 Dify 报错解决:The length of output variable xxx must be less than 30 elements
7.第 7 篇 Dify 应用介绍 + 聊天助手&Agent 应用关键点说明
11.第 11 篇 Dify 入坑记录:插件安装报错,[ERROR]init environment failed_ failed to install dependencies