defreconndb( Self):
# DB connection for database connection retry feature and connection timeout feature
_conn_status =True
_max_retries_count =Ten# set Maximum retry count
_conn_retries_count =0# Initial Retry attempts
_conn_timeout =3# Connection time-out is 3 seconds
while_conn_status and_conn_retries_count <= _max_retries_count:
Try:
Print' Connect to database: '
conn = Pymysql.connect (Host=db_host, Port=3306, User=db_user, passwd=db_password, DB=db_database, CharSet=db_charset, Connect_timeout=_conn_timeout)
_conn_status =False# _status is set to FALSE if Conn is successful exit loop, return DB connection object
returnConn
except:
_conn_retries_count + =1
Print_conn_retries_count
Print' Connect db is error!! '
Time.sleep (3)# This is the test to see the effect
Continue
DB connection for MySQL database connection retry function and connection time-out feature Python implementation