惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
MyScale Blog
MyScale Blog
D
DataBreaches.Net
I
Intezer
GbyAI
GbyAI
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
Project Zero
Project Zero
博客园_首页
Martin Fowler
Martin Fowler
A
About on SuperTechFans
J
Java Code Geeks
AI
AI
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cisco Blogs
L
LangChain Blog
Google Online Security Blog
Google Online Security Blog
AWS News Blog
AWS News Blog
Help Net Security
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
N
Netflix TechBlog - Medium
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News
S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
雷峰网
雷峰网
V
Visual Studio Blog
T
Threat Research - Cisco Blogs

博客园 - Q.Lee.lulu

aProxy: 带认证授权和权限控制的反向代理 Nginx做前端Proxy时TIME_WAIT过多的问题 Cubieboard通过aria2和minidlna来架设家庭媒体中心 与IT&码农有关的电影和记录片 golang与node.js的http模块性能对比测试(go1) 用OpenCv来做人脸识别 linux下SublimeText的中文输入法问题之解决方案 golang与node.js的http对比测试 作为Web开发人员,我为什么喜欢Google Chrome浏览器 web.py大文件下载 Python和Node.js支持尾递归吗? 小文件、nginx、Redis、Moosefs 一道JavaScript面试题(setTimeout) 用Eclipse调试Node.js代码 Javascript中的类数组对象 Javascript正则分组命名 抛弃Fastcgi,用uwsgi来部署你的Django程序吧 Node.js:用JavaScript写服务器端程序-介绍并写个MVC框架 FaWave(发微)-Chrome上的多微博全能插件
sqlalchemy在web.py中的session使用
Q.Lee.lulu · 2011-08-10 · via 博客园 - Q.Lee.lulu

按照sqlalchemy的文档中关于sqlalchemy的session在web应用上下文的生命周期应该是:

Web Server          Web Framework        User-defined Controller Call
-------------- -------------- ------------------------------
web request
->
call controller
-> # call Session(). this establishes a new,
# contextual Session.
session
= Session()

# load some objects, save some changes
objects

= session.query(MyClass).all()

# some other code calls Session, it

's the
# same contextual session as "sess"
session2
= Session()
session2.add(foo)
session2.commit()

# generate content to be returned

return generate_content()
Session.remove()
<-
web response
<-

见:http://www.sqlalchemy.org/docs/05/session.html#lifespan-of-a-contextual-session

不过web.py的cookbook中关于使用sqlalchemy的示例中,最后并没有释放sqlalchemy的session资源,正确的应该如下:

def load_sqla(handler):
web.ctx.orm
= scoped_session(sessionmaker(bind=engine))
try:
return handler()
except web.HTTPError:
web.ctx.orm.commit()
raise
except:
web.ctx.orm.rollback()
raise
finally:
web.ctx.orm.commit()
# If the above alone doesn't work, uncomment
# the following line:
#web.ctx.orm.expunge_all()
web.ctx.orm.close() # <=- 关闭session,或者用 .remove()