我使用Django 1.10作为连接到Postgres DB的ORM。
我遇到一个问题,即旧连接在数据库中的[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project blkmessenger-parent: Failed to deploy artifacts: Could not transfer artifact com.jd.terf:blkmessenger-parent:pom:1.10.0-RC1-20190627.131740-1 from/to blkmessenger.snapshots (http://jd.artifactory.net:8081/artifactory/blkmessenger/snapshots): Failed to transfer file: http://jd.artifactory.net:8081/artifactory/blkmessenger/snapshots/com/jd/terf/blkmessenger-parent/1.10.0-RC1-SNAPSHOT/blkmessenger-parent-1.10.0-RC1-20190627.131740-1.pom. Return code is: 409, ReasonPhrase: Conflict. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
状态下保持打开状态。
我的要求是:
idle
我使用以下查询来验证连接是否保持打开状态:
MyTable.objects.all().first().id
仅当我关闭我的SELECT *
FROM pg_catalog.pg_stat_activity
WHERE usename = 'my_user_name'
ORDER BY backend_start DESC limit 3;
或在代码中添加ipython
时,该信息才会消失。
根据django.db.connection.close()
文档,如果MAX_CONN_AGE设置为默认(0),则连接应在请求结束后关闭,但未按预期关闭。
答案 0 :(得分:2)
这确实令人困惑,但是文档中的“请求结束后”表示HTTP请求,而不是对数据库的请求。由于ipython会话中没有HTTP请求,因此连接保持空闲状态。参见https://docs.djangoproject.com/en/2.2/ref/signals/#module-django.core.signals
这里是注册连接关闭处理程序的地方:https://github.com/django/django/blob/21ff23bfeb4014bceaa3df27677fb68409c0634d/django/db/init.py#L60
如果要在对数据库的每个请求之后关闭连接,则必须找到与其他信号(例如,post_save
)的折衷方案,或者通过装饰器,自定义查询集,自定义手动完成信号或通过手动调用close_if_unusable_or_obsolete
。