Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy数据库会话外部的访问表对象_Python_Mysql_Transactions_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy数据库会话外部的访问表对象

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

我有以下代码段可对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 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 ...
有关所有详细信息,请参阅。合并可能很棘手