I am using pymysql to connect to sql like this:
con=pymysql.connect(...)
cur=con.cursor()
Well I am using flask web framework for some use. I defined the connection object as a single time and pass it to other function for use like this:
@app.route('/')
def index():
cur.execute(...)
con.commit()
#other stuffs
My questions is that whether it is a good way or this:
@app.route('/')
def index():
con=pymysql.connect(...)
cur=con.cursor()
cur.execute(...)
con.commit()
#other stuffs
What should I choose using a single connection or multiple one for every function. I don't want to make it crash if two requests are made at the same time, stop data from corruption and make it synchronised too. What's actually a good way?
Thanks!
You only need one connection. You can create multiple cursors from that connection, and the cursors will keep different requests separated.