On Wed, Mar 10, 2021, at 5:39 AM, William Edwards wrote:
> Thanks for your response. So, when calling close() on an SQLAlchemy 
> connection, it is returned to the connection pool, but the MySQL connection 
> is not actually closed. That makes sense.
> 
> Could you elaborate on your comment about garbage collection? My application 
> is short-lived: it sets up the engine, executes a few queries, and the script 
> -and thus the Python process- ends.


so yes you probably want to call .dispose() on the engine if you want to 
prevent these errors in your MySQL logs.   

I think MySQL is a bit unreasonable in that it logs these trivial disconnects 
so in our environments where we are using mariadb , we are turning down the 
log_warnings variable 
(https://mariadb.com/kb/en/error-log/#configuring-the-error-log-verbosity)  
back to 1 which they changed in 10.2.4, but I understand that solution is not 
for everyone.   We are using HAProxy so we don't have too much option.

Another approach since your script is short lived is to disable pooling using 
NullPool: 
https://docs.sqlalchemy.org/en/14/core/pooling.html#switching-pool-implementations
  that will just close out the connection when returned to the pool and won't 
pool anything.


> 
> 
> 
> 
> I'm not even sure pooling makes sense for my use case, but just so I 
> understand: why shouldn't I call dispose() on the engine to close the MySQL 
> connections, and instead rely on MySQL to close those connections? Isn't 
> MySQL's connection close logic meant for misbehaving applications that don't 
> properly close connections?

probably more of a philosophical issue.  IMO it's not practical to consider 
idle connections that aren't in a transaction state or anything else to be 
"misbehaving" if they cleanly disconnect from a TCP perspective but don't emit 
special MySQL commands first.    But engine.dispose() at the end is certainly 
the most legit way to make sure the pool is closed out.


> 

> -- 
> 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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CALC%3Dt6-VW0kKh3sR3W0doP9mRjxgGsdd9LY7sgM7daEbtwCr5g%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/CALC%3Dt6-VW0kKh3sR3W0doP9mRjxgGsdd9LY7sgM7daEbtwCr5g%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/78724e68-c71e-474c-bed7-962fdbd6427b%40www.fastmail.com.

Reply via email to