在 Google Cloud SQL (GAE) Python 应用程序中管理数据库连接的好方法是什么? [英] What's a good approach to managing the db connection in a Google Cloud SQL (GAE) Python app?

查看:26
本文介绍了在 Google Cloud SQL (GAE) Python 应用程序中管理数据库连接的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在学习 Google App Engine,并且正在尝试找出一种很好的方法来管理我的数据库与 Google Cloud SQL 实例的连接(如果您还没有使用过 GC-SQL,基本上,它是云中的 MySQL,有一些限制).

I'm just learning Google App Engine and am trying to figure out a good approach to managing my database connection to a Google Cloud SQL instance (if you haven't used GC-SQL, basically, it's MySQL in the cloud, with a few limitations).

我使用 python (2.7) GAE 环境和 webapp2 框架来处理请求.我知道常见问题解答说建议每个请求都建立到数据库的新连接,但我不知道关闭连接的推荐方法是什么.每次我在开发过程中尝试删除表时,GC-SQL 都会挂起并显示进程列表".显示有一堆进程(可能是因为我没有关闭数据库)并且其中一个正在等待锁定(可能是进程试图删除表).这很烦人,并迫使我重新启动 GC-SQL 实例(就像重新启动 mysql-server 服务,我想).偶尔也会出现数据库问题,我认为这与我没有真正关闭数据库连接这一事实有关.

I'm using the python (2.7) GAE environment with the webapp2 framework for handling requests. I know the FAQ says that it's recommended that a new connection to the DB be made with each request, but I don't know what the recommended way of closing the connection is. Each time I try to drop tables during development, the GC-SQL hangs and "show processlist" shows that there are a bunch of processes (likely because I'm not closing the DB) and that one of them is waiting for a lock (likely the process trying to drop the tables). This is annoying and forces me to restart the GC-SQL instance (like restarting mysql-server service, I imagine). There are also occasional DB hiccups that I believe are related to the fact that I'm not really closing my DB connection.

那么,例如,我是否应该在我的 webapp2.Requesthandler 子类实例上使用析构函数来断开与数据库的连接?GAE 对象有时似乎会被缓存,因此这也是需要考虑的问题.我想我可以为每个查询连接/查询/断开连接,但这似乎不是最理想的.

So, for example, should I have a destructor on my webapp2.Requesthandler subclass instance to disconnect from the DB? GAE objects seem to be cached sometimes, so that's also something to consider. I suppose I could just connect/query/disconnect for each query, but this seems suboptimal.

我知道这是一个模糊的问题,但我希望在这方面玩过的人能给我一些建议.

I know this is a vague question, but I'm hoping someone who's played in this area can thow some tips my way.

提前致谢!

更新:我尝试围绕需要游标的方法实现包装器,使用 Shay 的答案作为起点.我收到 GAE 错误.这是一个专门针对此问题的新问题:来自 App Engine 的 Google Cloud SQL 的连接限制是什么,以及如何最好地重用数据库连接?

Update: I tried implementing a wrapper around methods that need a cursor, using Shay's answer as a starting point. I'm getting GAE errors. Here's a new question specific to that: What are the connection limits for Google Cloud SQL from App Engine, and how to best reuse DB connections?

推荐答案

我对 Google Cloud SQL 不熟悉,但是您不能使用 WSGI 中间件打开和关闭连接吗?

I'm not familiar with Google Cloud SQL, but couldn't you use a WSGI middleware to open and close the connection?

这篇关于在 Google Cloud SQL (GAE) Python 应用程序中管理数据库连接的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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