|
@@ -28,7 +28,6 @@ class TidbConn:
|
|
|
passwd=ConfConstant.TIDB_PASSWORD,
|
|
|
port=ConfConstant.TIDB_PORT, charset="utf8")
|
|
|
|
|
|
-
|
|
|
@staticmethod
|
|
|
def get_pro_connection():
|
|
|
"""
|
|
@@ -39,7 +38,6 @@ class TidbConn:
|
|
|
passwd=ConfConstant.TIDB_PRO_PASSWORD,
|
|
|
port=ConfConstant.TIDB_PRO_PORT, charset="utf8")
|
|
|
|
|
|
-
|
|
|
@classmethod
|
|
|
def _reConn(cls, num=28800, stime=5): # 重试连接总次数为1天,这里根据实际情况自己设置,如果服务器宕机1天都没发现就......
|
|
|
_number = 0
|
|
@@ -56,10 +54,10 @@ class TidbConn:
|
|
|
time.sleep(stime) # 连接不成功,休眠3秒钟,继续循环,知道成功或重试次数结束
|
|
|
|
|
|
@classmethod
|
|
|
- def quary(cls, sql):
|
|
|
- mysql_conn = TidbConn.get_connection()
|
|
|
- TidbConn._reCon()
|
|
|
- cur = mysql_conn.cursor()
|
|
|
+ def quary(cls, conn, sql):
|
|
|
+ # mysql_conn = TidbConn.get_connection()
|
|
|
+ TidbConn._reCon(conn)
|
|
|
+ cur = conn.cursor()
|
|
|
cur.execute(sql)
|
|
|
result = cur.fetchall()
|
|
|
return result
|
|
@@ -86,23 +84,28 @@ class TidbConn:
|
|
|
|
|
|
@staticmethod
|
|
|
def upsert(conn, sql_buff):
|
|
|
- TidbConn._reCon()
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ conn.ping()
|
|
|
+ break
|
|
|
+ except OperationalError:
|
|
|
+ conn.ping(True)
|
|
|
try:
|
|
|
TidbConn.exec_sql(conn, sql_buff)
|
|
|
except Exception as e:
|
|
|
logging.error('失败详细信息:', repr(e))
|
|
|
- TidbConn.get_connection().commit()
|
|
|
+ conn.commit()
|
|
|
|
|
|
- @classmethod
|
|
|
- def _reCon(cls):
|
|
|
+ @staticmethod
|
|
|
+ def _reCon(conn):
|
|
|
""" MySQLdb.OperationalError异常"""
|
|
|
# self.con.close()
|
|
|
while True:
|
|
|
try:
|
|
|
- TidbConn.get_connection().ping()
|
|
|
+ conn.ping()
|
|
|
break
|
|
|
except OperationalError:
|
|
|
- TidbConn.get_connection().ping(True)
|
|
|
+ conn.ping(True)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|