数据库
首页 > 数据库> > python – 我应该如何在瓶子应用程序中使用sqlalchemy会话以避免在查询期间“丢失与MySQL服务器的连接”

python – 我应该如何在瓶子应用程序中使用sqlalchemy会话以避免在查询期间“丢失与MySQL服务器的连接”

作者:互联网

我的瓶子/ sqlalchemy应用程序托管在pythonanywhere.com上,不确定这是否重要,我希望不是.在我离开它一段时间之后,应用程序抛出’在查询期间丢失与MySQL服务器的连接’.然后我刷新它,它工作正常.

请注意,我也没有sqlachemy实现相同的查询,仅使用MySQLdb.该实现始终工作正常,它从不抛出异常,因为它每次都建立一个新的连接.

我假设,在像pythonanywhere这样的托管环境中,我无法摆弄可能影响此错误的mysql配置,例如max_allowed_pa​​cket或timeout.

我应该如何创建sqlalchemy引擎和会话来解决这个问题?

bottle_app.py:

db_host = 'localhost'
db_user = 'root'
db_password = 'gggggg'
db_dbname = 'test'

#web framework imports
from bottle import default_app, route, run, template, redirect

#sqlalchemy and mysql setup
mysql_connect_string = 'mysql+mysqldb://%s:%s@%s/%s?charset=utf8' % (db_user, db_password, db_host, db_dbname)
from sqlalchemy import create_engine
engine = create_engine(mysql_connect_string)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
Base.metadata.bind = engine
from sqlalchemy import Column, Integer, String, Float, SmallInteger
from sqlalchemy.orm import sessionmaker
DBSession = sessionmaker()
DBSession.bind = engine

#import mysql package for raw SQL
import MySQLdb

#sqlalchemy model class for table 'nevek'
class Person(Base):
    __tablename__ = 'nevek'
    # Here we define columns for the table nevek
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)
    lev = Column(SmallInteger)
    point = Column(Float)
    play = Column(Integer)
    kmp = Column(Float)

#get records from table nevek with sqlalchemy
def get_nevek_from_db():
    session = DBSession()
    result = session.query(Person).all()
    session.close()
    return result

#basic handler will redirect to nevek
@route('/')
def hello_world():
    redirect('/nevek')

@route('/nevek')
def nevek():
    return template('nevek-obj', nevek=get_nevek_from_db())

#get records from table nevek
def get_raw_sql(sql):
    conn = MySQLdb.connect(host=db_host, user=db_user,
        passwd=db_password, db=db_dbname, charset='utf8')
    cur = conn.cursor()
    cur.execute(sql)
    res = cur.fetchall()
    cur.close()
    conn.close()
    return res

@route('/nevek-raw')
def nevek_raw():
    return template('nevek-tuple', nevek=get_raw_sql("SELECT * FROM nevek"))


#this will be imported and run by the wsgi.py (in hosted env)
application = default_app()

#this will be used when running on your own machine
if __name__ == '__main__':
    run(application)

例外:

2015-02-25 12:43:57,107 :Traceback (most recent call last):
2015-02-25 12:43:57,108 :  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 764, in _handle
2015-02-25 12:43:57,108 :    return route.call(**args)
2015-02-25 12:43:57,108 :  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1575, in wrapper
2015-02-25 12:43:57,108 :    rv = callback(*a, **ka)
2015-02-25 12:43:57,108 :  File "/home/bpgergo/bridge/bottle_app.py", line 49, in nevek
2015-02-25 12:43:57,108 :    return template('nevek-obj', nevek=get_nevek_from_db())
2015-02-25 12:43:57,108 :  File "/home/bpgergo/bridge/bottle_app.py", line 38, in get_nevek_from_db
2015-02-25 12:43:57,109 :    result = session.query(Person).all()
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2241, in all
2015-02-25 12:43:57,109 :    return list(self)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2353, in __iter__
2015-02-25 12:43:57,109 :    return self._execute_and_instances(context)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2368, in _execute_and_instances
2015-02-25 12:43:57,109 :    result = conn.execute(querycontext.statement, self._params)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 662, in execute
2015-02-25 12:43:57,109 :    params)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
2015-02-25 12:43:57,109 :    compiled_sql, distilled_params
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
2015-02-25 12:43:57,109 :    context)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
2015-02-25 12:43:57,109 :    exc_info
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 196, in raise_from_cause
2015-02-25 12:43:57,109 :    reraise(type(exception), exception, tb=exc_tb)
2015-02-25 12:43:57,109 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
2015-02-25 12:43:57,109 :    context)
2015-02-25 12:43:57,110 :  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 324, in do_execute
2015-02-25 12:43:57,110 :    cursor.execute(statement, parameters)
2015-02-25 12:43:57,110 :  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
2015-02-25 12:43:57,110 :    self.errorhandler(self, exc, value)
2015-02-25 12:43:57,110 :  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
2015-02-25 12:43:57,110 :    raise errorclass, errorvalue
2015-02-25 12:43:57,110 :OperationalError: (OperationalError) (2013, 'Lost connection to MySQL server during query') 'SELECT nevek.id AS nevek_id, nevek.name AS nevek_name, nevek.lev AS nevek_lev, nevek.point AS nevek_point, nevek.play AS nevek_play, nevek.kmp AS nevek_kmp \nFROM nevek' ()

解决方法:

你可以试试看,让我知道

通过使用pool_recycle我认为你可以过来.

http://docs.sqlalchemy.org/en/rel_0_9/dialects/mysql.html#connection-timeouts

更换

engine = create_engine(mysql_connect_string)

通过

engine = create_engine(mysql_connect_string, pool_size=100, pool_recycle=280)

连接超时

MySQL features an automatic connection close behavior, for connections
that have been idle for eight hours or more. To circumvent having this
issue, use the pool_recycle option which controls the maximum age of
any connection:

engine = create_engine(‘mysql+mysqldb://…’, pool_recycle=3600)

标签:bottle,python,mysql,sqlalchemy,pythonanywhere
来源: https://codeday.me/bug/20190725/1528566.html