当前位置:网站首页>【踩坑记录】数据库连接未关闭连接,释放资源的坑
【踩坑记录】数据库连接未关闭连接,释放资源的坑
2022-06-23 05:01:00 【士多碧莉】
今天不聊需求,在代码review时,看到这么一段代码。大家来看一下,这段数据库代码可能引发什么问题?
def execute(self, sql):
"""执行SQL语句方法"""
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 e把关闭数据库连接写在try里面,要是在查询的时候异常了,连接和游标能释放么?
是否该连接会长期占用系统资源直到被超时回收?
在限定连接池大小的条件下,如果这种查询SQL异常过多,超过了连接池限定大小,是否会导致其他正常查询无法执行?
相信这是大部分开发都容易踩的坑,为什么这么说呢?现在有很多完善的框架,帮我们做好了ORM,大部分开发长期依赖框架,业务写的很优秀但却把基础丢了。
下面是个人认为没什么问题的版本,欢迎指教。
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()
边栏推荐
- Day_ 11 smart communication health project - graphic report and poi Report
- Pat class B 1024 scientific notation C language
- Smart port: how to realize intelligent port supervision based on the national standard gb28181 protocol easygbs?
- Pyinstaller packaging pyttsx3 error
- Simple about fastdfs
- How to add libraries for Arduino ide installation
- Pyqt5 setting window top left Icon
- 【Vivado那些事儿】XilinxCEDStore介绍
- [DaVinci developer topic] -41-app how SWC reads and writes NVM block data
- Shutter style
猜你喜欢

微软面试题:打印折纸的折痕

十一、纺织面料下架功能的实现

mongodb 4.x绑定多个ip启动报错

Day_13 傳智健康項目-第13章

Memory analysis and memory leak detection

Layer 2技术方案进展情况

mongodb 4. X binding multiple IP startup errors

Machine learning 3-ridge regression, Lasso, variable selection technique

Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中

11、 Realization of textile fabric off shelf function
随机推荐
坐标 转化
Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
Ansible uses ordinary users to manage the controlled end
Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
[cocos2d-x] screenshot sharing function
Ant Usage Summary (III): batch packaging apk
Day_ 06 smart health project - mobile terminal development - physical examination appointment
exe闪退的原因查找方法
十一、纺织面料下架功能的实现
Remove the influence of firewall and virtual machine on live555 startup IP address
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)
Repeated DNA sequences for leetcode topic resolution
Day_02 传智健康项目-预约管理-检查项管理
Day_ 11 smart communication health project - graphic report and poi Report
射频基础理论(dB)
百度URL参数之LINK?URL参数加密解密研究(代码实例)
Day_ 10 smart health project - permission control, graphic report
How to add libraries for Arduino ide installation
[DaVinci developer topic] -42- how to generate template and header files of APP SWC
Linked Storage