mysql,python应用在travis测试超时
PHPz
PHPz 2017-04-17 11:09:14
0
0
295

我感觉问题出在数据库的连接部分。因为以前一直报错#2006 说mysql connection has gone away

后来我修改了下,连接的时候进行下ping,出现exception的时候新建一个连接

数据库连接部分代码:

class Database:

    configs = {  # default configs
        "host": "localhost",
        "port": 3306,  # int
        "db": "",
        "user": "",
        "passwd": "",
        "charset": "utf8"
    }

    conn = None

    debug = True

    query_times = 0

    SQL = None

    @classmethod
    def config(cls, debug=True, **configs):
        cls.configs.update(configs)
        cls.debug = debug

    @classmethod
    def new_conn(cls):
        cls.conn = MySQLdb.connect(
            cursorclass=MySQLdb.cursors.DictCursor,
            **cls.configs
        )

    @classmethod
    def connect(cls):
        # singleton
        if not cls.conn or not cls.conn.open:
            cls.new_conn()
        try:
            cls.conn.ping()  # ping to test if the connection is working
        except MySQLdb.OperationalError:
            cls.new_conn()

        return cls.conn
PHPz
PHPz

学习是最好的投资!

全部回覆(0)
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!