TidbProConn.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/15 11:06 上午
  4. # @Site :
  5. # @File : TidbProConn.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. import logging
  9. import time
  10. from sqlite3 import OperationalError
  11. import pymysql
  12. from Apietl.constant.ConfConstant import ConfConstant
  13. class TidbProConn:
  14. """
  15. Tidb连接管理
  16. """
  17. @staticmethod
  18. def get_connection():
  19. """
  20. 获取Tidb连接
  21. :return: 连接
  22. """
  23. return pymysql.connect(host=ConfConstant.TIDB_PRO_HOST, user=ConfConstant.TIDB_PRO_USER,
  24. passwd=ConfConstant.TIDB_PRO_PASSWORD,
  25. port=ConfConstant.TIDB_PRO_PORT, charset="utf8")
  26. @classmethod
  27. def _reConn(cls, num=28800, stime=5): # 重试连接总次数为1天,这里根据实际情况自己设置,如果服务器宕机1天都没发现就......
  28. _number = 0
  29. _status = True
  30. while _status and _number <= num:
  31. try:
  32. TidbProConn.get_connection().ping() # cping 校验连接是否异常
  33. _status = False
  34. except:
  35. if TidbProConn.get_connection() == True: # 重新连接,成功退出
  36. _status = False
  37. break
  38. _number += 1
  39. time.sleep(stime) # 连接不成功,休眠3秒钟,继续循环,知道成功或重试次数结束
  40. @classmethod
  41. def quary(cls, sql):
  42. mysql_conn = TidbProConn.get_connection()
  43. TidbProConn._reCon()
  44. cur = mysql_conn.cursor()
  45. cur.execute(sql)
  46. result = cur.fetchall()
  47. return result
  48. @staticmethod
  49. def conn_close(conn: pymysql.connections.Connection):
  50. """
  51. 关闭资源
  52. """
  53. if conn:
  54. try:
  55. conn.close()
  56. except pymysql.err.Error:
  57. pass
  58. @staticmethod
  59. def exec_sql(conn, sql_buff):
  60. sql_list = sql_buff.split(";")
  61. cur = conn.cursor()
  62. for sql in sql_list:
  63. if sql.strip():
  64. cur.execute(sql)
  65. return cur
  66. @staticmethod
  67. def upsert(conn, sql_buff):
  68. TidbProConn._reCon()
  69. try:
  70. TidbProConn.exec_sql(conn, sql_buff)
  71. except Exception as e:
  72. logging.error('失败详细信息:', repr(e))
  73. TidbProConn.get_connection().commit()
  74. @classmethod
  75. def _reCon(cls):
  76. """ MySQLdb.OperationalError异常"""
  77. # self.con.close()
  78. while True:
  79. try:
  80. TidbProConn.get_connection().ping()
  81. break
  82. except OperationalError:
  83. TidbProConn.get_connection().ping(True)
  84. if __name__ == '__main__':
  85. db = TidbProConn.get_connection()
  86. cursor = db.cursor()
  87. # 执行sql语句
  88. sql = "show databases"
  89. cursor.execute(sql)
  90. result = cursor.fetchall()
  91. print("result====", result)