当前位置:网站首页>[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()
边栏推荐
- Spock约束-调用频率/目标/方法参数
- How to solve CSRF attack in laravel
- Pagoda forgot password
- 306. Addenda
- The List
- Advanced drawing skills of Excel lecture 100 (VIII) -excel drawing WiFi diagram
- Nacos adapts Oracle11g create table DDL statement
- Console Application
- 897. incremental sequential search tree
- Deeplab V3 code structure diagram
猜你喜欢

How to quickly and gracefully download large files from Google cloud disk (II)

U-Net: Convolutional Networks for Biomedical Image Segmentation

Product axure9 (English version), prototype design background dynamic secondary menu display content

传智教育 | 多人协作开发出现代码冲突,如何合并代码?

Akamai-1.75 version-_ Abck parameter generation JS reverse analysis

SimpleDateFormat 线程安全问题

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

启发式的搜索策略

都是硬盘分区 C盘和D盘到底有什么区别?

Learning and using quartz scheduling framework
随机推荐
Deep learning series 47: Super sub model real esrgan
Arthas-thread命令定位线程死锁
npm下载报错npm ERR code ERESOLVE
PSP code implementation
MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts
MySQL (V) - locks and transactions
Heterogeneous transaction scenario interaction process and consistency assurance
Akamai-1.75版本-_abck参数生成-js逆向分析
链游飞船开发 农民世界链游开发 土地链游开发
用户态和内核态
[* * * array * * *]
How to quickly and gracefully download large files from Google cloud disk (II)
SSM integration
897. incremental sequential search tree
Live broadcast review | how can the container transformation of traditional applications be fast and stable?
901. stock price span
Ffplay realizes user-defined input stream playback
Simpledateformat thread safety issues
char和varchar区别
898. subarray bitwise OR operation