Django中的空闲数据库连接 [英] Idle database connections in Django

查看:261
本文介绍了Django中的空闲数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,
如何使我的代码从数据库连接中恢复,该连接已经死了?
这是场景:

 #请求进入视图处理程序

#执行代码做一些同步任务,没有任何数据库交互15分钟。 15分钟后,此请求中的第一个数据库活动


try:
o,created = act_details.objects.get_or_create(id = 1)#此行由于死数据库连接而失败。

除了异常作为e:
logger.error(e)#ORA-03114:未连接到ORACLE

假设我处理异常,有没有办法通过在异常块中创建一个新的数据库连接来将事情重新放在轨道上?



我的问题类似于从Django中的死数据库连接恢复但是不同,因为我使用的连接是完全框架管理的。



当远程服务器/网络/防火墙丢失空闲连接时,此问题仅发生在

解决方案

通常作为开发人员,您不应该以这种方式暴露在Django的内部。



由于Django在每个请求上建立连接,因此它有机会连接池并保持连接。



连接超时终止,所以你需要一种机制来提供数据库连接。



A 池模式真的是你需要的,根据你的情况,有一些提供Oracle连接池的软件包:
https://pypi.python.org/pypi/django-oraclepool



我没有尝试告诉你是否有效。



更新



另外,您可以访问底层数据库游标:

  f rom django.db import connections 

try:
o,created = act_details.objects.get_or_create(id = 1)#这行因为数据库连接不正确而失败。

除了异常作为e:
logger.error(e)#ORA-03114:未连接到ORACLE
conn = connections ['oracle']
如果conn 。连接为无:#尝试再次获得
with connection.cursor()as c:
c.execute(SELECT * from act_details WHERE id =%s%[self.id])

尽管您失去了Django ORM的所有好处,但它的解决方法也是如此。

In Django, how do I make my code recover from a database connection which is dead ? This is the scenario:

# requests enters view handler

# executes code which does some synchronous tasks without any database interaction for 15min.

# first database activity in this request after 15min.
try:
   o, created = act_details.objects.get_or_create(id=1) # this line fails because of a dead database connection.

except Exception as e:
   logger.error(e) # ORA-03114: not connected to ORACLE

Assuming I handle the exception, is there a way to put things back on track by creating a new database connection in the exception block ?

My issue is similar to Recover from dead database connection in Django but different because the connections I use are entirely framework managed.

This issue happens only when the remote server/network/firewall drops idle connections.

解决方案

Normally as a developer you shouldn't be exposed to the internals of Django that way.

As Django establishes a connection on each request it has an opportunity to both pool connections and persist connections.

Connections time out eventially anyway so you need a mechanism to provide you with database connections.

A pool pattern is really what you need and depending on your situation there are some packages that offer Connection pooling for Oracle: https://pypi.python.org/pypi/django-oraclepool

I haven't tried that to tell you if it works though.

UPDATE

Alternativelly you can access the underlying database cursor:

from django.db import connections

try:
   o, created = act_details.objects.get_or_create(id=1) # this line fails because of a dead database connection.

except Exception as e:
  logger.error(e) # ORA-03114: not connected to ORACLE
  conn = connections['oracle']
  if conn.connection is None: # Attempt to get it again 
     with connection.cursor() as c:         
        c.execute("SELECT * from act_details WHERE id = %s" % [self.id])

This way though you lose all the benefits of the Django ORM but its a workaround.

这篇关于Django中的空闲数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆