On Fri, Sep 1, 2017 at 8:26 AM, Антонио Антуан <a.ch....@gmail.com> wrote:
> Hi guys.
> I have several questions and, possibly, misunderstandings about sqlalchemy
> connection pooling behavior
>
> 1. I try to figure out if this is normal:
> https://gist.github.com/aCLr/be78f3495892978ee868c9b5adcef0e6
>
> As you can see, I checked that `bind` is the same on `get_bind()` invokation
> and on attribute call.
> I see, that each connection into session binds only for session, not for
> bind...
> Is it correct? I expected that each connection is bound for `bind`
> (`engine`).

that's normal because your exception is when you try to execute
directly from the Engine.

this is equivalent:

    engine = create_engine(conn_string,
                       poolclass=pool.AssertionPool)
    conn = engine.connect()
    conn.scalar("select 1")  # ok
    conn.execute("select 1").scalar()  # ok
    engine.execute("select 1") # not OK, the engine doesn't know about
your "conn", it checks out a new connection


>
> 2. Here is the problem, on which I started my research:
> https://gist.github.com/aCLr/b642c822b0bd3c9ec1fe775452479acd
> I don't use commit here, only flush. But I expected that results on each
> execution will be the same, because I used same bind on query compilation
> and on `Session.query...` execution.
>
> Yes, I've read about `reset_on_return` parameter of Pool.

right, there's some odd patterns here, but it is "reset_on_return"
that is ultimately hitting the rollback here.

The query is wrong too, needs a FROM:

_query = 
Session.query(func.count(literal('*'))).select_from(Test).limit(1).statement.compile(bind=Session.bind,
dialect=Session.bind.dialect)

and the assertions need to use scalar():

assert (_query.scalar() == amount), 'failed on compiled query execution'


reset on return, as well as some nice debugging so you can see things happening:

engine = create_engine(conn_string,
                       poolclass=pool.StaticPool, echo='debug',
echo_pool='debug',
                       pool_reset_on_return=None)


And when I specify
> that parameter with `None` value, failed only first asserion ('failed on
> compiled query execution') because, as I think, another connection is used.
> My question is the same: isn't it wrong? And if it is, how can I avoid
> AsserionError on compiled query execution? Should I use private
> `_connection_for_bind` method of Session class or there are any other
> options?
>
>
>
> --
> 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.

-- 
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