SQLAlchemy 'prepared' state problem

3,736 views
Skip to first unread message

yaro...@enkord.com

unread,
Jul 12, 2013, 3:26:32 AM7/12/13
to sqlal...@googlegroups.com

I have a problem with SQL Alchemy 0.8.1 on Python 2.7 running on linux box via mod_wsgi. From time to time it produces the "InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction". I use mod_wsgi with multiple processes of 1 thread per process.

This error happens at random - it may take a few days to see it or it might happen after a 5 minutes. I cannot understand what causes this and the biggest problem - I don't understand what this error means. What is 'prepared' state? The documentation has no information on this state. From what I saw it has some relation to two-way transactions but I do not use them in my code. I use Session.query and Session.commit.

Any help would be appreciated.

Below is detailed stack trace of the error:

...
 File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2173, in one
    ret = list(self)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2216, in __iter__
    return self._execute_and_instances(context)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>
             context = <sqlalchemy.orm.query.QueryContext object at 0x7f6f64348d50>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2229, in _execute_and_instances
    close_with_result=True)
        querycontext = <sqlalchemy.orm.query.QueryContext object at 0x7f6f64348d50>
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2220, in _connection_from_session
    **kw)
                self = <sqlalchemy.orm.query.Query object at 0x7f6f64348dd0>
                  kw = {'clause': , 'close_with_result': True, 'mapper': }

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 798, in connection
    close_with_result=close_with_result)
              mapper = 
              clause = <sqlalchemy.sql.expression.Select at 0x7f6f64348d90; Select object>
                self = <sqlalchemy.orm.session.Session object at 0x7f6f68f6fe50>
                  kw = {}
                bind = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
   close_with_result = True

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 802, in _connection_for_bind
    return self.transaction._connection_for_bind(engine)
              engine = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
                self = <sqlalchemy.orm.session.Session object at 0x7f6f68f6fe50>
              kwargs = {'close_with_result': True}

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 281, in _connection_for_bind
    self._assert_active()
                bind = Engine(mysql+mysqldb://xxx:yyy@localhost/zzz?charset=utf8)
                self = <sqlalchemy.orm.session.SessionTransaction object at 0x7f6f69be8050>

  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 181, in _assert_active
    "This session is in 'prepared' state; no further "
         rollback_ok = False
         prepared_ok = False
          closed_msg = 'This transaction is closed'
                self = <sqlalchemy.orm.session.SessionTransaction object at 0x7f6f69be8050>

InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction.
 

 


Michael Bayer

unread,
Jul 12, 2013, 10:40:37 AM7/12/13
to sqlal...@googlegroups.com
"prepared" state is an internal state in the Session which occurs within the transaction commit process.   This error can occur in one of the following ways:

1. you have an after_commit() event handler which raised an exception, leaving the Session in the "prepared" state without being able to finish its commit, then that Session was used again to try to emit a query insted of being discarded

2. you have concurrent access to the session such that one thread is calling upon session.commit() while another is calling upon a query.   I know you said that your mod_wsgi is configured for "single thread", but the pattern in which the error occurs here (very non-deterministically) strongly suggests there are is in fact more than one thread occurring.




--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

yaro...@enkord.com

unread,
Jul 12, 2013, 1:23:13 PM7/12/13
to sqlal...@googlegroups.com

You nailed it down damn straight! For some reason mod_wsgi was indead configured to use threads instead of processes. Thanks a lot for the heads up!

пятница, 12 июля 2013 г., 17:40:37 UTC+3 пользователь Michael Bayer написал:
Reply all
Reply to author
Forward
0 new messages