代码之家  ›  专栏  ›  技术社区  ›  Sean Woods

优雅地处理“MySQL已经消失”

  •  17
  • Sean Woods  · 技术社区  · 15 年前

    wait_timeout 超过了。我准备好了 等待超时 10 所以我可以试试这个。

    这是我的密码:

    def select(self, query, params=[]):
            try:
                self.cursor = self.cxn.cursor()
                self.cursor.execute(query, params)
            except MySQLdb.OperationalError, e:
                if e[0] == 2006:
                    print "We caught the exception properly!"
                    print self.cxn
                    self.cxn.close()
                    self.cxn = self.db._get_cxn()
                    self.cursor = self.cxn.cursor()
                    self.cursor.execute(query, params)
                    print self.cxn
    
            return self.cursor.fetchall()
    

    接下来,我等了十秒钟,试图提出一个请求。下面是CherryPy的样子:

    [31/Dec/2009:20:47:29] ENGINE Bus STARTING
    [31/Dec/2009:20:47:29] ENGINE Starting database pool...
    [31/Dec/2009:20:47:29] ENGINE POOL Connecting to MySQL...
    [31/Dec/2009:20:47:29] ENGINE POOL Connecting to MySQL...
    [31/Dec/2009:20:47:29] ENGINE POOL Connecting to MySQL...
    [31/Dec/2009:20:47:29] ENGINE POOL Connecting to MySQL...
    [31/Dec/2009:20:47:29] ENGINE POOL Connecting to MySQL...
    [31/Dec/2009:20:47:29] ENGINE Started monitor thread '_TimeoutMonitor'.
    [31/Dec/2009:20:47:29] ENGINE Started monitor thread 'Autoreloader'.
    [31/Dec/2009:20:47:30] ENGINE Serving on 0.0.0.0:8888
    [31/Dec/2009:20:47:30] ENGINE Bus STARTED
    We caught the exception properly!   <====================================== Aaarg!
    <_mysql.connection open to 'localhost' at 1ee22b0>
    [31/Dec/2009:20:48:25] HTTP Traceback (most recent call last):
      File "/usr/local/lib/python2.6/dist-packages/CherryPy-3.1.2-py2.6.egg/cherrypy/_cprequest.py", line 606, in respond
    cherrypy.response.body = self.handler()
      File "/usr/local/lib/python2.6/dist-packages/CherryPy-3.1.2-py2.6.egg/cherrypy/_cpdispatch.py", line 25, in __call__
        return self.callable(*self.args, **self.kwargs)
      File "adp.py", line 69, in reports
        page.sources = sql.GetSources()
      File "/home/swoods/dev/adp/sql.py", line 45, in __call__
        return getattr(self.formatter.cxn, parsefn)(sql, sql_vars)
      File "/home/swoods/dev/adp/database.py", line 96, in select
        self.cursor.execute(query, params)
      File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
        self.errorhandler(self, exc, value)
      File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
        raise errorclass, errorvalue
    OperationalError: (2006, 'MySQL server has gone away')
    
    [31/Dec/2009:20:48:25] HTTP 
    Request Headers:
      COOKIE: session_id=e14f63acc306b26f14d966e606612642af2dd423
      HOST: localhost:8888
      CACHE-CONTROL: max-age=0
      ACCEPT: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
      ACCEPT-CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.3
      USER-AGENT: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.5 (KHTML, like     Gecko) Chrome/4.0.249.43 Safari/532.5
      CONNECTION: keep-alive
      Remote-Addr: 127.0.0.1
      ACCEPT-LANGUAGE: en-US,en;q=0.8
      ACCEPT-ENCODING: gzip,deflate
    127.0.0.1 - - [31/Dec/2009:20:48:25] "GET /reports/1 HTTP/1.1" 500 1770 "" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.43 Safari/532.5"
    

    为什么这不管用??很明显,我捕捉到了异常,重新生成了连接和光标,但它仍然不起作用。它是否与MySQLdb获取连接的方式有关?

    1 回复  |  直到 10 年前
        1
  •  12
  •   bobince    15 年前

    无法从代码中看到,但我猜 db._get_cxn() 方法执行某种连接池并返回现有连接对象,而不是创建新的连接对象。没有你可以打的电话吗 db 刷新现有的无用连接(你真的应该打内部电话吗 _ -前缀方法?)

    预防 MySQL has gone away try...except OperationalError...try again .