SQLAlchemy: Lost connection to MySQL server during query

2024/10/13 0:33:20

There are a couple of related questions regarding this, but in my case, all those solutions is not working out. Thats why I thought of asking again. I am getting this error while I am firing below query using sqlalchemy orm.

Traceback (most recent call last):
File "MyFile.py", line 1010, in <module>
handler.handle(line.split('\t'))
File "MyFile.py", line 849, in handle
self.getRecord(whatIfFlag, id)
File "MyFile.py", line 143, in getRecord
newRecord = self.recordSearcher.getRecordByParams(name, pId)
File "abc.py", line 67, in getRecord
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 2361, in one
ret = list(self)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 2404, in __iter__
return self._execute_and_instances(context)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 2419,  in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 720, in execute
return meth(self, multiparams, params)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/sql/elements.py", line 317, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 817, in _execute_clauseelement
compiled_sql, distilled_params
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 947, in _execute_context
context)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1108, in _handle_dbapi_exception
exc_info
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/util/compat.py", line 185, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 940, in _execute_context
context)
File "/opt/product/python/prod/bin/2.7.6/lib/python2.7/site-packages/SQLAlchemy-0.9.4-py2.7-linux-x86_64.egg/sqlalchemy/engine/default.py", line 435, in do_execute
cursor.execute(statement, parameters)
File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 205, in execute
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
sqlalchemy.exc.OperationalError: (OperationalError) (2013, 'Lost connection to MySQL server during query') ....query = self.__session.query(MyTable).filter(and_(MyTable.NAME == name,MyTable.P_ID == p_id)
try:record = query.one()
except NoResultFound:new_record = MyTable(params)self.__session.add(new_record)self.__session.commit()self.__session.close()

It is expected to return only one record. This is how I create my session.

sqlEngine = sqlalchemy.create_engine(self.getMySQLURI(), pool_recycle=10800, echo=False, echo_pool=False) 
session = scoped_session(sessionmaker(autoflush=True,autocommit=False,bind=sqlEngine,expire_on_commit=False))

These are my mysql configurations: interactive_timeout and wait_timeout is set to 28800 ~ 8 hours. net_write_timeout is set to 3600 ~ 60 mins and net_read_timeout is set 300 ~ 5 mins.

Any help is highly appreciated.

Answer

It is turned out to be problem with the tcp_connect_timeout between the application server and the database server. The tcp connect timeout was default of 1 hour and my pool recycle settings was 3 hrs. So anything between 1 and 3 were failing. Posting the answer to help others who might face this later.

https://en.xdnf.cn/q/118144.html

Related Q&A

row to columns while keeping part of dataframe, display on same row

I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.Resulting Dataframe:ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 1 …

SQLAlchemy InvalidRequestError when using composite foreign keys

My table relationships in SQLAlchemy have gotten quite complex, and now Im stuck at this error no matter how I configure my relationship.Im a bit new to SQLAlchemy so Im not sure what Im doing wrong, b…

google search by google api in r or python

I want to search some thing (ex:"python language") in google by python or R and it will give me the list of links for that google search like:https://en.wikipedia.org/wiki/Python_(programming…

NumPy Wont Append Arrays

Im currently working on a neural network to play Rock-Paper-Scissors, but Ive run into an enormous issue.Im having the neural network predict what will happen next based on a history of three moves, wh…

(Django) Limited ForeignKey choices by Current User in UpdateView

I recently was able to figure out how to do this in the CreateView, but the same is not working for the UpdateView (Heres the original post on how to do it in the CreateView: (Django) Limited ForeignKe…

Merge list concating unique values as comma seperated retaining original order from csv

Here is my data:data.csvid,fname,lname,education,gradyear,attributes 1,john,smith,mit,2003,qa 1,john,smith,harvard,207,admin 1,john,smith,ft,212,master 2,john,doe,htw,2000,devHere is the code:from iter…

Unable to submit Spark job from Windows IDE to Linux cluster

I just read about findspark and found it quite interesting, as so far I have only used spark-submit which isnt be suited for interactive development on an IDE. I tried executing this file on Windows 10…

Updating variable values when running a thread using QThread in PyQt4

So problem occurred when I tried using Threading in my code. What I want to do is passing default values to the def __init__ and then calling the thread with its instance with updated values but someho…

Show terminal status in a Tkinter widget

I am using python2.7.10 on MacOs Sierra and have created a rsync over ssh connection with my raspberrypi. The Idea is to synchronize my local folder with my remote folder on the raspberrypi. My functio…

Python Convert HTML into JSON using Soup

These are the rulesThe HTML tags will start with any of the following <p>, <ol> or <ul> The content of the HTML when any of step 1 tags is found will contain only the following tags: …