sqlalchemy list connections inside connection pool

958 views
Skip to first unread message

Oğuzhan Kaya

unread,
May 5, 2020, 11:14:59 AM5/5/20
to sqlalchemy
I made a connection with sqlalchemy to mssql and I used queue pool. I am trying to understand this connetion pool is worked or not. and also I want to see the connections and the queue inside connection pool how can I see?

engine = sal.create_engine('IP',pool_size=1,max_overflow=0)
result = engine.execute('select * from mytable')
result.close()

Mike Bayer

unread,
May 5, 2020, 12:50:22 PM5/5/20
to noreply-spamdigest via sqlalchemy
engine.pool has some metrics on it:

>>> from sqlalchemy import create_engine
>>> e = create_engine("mysql://scott:tiger@localhost/test")
>>> e.pool.status()
'Pool size: 5  Connections in pool: 0 Current Overflow: -5 Current Checked out connections: 0'
>>> e.pool.checkedout()
0
>>> c1 = e.connect()
>>> e.pool.checkedout()
1
>>> e.pool.status()
'Pool size: 5  Connections in pool: 0 Current Overflow: -4 Current Checked out connections: 1'
>>> c2 = e.connect()
>>> e.pool.status()
'Pool size: 5  Connections in pool: 0 Current Overflow: -3 Current Checked out connections: 2'
>>> c1.close()
>>> e.pool.status()
'Pool size: 5  Connections in pool: 1 Current Overflow: -3 Current Checked out connections: 1'
>>> c2.close()
>>> e.pool.status()
'Pool size: 5  Connections in pool: 2 Current Overflow: -3 Current Checked out connections: 0'
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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+...@googlegroups.com.

Mike Bayer

unread,
May 5, 2020, 12:52:09 PM5/5/20
to noreply-spamdigest via sqlalchemy
if you wanted to see the queue you could do this:

>>> [rec.connection for rec in e.pool._pool.queue]
[<_mysql.connection open to 'localhost' at 0x55ac3f6dfd20>, <_mysql.connection open to 'localhost' at 0x55ac3f6f52f0>]



but for more general tracking of connection pool connections you would use the event listeners and track them yourself:  https://docs.sqlalchemy.org/en/13/core/events.html#sqlalchemy.events.PoolEvents
Message has been deleted

Oğuzhan Kaya

unread,
May 5, 2020, 1:23:49 PM5/5/20
to sqlalchemy
Thanks mate you are a life saver. I think you mean this about event listeners. 

-----------------------------------------------------------------------------------------------------
what is dbapi_conn connection_rec and connection_proxy. I couldnt get it.

def my_on_checkout(dbapi_conn, connection_rec, connection_proxy):
    "handle an on checkout event"

event.listen(Pool, 'checkout', my_on_checkout)






5 Mayıs 2020 Salı 19:52:09 UTC+3 tarihinde Mike Bayer yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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 sqlal...@googlegroups.com.

Oğuzhan Kaya

unread,
May 6, 2020, 8:45:00 AM5/6/20
to sqlalchemy
what is mean that checkedout checkedin connection and why When connection is closed connection in pool number increases?

5 Mayıs 2020 Salı 19:50:22 UTC+3 tarihinde Mike Bayer yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.

Mike Bayer

unread,
May 6, 2020, 10:11:32 AM5/6/20
to noreply-spamdigest via sqlalchemy
the connection pool reuses connections, so when you "close" a connection, often it is recycled so that it is returned to its pooled store rather than being fully closed.

To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages