我想在一个连接中拆分对SQL Server的读/写。我使用链接服务器作为指示,跟随此链接:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b543e671-6604-4c8f-b838-ed4255eea6a5/how-to-use-readwrite-spliting-in-ms-sql-server?forum=databasedesign
但是我不能插入数据库。我的代码卡在了执行命令中(我使用Python pyodbc库)
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=<Domain name listener>;'
'Database=<DB name>;'
'uid=<user login>;pwd=<password login>;'
'Trusted_Connection=no;'
'ApplicationIntent=ReadOnly;') --> use for read on replica node
sql_write_link_server = """
INSERT OPENQUERY ([<Domain name listener>], 'SELECT FullName, Age FROM TestingInsert.dbo.Persons') VALUES ('Hitsu', 29)
"""
cursor = conn.cursor()
cursor.execute(sql) --> this code stuck at here
conn.commit()
我的脚本导出错误如下:
pyodbc.ProgrammingError: ('42000', u'[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "<name link server>" was unable to begin a distributed transaction. (7391) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]OLE DB provider "SQLNCLI11" for linked server "name link server" returned message "No transaction is active.". (7412)')
请注意以下几点: