closing the database connection

104 views
Skip to first unread message

user2301

unread,
Mar 1, 2018, 7:21:21 AM3/1/18
to PyMySQL Users
Hi,

I am using pymysql to connect to MySQL database. My back-end functionality is to store the messages received from MQTT broker. The backend database application shall operate nonstop. I am not closing the database connection nor closing the cursor. Does this effect the performance of the database? The physical connection will not break or fail? May be at some point there will be no messages to process. Do I have to close the connection explicitly? or will it be handled by the pymysql ? 

Thanks 

INADA Naoki

unread,
Mar 1, 2018, 9:16:22 AM3/1/18
to pymysq...@googlegroups.com
> I am not closing the database connection nor closing the cursor. Does this effect the performance of the database?

I don't think so. But I don't recommend long living connection. It
cause many pitfalls.

* MySQL's wait_timeout may close connection from server side.
* When you change database setting via `SET GLOBAL` command, old
connection continue to use old configuration.
* OS or network router may close long idle connections.
* If MySQL is behind load balancer, long living connection will block
removing database server instance.
* etc...

> The physical connection will not break or fail?

Physical connection will be broken anytime, and long living connection
increase it's possibility.

> Do I have to close the connection explicitly? or will it be handled by the pymysql ?

PyMySQL doesn't close connection implicitly, because PyMySQL doesn't
use background thread or other mechanism.
You have to have connection manager (or pool) to manage connection lifetime.
Reply all
Reply to author
Forward
0 new messages