WebwareForPython / DBUtils

Database connections for multi-threaded environments
MIT License
335 stars 52 forks source link

seesion expire problem #10

Closed hyperknob closed 5 years ago

hyperknob commented 5 years ago

I declared a flask service as the following shown to execute insert or update sql to mysql 5.7, however, About after 10 days , the service can't insert or update any data and not any exception thrown.

`@app.route('/data-sync/increment', methods=['POST']) def increment_data_sync(): if not request.json or 'dest_db' not in request.json or 'sql' not in request.json: abort(400) else: sql = request.json['sql'] dest_conn = dest_db_pool.connection() try: with dest_conn.cursor() as cursor: print(sql) cursor.execute(sql) cursor.close() except Exception: traceback.print_exc() return jsonify({'code': -1, 'msg': 'failed', 'data': None}) finally: dest_conn.close() return jsonify({'code': 0, 'msg': 'success', 'data': None})

if name == "main": ConfigParser.load_config() dest_conn_setting = {'host': ConfigParser.get_dest_db_host(), 'port': ConfigParser.get_dest_db_port(), 'user': ConfigParser.get_dest_db_usr(), 'passwd': ConfigParser.get_dest_db_passwd()} dest_db_pool = PooledDB(creator=pymysql, mincached=2, maxcached=20, maxshared=20, maxconnections=20, ping=7, blocking=False, maxusage=0, setsession=['SET AUTOCOMMIT = 1'], **dest_conn_setting) app.run(host="0.0.0.0", port=8001, debug=True)`

any suggestion? thank u.

Cito commented 5 years ago

Hi @hyperknob.

You need to find out where the execution is not passed on, using more logging. Put a log/print statement before and after the line dest_conn = ..., and before and after the cursor.execute(). Then you can see exactly where it hangs. Also, log which sql statements are executed to see if its caused by some bad sql.

Btw, executing arbitrary sql looks pretty dangerous to me, I hope in the real application you're checking the client and sql before executing it on the database.

Also, have a look at the max_connections setting of the database, and the database log files. You can also run show processlist on the database as admin to see if there's a problem.

I have web apps using DBUtils for years without rebooting. But I have not much experience with MySQL, only with PostgreSQL.