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

我感觉问题出在数据库的连接部分。因为以前一直报错#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

学习是最好的投资!

membalas semua(0)
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!