DB connection for MySQL database connection retry function and connection time-out feature Python implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.