the Session may make use of more than one DBAPI connection, it uses basically one connection per transaction (per database host, if you have multiple) procured from the connection pool. If the pool has more than one connection pooled, you might not see the same one repeatedly. So the report of status in session here is not necessarily going to give you the right number. Also Connection.info is associated with the pooled DBAPI connection, so again this number will be the aggregate of queries among all use of this DBAPI connection, not just the one Session.

If you want to log the count on a per-session basis, you should use Session.info as the place to store your count. So to get that in after_cursor_execute you need to associate your session with your connection:


@event.listens_for(Session, "after_begin")
def _b(session, transaction, connection):
    connection.info['log_session'] = session

@event.listens_for(engine, "after_cursor_execute")
def query_count_hook(
        conn, cursor, statement, parameters, context, executemany,
):
    sess = conn.info.get('log_session', None)
    if sess:
        info = sess.info
        info['query_count'] = info.get('query_count', 0) + 1

# pop it on pool checkin, this will catch it unconditionally
# for any connection that is going to the pool for re-use
@event.listens_for(engine, "checkin")
def _ci(dbapi_conn, rec):
    rec.info.pop('log_session', None)


# can also pop it on commit/rollback, but this won't
# catch if the Session is just closed() without a commit/rollback.
# if your app always commits/rollbacks, then this is fine.
@event.listens_for(Session, "after_commit")
@event.listens_for(Session, "after_rollback")
def _c(session, transaction, connection):
    connection.info.pop('log_session', None)


On 03/21/2017 01:49 PM, Theron Luhn wrote:
Hi!

Because the SQLAlchemy ORM by default lazy-loads relationships, I've
found that the greatest performance issues are caused by burgeoning
query counts.  In order to try and keep on top of this, I've decided to
count and log the number of queries per request in my web application.

I wrote up a simple query counter which seems to work, but I'm a bit
nervous I've made a mistake, especially in my assumptions on how
sessions and connections relate.  Can you take a peek and let me know
how it looks?  https://gist.github.com/luhn/96cd88fe69e65b75166d07e6a6f42923

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to