当前位置:网站首页>[pit stepping record] a pit where the database connection is not closed and resources are released
[pit stepping record] a pit where the database connection is not closed and resources are released
2022-06-23 07:32:00 【Stoblie】
Today we don't talk about needs , In the code review when , See such a piece of code . Let's see , What problems might this database code cause ?
def execute(self, sql):
""" perform SQL Sentence method """
try:
conn = self.client.connection()
cursor = conn.cursor()
cursor.execute(sql)
result = cur.fetchall()
cur.close()
conn.close()
return result
except Exception as e:
log.error(e)
raise eWrite the closing database connection in try Inside , If the query is abnormal , Can the connection and cursor be released ?
Whether the connection will occupy system resources for a long time until it is recycled over time ?
Under the condition of limiting the size of the connection pool , If this query SQL Too many exceptions , Exceeded the connection pool limit , Whether other normal queries cannot be executed ?
It is believed that this is a pit that most developers are easy to tread on , Why do you say that ? Now there are many perfect frameworks , Help us do it ORM, Most development has long relied on frameworks , The business is well written but the foundation is lost .
The following is the version that I think is OK , Welcome to advise .
try:
conn = self.client.connection()
cur = conn.cursor()
cur.execute(sql)
result = cur.fetchall()
except Exception as e:
log.error(e)
raise e
else:
return result
finally:
if cur:
cur.close()
if conn:
conn.close()
边栏推荐
- 基于51单片机的温度检测监测报警系统设计
- What is the experience of being a data product manager in the financial industry
- Heterogeneous transaction scenario interaction process and consistency assurance
- What is customer experience automation?
- npm下载报错npm ERR code ERESOLVE
- MySQL transaction isolation level
- 897. incremental sequential search tree
- 传智教育 | 项目发布前如何打tag标签及标签命名规范
- Sstable details
- ‘latin-1‘ codec can‘t encode characters in position 103-115: Body (‘一串中文‘) is not valid Latin-1
猜你喜欢

启发式的搜索策略

MySQL(四) — MySQL存储引擎

Live broadcast review | how can the container transformation of traditional applications be fast and stable?

SimpleDateFormat 线程安全问题

小爱音箱连接网络异常解决办法

Nacos adapts to oracle11g- modify the source code of Nacos

Pagoda forgot password

Data types in tensorflow

Flannel 工作原理

3DMAX plug-in development environment configuration and fileexport and utilities template testing
随机推荐
Akamai-1.75 version-_ Abck parameter generation JS reverse analysis
SSTable详解
307. area and retrieval - array modifiable
The List
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
How to achieve efficient network information dissemination
What is the experience of being a data product manager in the financial industry
‘latin-1‘ codec can‘t encode characters in position 103-115: Body (‘一串中文‘) is not valid Latin-1
【2022毕业季】从毕业到转入职场
Realization of rolling broadcast effect
TensorFlow中的数据类型
The original cloud landed in deep water, and the cloud product family of Boyun container released four values
In depth learning series 46: face image super score gfp-gan
Ntu-rgbd data set download and data format analysis
GINet
Database principle experiment test questions, about book classification table
在线文本过滤小于指定长度工具
Yolov5 detecting small targets (with source code)
Spock sub piling
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs