1

最近我有一个 SQLAlchemy InvalidRequestError。错误日志显示:

InvalidRequestError: Transaction <sqlalchemy.orm.session.SessionTransaction object at
0x106830dd0> is not on the active transaction list

什么情况下会报这个错误???

- - -编辑 - -

# the following two line actually in my decorator
s = Session()
s.add(model1)

# refer to <http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/>
s2 = Session().using_bind('master')

model2 = s2.query(Model2).with_lockmode('update').get(1)
model2.somecolumn = 'new'

s2.commit() 

引发此异常

-----编辑2 -----

s = Session().using_bind('master')


model = Model(user_id=123456)
s.add(model)
s.flush() 
# here, raise the exception. 
# I add log in get_bind() of RoutingSession. when doing 'flush', the _name is None, and it returns engines['slave'].  
#If I use commit() instead of flush(), then it commits successfully

我将 using_bind 方法更改如下,它运行良好。

def using_bind(self, name):
    self._name = name
    return self

上一个 RoutingSession:

class RoutingSession(Session):
    _name = None                                                                                                                                                

    def get_bind(self, mapper=None, clause=None):   
        logger.info(self._name)         
        if self._name:                                                                           
            return engines[self._name]                                          
        elif self._flushing:                                                                                                                                                                                                                  
            logger.info('master')                                                  
            return engines['master']                                               
        else:                                                                      
            logger.info('slave')    
            return engines['slave']

    def using_bind(self, name):                              
        s = RoutingSession()                                                      
        vars(s).update(vars(self))   
        s._name = name                                                                    
        return s
4

1 回答 1

2

这是一个永远不应该发生的内部断言。如果您可能不正确地以并发方式使用 Session 或操纵其内部,那么至少没有完整的堆栈跟踪就无法回答这个问题。如果我操作与 Session 对象有关的私有方法或状态,我只能显示引发的异常。

就是这样:

from sqlalchemy.orm import Session

s = Session()
s2 = Session()

t = s.transaction
t2 = s2.transaction

s2.transaction = t   # nonsensical assignment of the SessionTransaction
                     # from one Session to also be referred to by another,
                     # corrupts the transaction chain by leaving out "t2".
                     # ".transaction" should never be assigned to on the outside

t2.rollback()  # triggers the assertion case

基本上,上述情况永远不会发生,因为您不应该分配给“.transaction”。这是一个只读属性。

于 2012-12-21T05:37:44.850 回答