在特定数据库上释放turbogears2句柄(控制事务管理器)
我正在构建一个与 2 db 一起使用的涡轮齿轮应用程序 - 第二个 - 我指的是一个 mssql db - 由另一个应用程序使用(不是我的 - 我的应用程序实际上是解决问题的 hack - 所以我可以不控制其他应用程序或 mssql 数据库设置)
我正在使用 sqlalchemy(通过turbogears)写入特定的数据库表:
DBSession.add(object)
DBSession.flush()
数据被写入数据库 - 但turbogears应用程序保留某种处理数据库,因此使用该数据库表的主应用程序可以从中读取但无法更改它。直到我停止涡轮齿轮应用程序,然后一切正常。 我尝试调用:
DBSession.close()
但随后数据被神奇地从数据库中删除 - 可能是事务回滚。 我还尝试调用:
transaction.doom()
具有类似的效果(或者根本没有效果,我不确定)
我读到在涡轮齿轮中事务管理器(我猜 repoze.tm)处理提交 但我不知道 - 它什么时候被称为?我该如何控制它?特别是当函数完成预定运行时如何删除数据库句柄(我不能只是结束脚本,它是一个 cron 作业,每小时运行一次)。 tg2.1 文档在这个主题上非常不清楚
我还在某处读过我应该覆盖 commit_veto - 但不明白 - 我应该如何做以及在哪里?在我的应用程序中,我应该调用 transaction.abort() .doom() 或其他什么?
我还使用事务挂钩尝试了相同的功能,但没有成功实际调用挂钩,
感谢您的帮助。
版本数据:
- turbogears 2.1.3
- sqlalchemy 0.7
- mssql 2005
- 使用pyodbc连接mssql
I'm building a turbogears application that works with 2 db - the second one - which I'm referring to is an mssql db - used by another application (not mine - my application is actually a hack to solve a problem - so I can't control the other application or the mssql db settings)
I'm writing to a specific db table with sqlalchemy (through turbogears) using:
DBSession.add(object)
DBSession.flush()
the data is written to the db - but the turbogears application retains some sort of handle on the db, so the main applicaion using that db table can read from it but can't change it. until I stop the turbogears application and then everything works.
I tried to call:
DBSession.close()
but then the data was magically removed from the db - probably a transaction rollback.
I also tried to call:
transaction.doom()
with similiar effects (or no effect at all I'm not sure)
I read that in turbogears the transaction manager (I guess repoze.tm) handles the commits
but I can't figure - when is it called? how do I control it? and especially how to remove the db handle when the function finished it's scheduled run (I can't just end the script, it's a cron job, running every hour). the tg2.1 docs is very unclear on this subject
I also read somewhere I should override the commit_veto - but didn't understand - how should I do it and where? and where in my application I should call the transaction.abort() .doom() or whatever?
I also tried the same functions using transaction hooks but didn't succeed to actually call the hook
thanks for any help.
version data:
- turbogears 2.1.3
- sqlalchemy 0.7
- mssql 2005
- using pyodbc to connect to mssql
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DBSession.flush()
执行 SQL,但不提交。您必须调用DBSession.commit()
来完成事务(在这种情况下不需要显式调用flush()
,commit()
将做吧)。在commit()
之后,SQLAlchemy 启动新事务,但这应该不是问题,因为数据通常仅在执行某些 SQL 语句时才会被锁定。DBSession.flush()
executes SQL, but doesn't commit. You have to callDBSession.commit()
to finish transaction (not need to explicitly callflush()
in this case,commit()
will do it). Aftercommit()
SQLAlchemy starts new transaction, but this shouldn't be a problem since data is usually locked only when you execute some SQL statement.