Python mysql中的同一行(nginx+;tornado+;mysqldb)
我使用tornado作为我的web应用程序。mysqldb用于将数据插入mysql5.1。 在生产环境中,有一个nginx+上游10 tornado流程。 当网络速度较慢时,用户会双击按钮并将相同的json数据发布到tornado处理程序,有时mysql中会生成两行相同的数据。实际上,我已经使用了mysqldb转换并测试了逻辑。在开发环境中(一个tornado过程)是可以的 我的代码:Python mysql中的同一行(nginx+;tornado+;mysqldb),python,mysql,tornado,Python,Mysql,Tornado,我使用tornado作为我的web应用程序。mysqldb用于将数据插入mysql5.1。 在生产环境中,有一个nginx+上游10 tornado流程。 当网络速度较慢时,用户会双击按钮并将相同的json数据发布到tornado处理程序,有时mysql中会生成两行相同的数据。实际上,我已经使用了mysqldb转换并测试了逻辑。在开发环境中(一个tornado过程)是可以的 我的代码: import MySQLdb hostname = options.mysql_host
import MySQLdb
hostname = options.mysql_host
uid = options.mysql_user
database = options.mysql_database
pwd = options.mysql_password
port = 3306
newid=-1
conn = MySQLdb.connect(host=hostname, user=uid,db=database,passwd=pwd,port=int(port),use_unicode=True,charset="utf8")
cursor = conn.cursor()
try:
sql_query = "select ID from ATable where USER_ID = "+str(_user_id)+" and START_DATE_LOCAL = '"+str(_start_date_local)+"' and FLAG = 1"
cursor.execute(sql_query)
results_query = cursor.fetchone()
if results_query is not None:
newid =int(results_query[0])
else:
#do insert
sql="insert into ATable..."
print sql
cursor.execute(sql)
newid = int(conn.insert_id())
conn.commit()
except Exception,e:
print 'ERROR:',e
conn.rollback()
conn.close()
return newid
我认为我的代码是正确的。
nginx或mysql5.1可能有问题
我是否应该在nginx upstrem中为此处理程序配置ip_哈希?您有一个竞争条件。在生产环境中更容易看到,因为您是通过延迟更高的网络发布的,所以在第一次插入完成之前,您有更多的时间单击按钮两次。典型的解决方案是在按钮发布到服务器之前使用Javascript使其自身禁用,以防止双击。有关在等待AJAX请求完成时禁用按钮的示例,请参阅