On Tue, Jan 15, 2019 at 3:55 PM Wugang Xu <[email protected]> wrote:
>
> I am using sqlalchemy 0.9.8 on python 2.7 with Oracle database. I tried 
> thread local strategy since we have lot of code acquiring a connection 
> without closing the current ones, assuming that the new connection will be 
> the same one as currently checked out connection.

the threadlocal strategy is being deprecated in 1.3 and I would not
advise new development using this flag.   in particular, it has no
effect with typical use of an Engine object with explicit connections
unless you call upon the contextual_connect() method, which is also
going to go away around 1.4 or so.   There are also a few stability
bugs in the threadlocal strategy that aren't going to be fixed.


> However, when I tested the following code, it wait on the seconds time 
> acquiring connection from engine until timing out. It is the same thing when 
> I tried contextual_connect instead of raw_connection. is there anything wrong 
> with my code?
>
> from sqlalchemy import create_engine
>
> def _main():
>     conn_str = ''
>     engine = create_engine(conn_str, pool_size=1, max_overflow=0, 
> pool_recycle=5, strategy='threadlocal')
>     conn=engine.raw_connection()
>     cur = conn.cursor()
>     cur.execute('insert into wugang_test values(1)')
>     cur.execute('commit')
>     # conn.close()
>     conn = engine.raw_connection()
>     cur = conn.cursor()
>     for r in cur.execute('select * from wugang_test'):
>         print r

engine.raw_connection does not seem to be calling upon the thread
local context here so you won't get any threadlocal behavior with it.
only contextual_connect() should be doing that.      it's also not
really a good idea to call "cursor.execute("commit")", the DBAPI has a
commit() method on the connection object that should be used for this
if you are working with raw DBAPI connections.

for contextual_connect(), the code should be working, here's a proof
of concept which only blocks if you remove the "threadlocal" part, so
try this out:

from sqlalchemy import create_engine

e = create_engine(
    "mysql://scott:tiger@localhost/test",
    pool_size=1,
    max_overflow=0,
    pool_recycle=5,
    strategy="threadlocal",
)

conn = e.contextual_connect()

conn2 = e.contextual_connect()

assert conn.connection.connection is conn2.connection.connection




>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to