Issues with flask-based Web application deployment to SAE

Source: Internet
Author: User
Tags db2

My application of the underlying database is MySQL, using Flask-sqlalchemy to implement interface operations. The problem I encountered was:

After I deployed the code on the SAE, the "2006,mysql has gone away" issue always appears when the data is being insert into the database.

Check the official documents and Google a lot, the first is to do the following methods:

1, sqlalchemy_pool_size = 10

2. Close the database after each operation

def init_after_handlers (APP):    @app. Teardown_appcontext    def teardown_request (exception= None):        if'db'):            g.db.close ()        #  DB2 = GetAttr (g, ' DB2 ', none)        if is not None:            Db.session.remove ()

But the above steps do not work for me, really should not AH!!!!!

Later looked again, said can use Ping, so I now abandoned the use of sqlalchemy operation of this part of the data, and instead of directly using MYSQLDB. The way to use ping is to keep making new connections.

def _connect (self,dbname):        tar = self.config[dbname]        Self.con = MySQLdb.connect (host=tar[' host '),                                   user= tar[' user '],                                   port=tar[' Port '],                                   passwd=tar[' passwd '],                                   charset=tar[' CharSet '],                                   db=tar[' db ')        self.con.ping (True)        self.cursor = Self.con.cursor (cursorclass=mysqldb.cursors.dictcursor)

At the time of insertion:

def insert_by_dic (self,table,data):        keys = Data.keys ()        values = []        keystr = ', '. Join (' + x + ' ' For x in Ke YS) for        key in keys:            values.append (Data[key])        valstr = ', '. Join ("'" + x + "'" If Isinstance (x,unicode)                            El Se "'" + str (x). Decode (' UTF8 ') + "'" for x in values)           sql = "INSERT into  %s (%s) values (%s)"% (table,keystr,val STR)        self.cursor.execute (SQL)        Self.con.commit ()

def insert_list_dic (self,data,table):        for item in data:            try:                self.insert_by_dic (table, item)            except Mysqldb.operationalerror:                self._connect (dbname)                self.insert_by_dic (table, item)                continue            except Mysqldb.integrityerror,e:                print str (e)                continue

  

After the code is done, 2006 of the problem has been completely resolved.

In fact, now there is a problem, that is, my previous pagination is implemented in the background, that is, in the model using Pagnite implementation, but after the use of MYSQLDB, I have to learn to page the front end of the data.

To be continued ......... ..........

Issues with flask-based Web application deployment to SAE

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.