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
有帮助吗?

解决方案

DBSession.flush() executes SQL, but doesn't commit. You have to call DBSession.commit() to finish transaction (not need to explicitly call flush() in this case, commit() will do it). After commit() SQLAlchemy starts new transaction, but this shouldn't be a problem since data is usually locked only when you execute some SQL statement.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top