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