TidbProConn.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, conn, sql):
  42. TidbProConn._reCon(conn)
  43. cur = conn.cursor()
  44. cur.execute(sql)
  45. result = cur.fetchall()
  46. return result
  47. @staticmethod
  48. def conn_close(conn: pymysql.connections.Connection):
  49. """
  50. 关闭资源
  51. """
  52. if conn:
  53. try:
  54. conn.close()
  55. except pymysql.err.Error:
  56. pass
  57. @staticmethod
  58. def exec_sql(conn, sql_buff):
  59. sql_list = sql_buff.split(";")
  60. cur = conn.cursor()
  61. for sql in sql_list:
  62. if sql.strip():
  63. cur.execute(sql)
  64. return cur
  65. @staticmethod
  66. def upsert(conn, sql_buff):
  67. TidbProConn._reCon(conn)
  68. try:
  69. TidbProConn.exec_sql(conn, sql_buff)
  70. except Exception as e:
  71. logging.error('失败详细信息:', repr(e))
  72. TidbProConn.get_connection().commit()
  73. @staticmethod
  74. def _reCon(conn):
  75. """ MySQLdb.OperationalError异常"""
  76. # self.con.close()
  77. while True:
  78. try:
  79. conn.ping()
  80. break
  81. except OperationalError:
  82. conn.ping(True)
  83. if __name__ == '__main__':
  84. db = TidbProConn.get_connection()
  85. cursor = db.cursor()
  86. # 执行sql语句
  87. sql = "show databases"
  88. cursor.execute(sql)
  89. result = cursor.fetchall()
  90. print("result====", result)