Python SQLAlchemy数据库会话外部的访问表对象
我有以下代码段可对Customer表进行操作:Python SQLAlchemy数据库会话外部的访问表对象,python,mysql,transactions,sqlalchemy,Python,Mysql,Transactions,Sqlalchemy,我有以下代码段可对Customer表进行操作: with session.begin(subtransactions=True): db_obj = Customer(...) result = io_processing() # may cause greenlet switching # I do not want to make it inside the transaction above, # Because some legacy I/O related code whi
with session.begin(subtransactions=True):
db_obj = Customer(...)
result = io_processing() # may cause greenlet switching
# I do not want to make it inside the transaction above,
# Because some legacy I/O related code which will causes greenlet switching
if result:
self.read_write_db(session, db_obj)
在读写数据库功能中:
with session.begin(subtransactions=True):
# do some work on db_obj passed from outside
将事务外部的“db_obj”传递到另一个函数是否安全
或者我必须在读写数据库中再次查询db_obj并更新它?是的,这是可能的,但是您必须通过在
读写数据库的会话中合并db_obj
来获得一个新实例
with session.begin(subtransactions=True):
merged_db_obj = session.merge(db_obj)
# work with merged_db_obj ...
有关所有详细信息,请参阅。合并可能很棘手