1

I'm using Flask SQLAlchemy to connect to my mysql db, but it has defualt wait_timeout 120 seconds, so after I query my users and web isn't used for a while I get an error

(2013, 'Lost connection to MySQL server during query')

Important piece of my db.py

app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://urltomyserver'
app.config['SQLALCHEMY_POOL_RECYCLE'] = 10
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 120
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

if __name__ == 'createdb':
    db.reflect()
    db.drop_all()
    db = SQLAlchemy(app)

And piece of my views.py

  @core.route('/')
    def index():
        userzy = sftpuser.query.all()
        return render_template('index.html', userzy=userzy)
        #I'D LIKE TO CLOSE MY CONNECTION HERE

I've tried these under after I return template in def index

  db.session.close()
    db.close()
    db.dispose()
    db.session.close()
    db.engine.dispose()
    db.session.commit()

And this

@app.teardown_appcontext
def teardown_db(error):
    db.session.close()
    db.engine.dispose()

But it didnt help me that much, anyone knows the solution why do am I keep getting error even if I set the pool?

1

1 Answer 1

-1

Did yiu try to change this config: app.config['SQLALCHEMY_POOL_TIMEOUT'] = 120???

Sign up to request clarification or add additional context in comments.

6 Comments

yep, I was trying many options like 120-1, 60, 30, 40, 100, more than 120. nothing really helped, the question is why?
I also used stackoverflow.com/questions/42163359/… but didn't help me.
I also found this stackoverflow.com/questions/52992361/… soo is it possible that it's not possible to fix that on "minimal application model" I used? should I reconfigure my whole app to Declarative with engine model?
did you tried to remove app.config['SQLALCHEMY_POOL_TIMEOUT'] = 120? or to change Value to none? also ['SQLALCHEMY_POOL_TIMEOUT'] is Deprecated as of v2.4 and will be removed in v3.0. And check this stackoverflow.com/a/35640876/9988043
I'm using v2, and yes. I tried to change value to none, or remove it. In the link you post the SQLAlchemy is based on engine, my isn't. So I'm wondering if it's even possible to fix it on my app or I have to rewrite the config so it will work on engine instead of app.config
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.