几乎10个树莓派使用以下相同的脚本,并尝试每分钟(几乎同时)将数据发送到数据库。
[当只有一台设备与数据库通信时几乎没有问题,但是随着我开始添加更多的树莓派,问题开始了。
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:在树莓派上运行的树莓派,的Python:2.7,DB:MSSQL
谢谢
我应该在每次执行光标时调用提交/关闭吗?
是。 commit()
。事务期间将保留对插入的行和索引的锁定。 commit()
将释放该会话的所有锁。
如果在每行之后提交是性能问题,则可以打开delayed durability,也可以代替包含单行插入的嵌套循环,构建一个包含所有360行的JSON文档,将其发送到SQL Server并使用SQL服务器parse and INSERT在单个语句中。