File tree Expand file tree Collapse file tree 1 file changed +29
-15
lines changed Expand file tree Collapse file tree 1 file changed +29
-15
lines changed Original file line number Diff line number Diff line change @@ -180,6 +180,8 @@ import json
180180
181181app = Flask(__name__ )
182182
183+
184+ # 连接到 MySQL 数据库
183185conn = None
184186mb = Mybatis(conn, " mapper" , cache_memory_limit = 50 * 1024 * 1024 )
185187connection_error = False
@@ -226,24 +228,36 @@ def make_connection_and_mybatis():
226228def select_one (id :int ):
227229 pass
228230
231+ @mb.SelectMany (" SELECT * FROM fruits" )
232+ def select_many ():
233+ pass
234+
235+ def mysql_auto_reconnect (func ):
236+ def wrapper (* args , ** kwargs ):
237+ global connection_error
238+ try :
239+ ret = make_connection_and_mybatis()
240+ if ret is False :
241+ return error_string, 500
242+
243+
244+ ret = func(* args, ** kwargs)
245+ return ret, 200
246+
247+ except mysql.connector.errors.Error as e:
248+ connection_error = True
249+ return str (e), 500
250+ except Exception as e:
251+ return str (e), 500
252+ return wrapper
253+
254+
229255@app.route (' /' )
256+ @mysql_auto_reconnect
230257def hello ():
231- global connection_error
232- try :
233- ret = make_connection_and_mybatis()
234- if ret is False :
235- return error_string, 500
236-
237- # ret = mb.select_one("testBasic1", {'id':1})
238- ret = select_one(id = 2 )
239- return json.dumps(ret)
240- except mysql.connector.errors.Error as e:
241- connection_error = True
242- return str (e), 500
243- except Exception as e:
244- return str (e), 500
258+ ret = select_many()
259+ return json.dumps(ret)
245260
246261if __name__ == " __main__" :
247262 app.run(debug = True )
248-
249263```
You can’t perform that action at this time.
0 commit comments