当从不同设备完成许多连接时,pyodbc cursor.execute 冻结

pyodbc cursor.execute freezes when many connection are done from different devices

几乎 10 raspberry pi 使用与下面提到的相同的脚本并尝试每分钟(几乎同时)将数据发送到数据库。

当只有一台设备与数据库通信时几乎没有问题,但当我开始添加更多设备时问题就开始了 raspberry pi。


Code:

import pyodbc 

def update_database():
        try:            
            mydb = pyodbc.connect('DRIVER=FreeTDS;SERVER=192.xxx.xxx.xxx;PORT=xxxx;DATABASE=xxxxxxx;UID=xxxxxxx;PWD=xxxxxxx;TDS_Version=4.2;')
            mycursor = mydb.cursor()

            if mycursor.tables(table='DBDBDBDB', tableType='TABLE').fetchone():
                print("DB exists")
            else:
                print("DB absent")
                DB_Connection_status=0


            ycursor = mydb.cursor()            
            sql = "INSERT INTO DBDBDBDB (docdt, timestamp, cocd, param1, param2, param3, param4, param5, param6, param7, param8) \
                    VALUES (?,?,?,?,?,?,?,?,?,?,?)"


            DB_Connection_status=1
            systemlog("DB Connected")
        except:
            DB_Connection_status=0
            print ("Connection with database: failed")
            systemlog("DB Not Connected")



        for index_y in range (0,6):
            #few lines of code here

            for index_x in range (0,60): 
                #few lines of code here

                if(DB_Connection_status==1):

                    val = (formatted_date, formatted_time, COCD, str(var1), \
                           str(var2), str(var3),\
                           'A','1', str( var4[index_y][index_x]), \
                           str(var5[index_y][toggle_sw_num-1]),0)
                    try:
                        mycursor.execute(sql, val)
                    except:
                        systemlog("DB record skipped")


        if(DB_Connection_status==1):
            try:
                mydb.commit()
                print(mycursor.rowcount, "record inserted.")
                systemlog("Record inserted")
            except:
                systemlog("DB commit skipped")

while 1:
    #few lines of code here
    if(system_minute!=oldsystem_minute):
        oldsystem_minute=system_minute
        #few lines of code here
        update_database()


最初在我为游标执行和提交添加错误处理之前抛出如下所示的错误

Traceback (most recent call last):
  File "mod.py", line 461, in <module>
  File "mod.py", line 213, in update_database
pyodbc.ProgrammingError: ('42000', '[42000] [FreeTDS][SQL Server]Transaction (Process ID 52) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. (1205) (SQLExecDirectW)')

但是这个错误处理只是为了避免代码崩溃。

代码顺序有问题吗?例如,每次执行游标时我都应该调用 commit/close 吗? 有什么帮助吗?

OS: raspbian 运行 raspberry pi, Python: 2.7, 数据库:MSSQL

谢谢

should I call commit/close every time on cursor execute?

是的。 commit()。插入的行和索引上的锁在事务期间保持。 commit() 将释放该会话的所有锁。

如果在每行之后提交是一个性能问题,您可以在单个语句中打开delayed durability, or, instead of a nested loop with single-row inserts, build a JSON document with all 360 rows, send that to SQL Server and have SQL Server parse and INSERT它。