Closed salmon7 closed 4 years ago
Thanks for the detailed analysis which I think is correct.
I also recommend to add autocommit to the connect parameters instead of using setsession
.
But nevertheless, I think this should be fixed, for other uses of setsession
with pymysql.
My suggested fix would be to replace self._con.ping()
in _ping_check()
with the following:
try:
self._con.ping(False) # do not reconnect
except TypeError: # reconnect parameter not supported
self._con.ping()
This should support pymysql, and other drivers which do not have a reconnect argument.
Can you check if this works for you? I would then add this in the next version of DBUtils.
It should be self._con.ping(False) # do not reconnect
, which delete extra _
. I have try it and it work for me. Thank you very much~
👍
Sorry, yes, the _
was a typo (updated already). Good to hear this solves the problem.
I meet something strange in production. I have set setsession=["set autocommit=1"] in PooledDB and think it will be auto commit even if the connection has been reconnect.
But it seems the setsession do not work for pymysql in reconnect situation. Here is my demo.
When the demo run, try to restart mysql server, and after the application reconnects mysql successfully and then get no error return, but, actually, it will not insert to mysql for rest sql. After debug the source code I think the reason it that in this situation it reconnects by pymysql directly not by dbutils.
I add some debug code in SteadyDBConnection to help me understand the process.
For pymysql, its ping method has default reconnect behavior, and the my_reconnect variable addde for debug always will be true. It seems that dbutils assumed ping will not reconnect. So the setsession parameter will not apply to pymysql reconnect situation. Should setsession used in this way? Is it possible to add compatible parameter for driver which has reconnect parameter in its ping method?
Actually, I found another way to solve my promble by adding autocommit in __db_con__, like
My local python and package version: dbutils : 1.1 pymysql : 0.9.3 Python : 2.7.13