22285461@qq.com há 6 meses atrás
pai
commit
eeb59108b9
2 ficheiros alterados com 5 adições e 29 exclusões
  1. 4 11
      main.py
  2. 1 18
      model.py

+ 4 - 11
main.py

@@ -298,20 +298,13 @@ async def test(request: Request):
         request_origin = "unknown"
     content = {"message": "Hello World" + request_origin, "db": "disconnect!!!"}
     headers = {'Access-Control-Allow-Origin': request_origin}
+    content["db"] = "is_closed: " + str(database.is_closed()) + " is_usable:" + str(database.is_connection_usable())
     try:
-        database.connect(reuse_if_open=True)
         dt = CustomUser.select()
         for item in dt:
-            print(item.id)
-        content["db"] = "connected"
-    except OperationalError as e:
-        if 'MySQL server has gone away' in str(e):
-            content["db"] = "broke"
-            database.close()
-            database.connect()
-            if database.is_connection_usable():
-                content["db"] = "broke , reconnect"
-            logging.info("reconnect database")
+            print(item.name)
+    except Exception as e:
+        logging.info(e)
     return JSONResponse(content=content, headers=headers)
 
 

+ 1 - 18
model.py

@@ -1,4 +1,3 @@
-import logging
 from peewee import *
 from playhouse.pool import PooledMySQLDatabase
 
@@ -11,29 +10,13 @@ database = PooledMySQLDatabase('yixue',
                                stale_timeout=300, )
 
 
-def get_db():
-    db = PooledMySQLDatabase('yixue',
-                             user='yixue',
-                             password='Lai123',
-                             host='test.db.cxhy.cn',
-                             port=3306,
-                             max_connections=32,
-                             stale_timeout=300,
-                             timeout=10, )
-    logging.getLogger().setLevel(logging.INFO)
-    logging.info("get db")
-    print("get db 2222")
-
-    return db
-
-
 class UnknownField(object):
     def __init__(self, *_, **__): pass
 
 
 class BaseModel(Model):
     class Meta:
-        database = get_db()
+        database = database
 
 
 class AreaInfo(BaseModel):