I have a flask rest api app with the following setup.
Packages installed
alembic==1.3.0
aniso8601==8.0.0
astroid==2.3.3
attrs==19.3.0
bcrypt==3.1.7
cffi==1.13.2
Click==7.0
colorama==0.4.1
Flask==1.1.1
Flask-Bcrypt==0.7.1
Flask-Migrate==2.5.2
flask-restplus==0.13.0
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.1
Flask-Testing==0.7.1
importlib-metadata==0.23
isort==4.3.21
itsdangerous==1.1.0
Jinja2==2.10.3
jsonschema==3.1.1
lazy-object-proxy==1.4.3
Mako==1.1.0
MarkupSafe==1.1.1
mccabe==0.6.1
more-itertools==7.2.0
mysqlclient==1.4.5
pycparser==2.19
PyJWT==1.7.1
pylint==2.4.3
PyMySQL==0.9.3
pyrsistent==0.15.5
python-dateutil==2.8.1
python-editor==1.0.4
pytz==2019.3
six==1.13.0
SQLAlchemy==1.3.11
Werkzeug==0.16.0
wrapt==1.11.2
zipp==0.6.0
I have following production config with debug enabled to true to know stack trace.
class ProductionConfig(Config):
DEBUG = True
user = 'xxxxxx#xxxxxx-mysqldbserver'
passs = 'xxxxxx'
host = 'xxxxxx-mysqldbserver.mysql.database.azure.com'
db = 'ifsc'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{0}:{1}#{2}/{3}'.format(user, passs, host, db)
I have __init__.py that initializes SQLAlchemy as follows
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from .config import config_by_name
db = SQLAlchemy()
flask_bcrypt = Bcrypt()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config_by_name[config_name])
db.init_app(app)
flask_bcrypt.init_app(app)
return app
I have an app.py with the following code that actually runs the flask app.
app.py
from flask_cors import CORS, cross_origin
from api import blueprint
from api.main import create_app, db
from api.main.seed import seed_db
app = create_app(os.getenv('BOILERPLATE_ENV') or 'prod') # dev|prod
CORS(app)
app.register_blueprint(blueprint)
app.app_context().push()
if __name__ == '__main__':
app.run()
Problem: I'm facing the mysql connection issues with the following error on azure's db. I've tried setting max_allowed_packet server parameter to 1073741824. But still see this error.
Symptoms: For the fresh deployment, It responds and if the API is left idle for 5 to 10 mins and make the same request, it behaves with the following error. Again it starts working after 60 mins and the behavior continues.
Stack Trace:
sqlalchemy.exc.OperationalError sqlalchemy.exc.OperationalError:
(pymysql.err.OperationalError) (2006, "MySQL server has gone away
(ConnectionResetError(10054, 'An existing connection was forcibly
closed by the remote host', None, 10054, None))") [SQL: SELECT
bank_details.id AS bank_details_id, bank_details.bank AS
bank_details_bank, bank_details.ifsc AS bank_details_ifsc,
bank_details.branch AS bank_details_branch, bank_details.address AS
bank_details_address, bank_details.district AS bank_details_district,
bank_details.city AS bank_details_city, bank_details.state AS
bank_details_state, bank_details.phone AS bank_details_phone,
bank_details.micr AS bank_details_micr, bank_details.pin AS
bank_details_pin FROM bank_details WHERE bank_details.bank =
%(bank_1)s AND bank_details.city = %(city_1)s] [parameters: {'bank_1':
'axis bank', 'city_1': 'goa'}] (Background on this error at:
http://sqlalche.me/e/e3q8)
Traceback (most recent call last) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 713, in _write_bytes self._sock.sendall(data) During handling of
the above exception, another exception occurred: File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 1245, in _execute_context self.dialect.do_execute( File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\default.py",
line 581, in do_execute cursor.execute(statement, parameters) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line
170, in execute result = self._query(query) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line
328, in _query conn.query(q) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 516, in query self._execute_command(COMMAND.COM_QUERY, sql) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 771, in _execute_command self._write_bytes(packet) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 716, in _write_bytes raise err.OperationalError( The above
exception was the direct cause of the following exception: File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2463,
in
call return self.wsgi_app(environ, start_response) File "E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2449,
in wsgi_app response = self.handle_exception(e) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py",
line 584, in error_router return original_handler(e) File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1866,
in handle_exception reraise(exc_type, exc_value, tb) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_compat.py", line
38, in reraise raise value.with_traceback(tb) File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 2446,
in wsgi_app response = self.full_dispatch_request() File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1951,
in full_dispatch_request rv = self.handle_user_exception(e) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py",
line 584, in error_router return original_handler(e) File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1820,
in handle_user_exception reraise(exc_type, exc_value, tb) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_compat.py", line
38, in reraise raise value.with_traceback(tb) File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1949,
in full_dispatch_request rv = self.dispatch_request() File
"E:\sampled\sample_api\env\Lib\site-packages\flask\app.py", line 1935,
in dispatch_request return
self.view_functionsrule.endpoint File
"E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\api.py",
line 325, in wrapper resp = resource(*args, **kwargs) File
"E:\sampled\sample_api\env\Lib\site-packages\flask\views.py", line 89,
in view return self.dispatch_request(*args, **kwargs) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\resource.py",
line 44, in dispatch_request resp = meth(*args, **kwargs) File
"E:\sampled\sample_api\env\Lib\site-packages\flask_restplus\marshalling.py",
line 243, in wrapper resp = f(*args, **kwargs) File
"E:\sampled\sample_api\api\main\controller\ifsc_controller.py", line
37, in get banks = get_banks_by_name_city(bank, city) File
"E:\sampled\sample_api\api\main\service\ifsc_service.py", line 28, in
get_banks_by_name_city return Ifsc.query.filter_by(bank = name, city =
city).all() File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py",
line 3211, in all return list(self) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py",
line 3367, in iter return self._execute_and_instances(context)
File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\orm\query.py",
line 3392, in _execute_and_instances result =
conn.execute(querycontext.statement, self._params) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 982, in execute return meth(self, multiparams, params) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\sql\elements.py",
line 287, in _execute_on_connection return
connection._execute_clauseelement(self, multiparams, params) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 1095, in _execute_clauseelement ret = self._execute_context( File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 1249, in _execute_context self._handle_dbapi_exception( File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\util\compat.py",
line 398, in raise_from_cause reraise(type(exception), exception,
tb=exc_tb, cause=cause) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\util\compat.py",
line 152, in reraise raise value.with_traceback(tb) File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\base.py",
line 1245, in _execute_context self.dialect.do_execute( File
"E:\sampled\sample_api\env\Lib\site-packages\sqlalchemy\engine\default.py",
line 581, in do_execute cursor.execute(statement, parameters) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line
170, in execute result = self._query(query) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\cursors.py", line
328, in _query conn.query(q) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 516, in query self._execute_command(COMMAND.COM_QUERY, sql) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 771, in _execute_command self._write_bytes(packet) File
"E:\sampled\sample_api\env\Lib\site-packages\pymysql\connections.py",
line 716, in _write_bytes raise err.OperationalError(
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006,
"MySQL server has gone away (ConnectionResetError(10054, 'An existing
connection was forcibly closed by the remote host', None, 10054,
None))") [SQL: SELECT bank_details.id AS bank_details_id,
bank_details.bank AS bank_details_bank, bank_details.ifsc AS
bank_details_ifsc, bank_details.branch AS bank_details_branch,
bank_details.address AS bank_details_address, bank_details.district AS
bank_details_district, bank_details.city AS bank_details_city,
bank_details.state AS bank_details_state, bank_details.phone AS
bank_details_phone, bank_details.micr AS bank_details_micr,
bank_details.pin AS bank_details_pin FROM bank_details WHERE
bank_details.bank = %(bank_1)s AND bank_details.city = %(city_1)s]
[parameters: {'bank_1': 'axis bank', 'city_1': 'goa'}] (Background on
this error at: http://sqlalche.me/e/e3q8) The debugger caught an
exception in your WSGI application. You can now look at the traceback
which led to the error. To switch between the interactive traceback
and the plaintext one, you can click on the "Traceback" headline. From
the text traceback you can also create a paste of it. For code
execution mouse-over the frame you want to debug and click on the
console icon on the right side.
You can execute arbitrary Python code in the stack frames and there
are some extra helpers available for introspection:
dump() shows all variables in the frame dump(obj) dumps all that's
known about the object
After 4 months of research and understanding how flask works, I made the following change by pushing the app_context to initialize the database resolved the issue.
with app.app_context():
db.init_app(app)
Point to note:
We must ensure that app context being pushed along with sqlalchemy database initialization. In the posted question, I've been initializing db and then I was pushing the app context later app.app_context().push().
References: Manually Push a Context
In MySQL server set these :
MAX_EXECUTION_TIME is used for long running queries.
SET SESSION MAX_EXECUTION_TIME=20000;
SET GLOBAL MAX_EXECUTION_TIME=200000;
Also set wait_timeout:
SET session wait_timeout=30000;
SET ##GLOBAL.wait_timeout=30000;
Related
I have a webapp using Python, Flask and Flask-Security-Too. When running locally against a deployed database (also in Azure) It's working as intended. When the application is deployed to Azure it also works nicely. However, after a period of time it will start to throw errors like the one below:
2021-11-25T08:33:20.676633302Z [2021-11-25 08:33:20 +0000] [8] [ERROR] Error handling request /login
2021-11-25T08:33:20.676686905Z Traceback (most recent call last):
2021-11-25T08:33:20.676694906Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1202, in _execute_context
2021-11-25T08:33:20.676700406Z conn = self._revalidate_connection()
2021-11-25T08:33:20.676705406Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 469, in _revalidate_connection
2021-11-25T08:33:20.676710407Z raise exc.InvalidRequestError(
2021-11-25T08:33:20.676715107Z sqlalchemy.exc.InvalidRequestError: Can't reconnect until invalid transaction is rolled back
2021-11-25T08:33:20.676720007Z
2021-11-25T08:33:20.676724408Z The above exception was the direct cause of the following exception:
2021-11-25T08:33:20.676729108Z
2021-11-25T08:33:20.676733508Z Traceback (most recent call last):
2021-11-25T08:33:20.676738108Z File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
2021-11-25T08:33:20.676742809Z self.handle_request(listener, req, client, addr)
2021-11-25T08:33:20.676747309Z File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
2021-11-25T08:33:20.676752109Z respiter = self.wsgi(environ, resp.start_response)
2021-11-25T08:33:20.676756710Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
2021-11-25T08:33:20.676761610Z return self.wsgi_app(environ, start_response)
2021-11-25T08:33:20.676766110Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
2021-11-25T08:33:20.676770711Z response = self.handle_exception(e)
2021-11-25T08:33:20.676775311Z File "/usr/local/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function
2021-11-25T08:33:20.676780011Z return cors_after_request(app.make_response(f(*args, **kwargs)))
2021-11-25T08:33:20.676784712Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
2021-11-25T08:33:20.676789412Z reraise(exc_type, exc_value, tb)
2021-11-25T08:33:20.676794012Z File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
2021-11-25T08:33:20.676798612Z raise value
2021-11-25T08:33:20.676813213Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
2021-11-25T08:33:20.676818114Z response = self.full_dispatch_request()
2021-11-25T08:33:20.676822214Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
2021-11-25T08:33:20.676826514Z rv = self.handle_user_exception(e)
2021-11-25T08:33:20.676830615Z File "/usr/local/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function
2021-11-25T08:33:20.676834915Z return cors_after_request(app.make_response(f(*args, **kwargs)))
2021-11-25T08:33:20.676840015Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
2021-11-25T08:33:20.676844515Z reraise(exc_type, exc_value, tb)
2021-11-25T08:33:20.676848616Z File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
2021-11-25T08:33:20.676852916Z raise value
2021-11-25T08:33:20.676874217Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
2021-11-25T08:33:20.676878918Z rv = self.dispatch_request()
2021-11-25T08:33:20.676883218Z File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
2021-11-25T08:33:20.676888318Z return self.view_functions[rule.endpoint](**req.view_args)
2021-11-25T08:33:20.676892919Z File "/usr/local/lib/python3.8/site-packages/flask_security/decorators.py", line 436, in decorated
2021-11-25T08:33:20.676897419Z return fn(*args, **kwargs)
2021-11-25T08:33:20.676901819Z File "/usr/local/lib/python3.8/site-packages/flask_security/views.py", line 162, in login
2021-11-25T08:33:20.676906220Z if form.validate_on_submit():
2021-11-25T08:33:20.676910620Z File "/usr/local/lib/python3.8/site-packages/flask_wtf/form.py", line 100, in validate_on_submit
2021-11-25T08:33:20.676915220Z return self.is_submitted() and self.validate()
2021-11-25T08:33:20.676919720Z File "/usr/local/lib/python3.8/site-packages/flask_security/forms.py", line 450, in validate
2021-11-25T08:33:20.676924121Z self.user = find_user(self.email.data)
2021-11-25T08:33:20.676928321Z File "/usr/local/lib/python3.8/site-packages/flask_security/utils.py", line 829, in find_user
2021-11-25T08:33:20.676932921Z user = _datastore.find_user(
2021-11-25T08:33:20.676937122Z File "/usr/local/lib/python3.8/site-packages/flask_security/datastore.py", line 585, in find_user
2021-11-25T08:33:20.676941622Z return query.filter(subquery).first()
2021-11-25T08:33:20.676945822Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3429, in first
2021-11-25T08:33:20.676950422Z ret = list(self[0:1])
2021-11-25T08:33:20.676954623Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3203, in __getitem__
2021-11-25T08:33:20.676959023Z return list(res)
2021-11-25T08:33:20.676967724Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3535, in __iter__
2021-11-25T08:33:20.676972424Z return self._execute_and_instances(context)
2021-11-25T08:33:20.676976724Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3560, in _execute_and_instances
2021-11-25T08:33:20.676981224Z result = conn.execute(querycontext.statement, self._params)
2021-11-25T08:33:20.676985525Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
2021-11-25T08:33:20.676989925Z return meth(self, multiparams, params)
2021-11-25T08:33:20.676994525Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
2021-11-25T08:33:20.676999126Z return connection._execute_clauseelement(self, multiparams, params)
2021-11-25T08:33:20.677003426Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1124, in _execute_clauseelement
2021-11-25T08:33:20.677007926Z ret = self._execute_context(
2021-11-25T08:33:20.677012127Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1206, in _execute_context
2021-11-25T08:33:20.677016627Z self._handle_dbapi_exception(
2021-11-25T08:33:20.677020827Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1510, in _handle_dbapi_exception
2021-11-25T08:33:20.677025327Z util.raise_(
2021-11-25T08:33:20.677029528Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
2021-11-25T08:33:20.677034028Z raise exception
2021-11-25T08:33:20.677038128Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1202, in _execute_context
2021-11-25T08:33:20.677042729Z conn = self._revalidate_connection()
2021-11-25T08:33:20.677046929Z File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 469, in _revalidate_connection
2021-11-25T08:33:20.677051429Z raise exc.InvalidRequestError(
2021-11-25T08:33:20.677055629Z sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back
2021-11-25T08:33:20.677061730Z [SQL: SELECT anon_1.user_id AS anon_1_user_id, anon_1.user_email AS anon_1_user_email, anon_1.user_username AS anon_1_user_username, anon_1.user_password AS anon_1_user_password, anon_1.user_active AS anon_1_user_active, anon_1.user_fs_uniquifier AS anon_1_user_fs_uniquifier, anon_1.user_confirmed_at AS anon_1_user_confirmed_at, anon_1.user_last_login_at AS anon_1_user_last_login_at, anon_1.user_current_login_at AS anon_1_user_current_login_at, anon_1.user_last_login_ip AS anon_1_user_last_login_ip, anon_1.user_current_login_ip AS anon_1_user_current_login_ip, anon_1.user_login_count AS anon_1_user_login_count, anon_1.user_tf_primary_method AS anon_1_user_tf_primary_method, anon_1.user_tf_totp_secret AS anon_1_user_tf_totp_secret, anon_1.user_tf_phone_number AS anon_1_user_tf_phone_number, anon_1.user_create_datetime AS anon_1_user_create_datetime, anon_1.user_update_datetime AS anon_1_user_update_datetime, anon_1.user_uuid AS anon_1_user_uuid, role_1.id AS role_1_id, role_1.name AS role_1_name, role_1.description AS role_1_description, role_1.permissions AS role_1_permissions, role_1.update_datetime AS role_1_update_datetime
2021-11-25T08:33:20.677072630Z FROM (SELECT TOP 1 [user].id AS user_id, [user].email AS user_email, [user].username AS user_username, [user].password AS user_password, [user].active AS user_active, [user].fs_uniquifier AS user_fs_uniquifier, [user].confirmed_at AS user_confirmed_at, [user].last_login_at AS user_last_login_at, [user].current_login_at AS user_current_login_at, [user].last_login_ip AS user_last_login_ip, [user].current_login_ip AS user_current_login_ip, [user].login_count AS user_login_count, [user].tf_primary_method AS user_tf_primary_method, [user].tf_totp_secret AS user_tf_totp_secret, [user].tf_phone_number AS user_tf_phone_number, [user].create_datetime AS user_create_datetime, [user].update_datetime AS user_update_datetime, [user].uuid AS user_uuid
2021-11-25T08:33:20.677078531Z FROM [user]
2021-11-25T08:33:20.677082731Z WHERE lower([user].email) = lower(?)) AS anon_1 LEFT OUTER JOIN (roles_users AS roles_users_1 JOIN role AS role_1 ON role_1.id = roles_users_1.role_id) ON anon_1.user_id = roles_users_1.user_id]
2021-11-25T08:33:20.677087431Z [parameters: [immutabledict({})]]
AFAIK, all the db.session's and user_datastore entities are either rolled back, commited and/or closed after they're used. Here's an example:
def create_user():
try:
current_app.logger.info('Creating admin user...')
setup_admin_user(current_app.user_datastore)
current_app.logger.info('Done!')
except IntegrityError as ie:
current_app.logger.warning('Failed to create admin user(s) as they already exists.')
db.session.rollback()
except Exception as e:
db.session.rollback()
raise e
finally:
db.session.close()
def setup_admin_user(usr_datastore):
if not usr_datastore.find_user(email="email#example.com"):
usr_datastore.create_user(email="email#example.com",
password=hash_password('password'),
roles=["admin"],
confirmed_at=datetime.datetime.utcnow())
usr_datastore.commit()
I also noticed that the parameters for the provided SQL are looking a bit suspicious. Sometimes there's a real parameter but the error message is still the same.
I'm confused as the query does work properly when executing it in the database and the Traceback does not mention any lines in the code I've written.
Any idea's on how I can fix this? I've tried bumping the versions for flask-sqlalchemy and flask-security-too to the latest stable versions.
This sometimes happens since often, cloud DB instances will close idle connections. Make sure you have the following in your config:
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_pre_ping": True}
and see if that helps.
Solved it by configuring a health check for the app service in Azure. The health check-endpoint will do a simple query to the database in order to keep it alive, which I believe was the problem.
def health_check():
role = current_app.user_datastore.find_role('dummy')
if role:
current_app.logger.info(f'[health_check] Found role {role.name}')
return jsonify(status='ok'), 200
return jsonify(status='failure'), 500
I am trying to turn logging off from sqlalchemy completely. I am creating a custom logging framework for Flask and errors from sqlalchemy/engine/base.py, sqlalchemy/engine/impl.py keep showing up..
I tried doing: logging.getLogger('SQLAlchemy.pool.base').addHandler(logging.NullHandler())
and
logging.getLogger('SQLAlchemy.pool.impl').addHandler(logging.NullHandler())
but they didn't work because they don't leverage python logging traditionally. Instead of having logger=logging.getLogger(__name__) they import from a log file in the library and so I then tried:
logging.getLogger('sqlalchemy.log').addHandler(logging.NullHandler()) instead, but that didn't work either.
I tried the following, but those didn't pan out either:
How to turn sqlalchemy logging off completely
https://docs.sqlalchemy.org/en/14/core/engines.html#more-on-the-echo-flag
Here is the code:
from sqlalchemy import create_engine
from snowflake.sqlalchemy import URL
#app.route("/")
#app.route("/home")
def home():
engine = create_engine('snowflake://{user}:{password}#{account}/'.format(user='user-name', password='password', account='account'), echo=False, echo_pool=False)
try:
connection = engine.connect()
results = connection.execute('select current_version()').fetchone()
print(results[0])
connection.close()
finally:
engine.dispose()
return render_template('home.html', posts=posts)
and here is the errors:
raceback (most recent call last):
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 606, in __connect
pool.logger.debug("Created new connection %r", connection)
TypeError: debug() takes 1 positional argument but 3 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sample_projects/app.py", line 69, in home
connection = engine.connect()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3165, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
else engine.raw_connection()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3244, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3211, in _wrap_pool_connect
return fn()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 84, in __exit__
compat.raise_(value, with_traceback=traceback)
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
TypeError: debug() takes 1 positional argument but 3 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "sample_projects/env/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "sample_projects/app.py", line 76, in home
engine.dispose()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2920, in dispose
self.pool.dispose()
File "sample_projects/env/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 195, in dispose
self.logger.info("Pool disposed. %s", self.status())
TypeError: info() takes 1 positional argument but 3 were given
I would appreciate any insight that you might have. Ask me questions if I forgot to address something. Thank you if you read this far haha!
The following doesn't work because you're adding a handler on top of the existing handlers.
logger = logging.getLogger('sqlalchemy')
logger.addHandler(logging.NullHandler()) # creates an additional log handler
What you actually want to do is modify the existing handler.
logger = logging.getLogger('sqlalchemy')
logger.setLevel(logging.ERROR) # modifies the current log handler
I have created a form in html with some basic information like Website, Link, Name, Last_name, email and price. I am trying to add new columns into my sql database from the html forms.
##When I do it in this way it works fine:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:password#localhost/db_name'
db=SQLAlchemy(app)
class Feedback(db.Model):
__tablename__='user'
website=db.Column('website',db.String(20))
link=db.Column('link',db.String(200))
name=db.Column('name',db.String(20),primary_key=True)
l_name=db.Column('l_name',db.String(20))
email_=db.Column('email_',db.String(40))
price=db.Column('price',db.Integer)
def __init__(self,a,b,c,d,e,f):
self.website=a
self.link=b
self.name=c
self.l_name=d
self.email_=e
self.price=f
a=''1''
b='Name'
c='n'
d='k'
e='j'
f=10
data=Feedback(a,b,c,d,e,f)
db.session.add(data)
db.session.commit()
#When I try to do the same thing with the values from the HTML form I get an Operational Error
from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:password#localhost/db_name'
db=SQLAlchemy(app)
class Feedback(db.Model):
__tablename__='user'
website=db.Column('website',db.String(20))
link=db.Column('link',db.String(200))
name=db.Column('name',db.String(20),primary_key=True)
l_name=db.Column('l_name',db.String(20))
email_=db.Column('email_',db.String(40))
price=db.Column('price',db.Integer)
def __init__(self,a,b,c,d,e,f):
self.website=a
self.link=b
self.name=c
self.l_name=d
self.email_=e
self.price=f
#app.route('/',methods=['GET','POST'])
def index():
if request.method=='POST':
#fetch the data
userDetails=request.form
a=userDetails['website']
b=userDetails['link']
c=userDetails['name']
d=userDetails['last_name']
e=userDetails['email']
f=userDetails['price']
data=Feedback(a,b,c,d,e,f)
db.session.add(data)
db.session.commit()
return ('Done')
return render_template('atlas_tracker.html')
if __name__=='__main__':
app.run()
Here is my Error message:
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 588, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\nitin\anaconda3\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\nitin\Repositories\Projects\Flipkart and Amazon Price Tracker\app.py", line 41, in index
db.session.commit()
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 1036, in commit
self.transaction.commit()
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 503, in commit
self._prepare_impl()
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 482, in _prepare_impl
self.session.flush()
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2479, in flush
self._flush(objects)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2617, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2577, in _flush
flush_context.execute()
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 422, in execute
rec.execute(self)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 589, in execute
uow,
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\persistence.py", line 245, in save_obj
insert,
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\orm\persistence.py", line 1084, in _emit_insert_statements
c = cached_connections[connection].execute(statement, multiparams)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
return meth(self, multiparams, params)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1101, in _execute_clauseelement
distilled_params,
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "C:\Users\nitin\anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 588, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: INSERT INTO user (website, link, name, l_name, email_, price) VALUES (?, ?, ?, ?, ?, ?)]
[parameters: ('Amazon', 'www.amazon.com', 'Name ', 'jjjj', 'email#live.com', '1200')]
(Background on this error at: http://sqlalche.me/e/e3q8)
127.0.0.1 - - [26/Jun/2020 23:30:14] "POST / HTTP/1.1" 500 -
Try to add this code in your app.py file
#app.before_first_request
def create_tables():
db.create_all()
It will create tables before first request
I am spinning up redash using helm on GKE with master/node version 1.12.10-gke.17 and istio 1.1.15
I use as database a GCP CloudSQL with postgres 9.6
When trying to execute the create_db script from the redash image (7.0.0.b18042) I encounter the following error
Traceback (most recent call last):
File "/app/manage.py", line 9, in <module>
manager()
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 345, in main
return AppGroup.main(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 229, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/app/redash/cli/database.py", line 31, in create_tables
db.create_all()
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 963, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 955, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", line 4005, in create_all
tables=tables)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1939, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1932, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2123, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2162, in _wrap_pool_connect
e, dialect, self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception_noconnection
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2158, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 400, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 788, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 529, in checkout
rec = pool._do_get()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1193, in _do_get
self._dec_overflow()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 1190, in _do_get
return self._create_connection()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 347, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 474, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 671, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 106, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 412, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
(Background on this error at: http://sqlalche.me/e/e3q8)
I cannot figure out what is causing this, i.e. whether it is the CloudSQL's issue, or some istio misconfiguration or sth else...
The same error (with smaller stack trace) is reproduced when I exec into a container and try to manually connect to CloudSQL
>>> import psycopg2
>>> conn = psycopg2.connect(host="192.44.33.1",database="postgres", user="postgres", password="mypass")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Have you enabled a ServiceEntry within the Istio mesh to allow egress traffic to CloudSQL?
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: www.googleapis.com
spec:
hosts:
- www.googleapis.com
- oauth2.googleapis.com
ports:
- number: 443
name: https
protocol: HTTPS
resolution: DNS
location: MESH_EXTERNAL
Further reading here: https://github.com/istio/istio/issues/6593
The issue I am having is with the mixing of sqlalchemy sessions with flask-sqlalchemy session. So for context, I am running a flask-restful app on the background and I have a file (runnable.py) with a main method which is supposed to run periodically for an average period of 4 weeks.
This file uses its own session (pure sqlalchemy session) and connects to this app (which uses flask-sqlalchemy session).
After a few hours (sometimes 1 day), I get this error
Traceback (most recent call last):
File "/home/ubuntu/cron/runnable.py", line 781, in <module>
main()
File "/home/ubuntu/cron/runnable.py", line 677, in main
db_process_attendee(attendee, eid, event_id)
File "/home/ubuntu/cron/runnable.py", line 78, in db_process_attendee
update_attendee_information(attendee, attributes, eid, event_id)
File "/home/ubuntu/cron/runnable.py", line 117, in update_attendee_information
update_attendee_profile(attendee, id_attendee_login, eid, event_id)
File "/home/ubuntu/cron/runnable.py", line 291, in update_attendee_profile
generate_company_tags_lower.generate_company_tags(id_attendee_login)
File "/home/ubuntu/cron/app/util/generate_company_tags_lower.py", line 28, in generate_company_tags
filter(a_p.id_attendee_login == id_attendee_login).\
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2426, in scalar
ret = self.one()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2395, in one
ret = list(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2438, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2453, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 729, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 322, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 826, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 958, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 951, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 436, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 134, in execute
result = self._query(query)
File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 282, in _query
conn.query(q)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 767, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 957, in _execute_command
self._write_bytes(prelude + sql[:chunk_size-1])
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 916, in _write_bytes
raise err.OperationalError(2006, "MySQL server has gone away (%r)" % (e,))
sqlalchemy.exc.OperationalError: (OperationalError) (2006, "MySQL server has gone away (error(110, 'Connection timed out'))") 'SELECT a_p.id_attendee_profile AS a_p_id_attendee_profile, a_p.id_attendee_login AS a_p_id_attendee_login \nFROM a_p \nWHERE a_p.id_attendee_login = %s' (128368,)
I tried the following suggestions but they did not work:
• In my app config, adding "pymysql" to the SQLALCHEMY_DATABASE_URI
• Doing a db.session.commit() when I enter generate_company_tags(id_attendee_login) before writing a sql select query. I thought it would flush the session.
Btw, the sqlalchemy pool recycle value is set to less than the max in AWS RDS.
Any suggestions on how I can go solving this?