Mixing of sqlalchemy and flask-sqlalchemy sessions - python

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?

Related

SQLAlchemy - Broken Pipe ERROR when trying to store large BLOB (i.e. machine learning model) to MySQL database

I would like to serialize many Gensim library Word2Vec models in MySQL database. For the purpose of my project I am utilizing: Python, Flask, SQLAlchemy, and MySQL+PyMySQL. Below you can see my Word2VecModel class which I am using in order to create word2vec table in MySQL DB:
from db.db import db
import db_models.influencer
class Word2VecModel(db.Model):
__tablename__ = "word2vec"
m_id = db.Column(db.Integer, primary_key=True)
m_username = db.Column(db.String(20), db.ForeignKey("influencer.m_username"))
m_binary = db.Column(db.LargeBinary)
influencer = db.relationship("InfluencerModel")
def __init__(self, m_binary, m_username):
self.m_binary = m_binary
self.m_username = m_username
def __repr__(self):
return '<Word2Vec {}>'.format(self.m_name)
#classmethod
def find_by_username(cls, m_username):
return cls.query.filter_by(name=m_username).first()
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
Whenever I try to execute:
word2vec_model = Word2VecModel(pickle.dumps(word2vec), username)
word2vec_model .save_to_db()
I get the following error message:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 705, in _write_bytes
self._sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 117, in <module>
word2vec.save_to_db()
File "/home/stefan/PycharmProjects/NLP - Influencer Text Analysis/src/flask_api/db_models/word2vec.py", line 25, in save_to_db
db.session.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1042, in commit
self.transaction.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 504, in commit
self._prepare_impl()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
self.session.flush()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2523, in flush
self._flush(objects)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2664, in _flush
transaction.rollback(_capture_exception=True)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2624, in _flush
flush_context.execute()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 1136, in _emit_insert_statements
statement, params
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1014, in execute
return meth(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1133, in _execute_clauseelement
distilled_params,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1318, in _execute_context
e, statement, parameters, cursor, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1512, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
Killed
I've read somewhere that increasing MySQL connection timeout might help, but not in my case. Here is my DB engine configuration:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:''#localhost/********'
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_recycle": 120}

Sqlalchemy / psycopg2 error when trying to connect to CloudSQL postgres

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

memoryError when connect() with a engine created by create_engine from sqlalchemy in flask

I want to access an oracle database through sqlalchemy in flask development. An engine is created by sqlalchemy create_engine()
MemoryError is reported when engine.connect() is called.
If setting a wrong password in the linkword, it would report:
sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-01017: invalid username/password; logon denied.
With the correct password and user name, MemoryError would appear.
It is very strange when i copy the codes into jupyter or a new project (not a flask project)in pycharm, the codes work well. I wonder if it is possible the flask has problems.
from sqlalchemy import create_engine
#test_bp.route('/testtd')
def testtd():
#the linkword has no problem because these codes works in Jupyter or a new project
linkword = 'blablabla'
engine = create_engine(linkword)
#here i got a problem. memoryError was reported.
conn = engine.connect()
ret = conn.execute('select table_name from user_tables').fetchall()
print(ret)
conn.close()
Errors reported by pycharm are as follows:
File "E:\Projects\Python\DAM2\blueprints\test.py", line 19, in testtd
conn = engine.connect()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 2196, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 103, in __init__
else engine.raw_connection()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 2296, in raw_connection
self.pool.unique_connection, _connection
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 2265, in _wrap_pool_connect
return fn()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\pool\base.py", line 649, in __connect
).exec_once(self.connection, self)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\event\attr.py", line 287, in exec_once
self(*args, **kw)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\event\attr.py", line 297, in __call__
fn(*args, **kw)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\util\langhelpers.py", line 1443, in go
return once_fn(*arg, **kw)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\strategies.py", line 199, in first_connect
dialect.initialize(c)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\dialects\oracle\cx_oracle.py", line 832, in initialize
super(OracleDialect_cx_oracle, self).initialize(connection)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\dialects\oracle\base.py", line 1140, in initialize
super(OracleDialect, self).initialize(connection)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\default.py", line 297, in initialize
connection
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\dialects\oracle\base.py", line 1239, in _get_default_schema_name
connection.execute("SELECT USER FROM DUAL").scalar()
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
return self._execute_text(object_, multiparams, params)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 1155, in _execute_text
parameters,
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 1468, in _handle_dbapi_exception
util.reraise(*exc_info)
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "C:\Users\GAC\.virtualenvs\DAM2-SwV_IMXP\lib\site-packages\sqlalchemy\engine\default.py", line 552, in do_execute
cursor.execute(statement, parameters)
MemoryError
how can i fix the codes?

SQLAlchemy: Lost connection to MySQL server during query

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.
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.

Flask-SQLAlchemy create_all()

When i run the dbManager.create_all() command, it runs with out errors but fails to create the tables. When i delete the database and run the create_all() command, i get the no such database as ##### error which i should get but when the database does exist, nothing happens.
Please can anyone see what i'm doing wrong?
from blogconfig import dbManager
class Art(dbManager.Model):
id = dbManager.Column(dbManager.Integer, primary_key = True)
title = dbManager.Column(dbManager.String(64), index = True, unique = True)
content = dbManager.Column(dbManager.Text(5000))
def __repr__(self):
return '<Art %r>' %(self.title)
EDIT
This is the shell command
from blogconfig import dbManager
>>> dbManager.create_all()
import models
>>> a = models.Art(title='des', content='asdfvhbdjbjdn')
>>> dbManager.session.add(a)
>>> dbManager.session.commit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/scoping.py", line 149, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 721, in commit
self.transaction.commit()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 354, in commit
self._prepare_impl()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 334, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1818, in flush
self._flush(objects)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1936, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 58, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1900, in _flush
flush_context.execute()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
rec.execute(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 525, in execute
uow
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
table, insert)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 569, in _emit_insert_statements
execute(statement, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 662, in execute
params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 195, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 324, in do_execute
cursor.execute(statement, parameters)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1146, "Table 'blog.art' doesn't exist") 'INSERT INTO art (title, content) VALUES (%s, %s)' ('des', 'asdfvhbdjbjdn')
dbManager will not know about the models you define in other modules unless they are imported before running create_all.
In a real application this shouldn't matter because running the flask app should set up the db and import views/blueprints to register them. Since the views use the models, the models are indirectly imported and are available to the dbManager.
Either import your models in the blogconfig module after creating the dbManager instance, or change the order of you shell commands to be
>>> from blogconfig import dbManager
>>> import models
>>> dbManager.create_all()
SQLAlchemy will only create tables, the database must already exist, which is why you're seeing the other error when you delete the database.

Categories