我们有一个C#WCF服务应用程序,它使用log4net将Web服务请求记录到表中。为了减轻来自客户数据请求的流量,我们创建了主数据库的镜像并将应用程序指向该数据库。但是,我们需要确保将日志条目写入主数据库,因此在镜像中,我们删除日志表,并用指向主数据库中“实际”表的SYNONYM替换。
奇怪的事情正在发生。
当指向包含真实表的主数据库时,INSERT语句可以正常工作,并且日志条目也可以发布。
但是,当指向包含指向实际表的同义词的镜像数据库时,似乎INSERT有效(无异常),但记录从未插入。
我们可以看到在SQL Profiler中正在执行的实际语句,在两种情况下都是相同的语句。但是,一个实际上插入了一条记录,而另一个没有。
有什么主意,为什么相同的代码在插入表和数据库同义词时会有不同的作用?
我们尝试过的事情:
我们这里没有主意。救命!
主数据库中表的架构:
CREATE TABLE [Osha].[Log](
[ID] [INT] IDENTITY(1,1) NOT NULL,
[Sequence] [BIGINT] NOT NULL,
[LastUpdated] [DATETIME] NOT NULL,
[LastUpdatedBy] [INT] NOT NULL,
[Process] [NVARCHAR](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Date] [DATETIME] NOT NULL,
[Thread] [NVARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Level] [NVARCHAR](25) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
[Logger] [NVARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[User] [NVARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[RequestID] [NVARCHAR](36) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Event] [NVARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Message] [NVARCHAR](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Property] [NVARCHAR](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Exception] [NVARCHAR](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [O_Log_pk_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
同义词SQL:
CREATE SYNONYM [Osha].[Log] FOR [Test].[Osha].[Log]
针对这两者执行SQL:
exec sp_executesql N'insert into [Osha].[Log] ([LastUpdatedBy], [Process], [Date], [Thread], [Level], [Logger], [User], [RequestID], [Event], [Message], [Property], [Exception]) values (1, ''d9a930b2-f29a-4764-89a0-1f3d7b6fb30a'', @Date, @Thread, @Level, @Logger, case when substring(@Message, 1, 5) = ''User:'' then substring(@Message, 7, charindex(char(13), @Message) - 7) else null end, case when substring(@Message, charindex(char(13), @Message) + 2, 10) = ''RequestID:'' then substring(@Message, charindex(char(13), @Message) + 13, 36) else null end, case when substring(@Message, charindex(char(13), @Message) + 51, 6) = ''Event:'' then substring(@Message, charindex(char(13), @Message) + 58, charindex(char(13), substring(@Message, charindex(char(13), @Message) + 59, 2147483647))) else null end, @Message, @Property, @Exception)',N'@Date datetime,@Thread nvarchar(255),@Level nvarchar(5),@Logger nvarchar(255),@Message nvarchar(max) ,@Property nvarchar(max) ,@Exception nvarchar(max) ',@Date='2018-09-20 16:32:44.663',@Thread=N'10',@Level=N'INFO',@Logger=N'blahblah',@Message=N'blahblach',@Property=N'{log4net:UserName=IIS APPPOOL\OshaTest, log4net:Identity=}',@Exception=N''
这是主数据库中的计划:
Clustered Index Insert(OBJECT:([Test].[Osha].[Log].[O_Log_pk_ID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_UserDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_UserLevelDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_RequestIDDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_DateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_LevelDateID]), SET:([Test].[Osha].[Log].[LastUpdatedBy] = [Expr1003],[Test].[Osha].[Log].[Process] = [Expr1004],[Test].[Osha].[Log].[Date] = RaiseIfNullInsert([@Date]),[Test].[Osha].[Log].[Thread] = [Expr1005],[Test].[Osha].[Log].[Level] = [Expr1006],[Test].[Osha].[Log].[Logger] = [Expr1007],[Test].[Osha].[Log].[User] = [Expr1008],[Test].[Osha].[Log].[RequestID] = [Expr1009],[Test].[Osha].[Log].[Event] = [Expr1010],[Test].[Osha].[Log].[Message] = [@Message],[Test].[Osha].[Log].[Property] = [@Property],[Test].[Osha].[Log].[Exception] = [@Exception],[Test].[Osha].[Log].[ID] = [Expr1002],[Test].[Osha].[Log].[Sequence] = RaiseIfNullInsert([Expr1011]),[Test].[Osha].[Log].[LastUpdated] = RaiseIfNullInsert([Expr1012]))) 0 0 Clustered Index Insert Insert OBJECT:([Test].[Osha].[Log].[O_Log_pk_ID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_UserDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_UserLevelDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_RequestIDDateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_DateID]), OBJECT:([Test].[Osha].[Log].[O_Log_ix_LevelDateID]), SET:([Test].[Osha].[Log].[LastUpdatedBy] = [Expr1003],[Test].[Osha].[Log].[Process] = [Expr1004],[Test].[Osha].[Log].[Date] = RaiseIfNullInsert([@Date]),[Test].[Osha].[Log].[Thread] = [Expr1005],[Test].[Osha].[Log].[Level] = [Expr1006],[Test].[Osha].[Log].[Logger] = [Expr1007],[Test].[Osha].[Log].[User] = [Expr1008],[Test].[Osha].[Log].[RequestID] = [Expr1009],[Test].[Osha].[Log].[Event] = [Expr1010],[Test].[Osha].[Log].[Message] = [@Message],[Test].[Osha].[Log].[Property] = [@Property],[Test].[Osha].[Log].[Exception] = [@Exception],[Test].[Osha].[Log].[ID] = [Expr1002],[Test].[Osha].[Log].[Sequence] = RaiseIfNullInsert([Expr1011]),[Test].[Osha].[Log].[LastUpdated] = RaiseIfNullInsert([Expr1012])) 1 0.06 6E-006 9 0.0600075 PLAN_ROW 0 1
|--Top(TOP EXPRESSION:((1))) 0 1 0 Top Top TOP EXPRESSION:((1)) 1 0 1E-007 1209 1.457E-006 [Expr1002], [Expr1003], [Expr1004], [Expr1005], [Expr1006], [Expr1007], [Expr1008], [Expr1009], [Expr1010], [Expr1011], [Expr1012] PLAN_ROW 0 1
|--Compute Scalar(DEFINE:([Expr1003]=(1), [Expr1004]=N'7524a82f-a8f7-47bc-ad2c-2674dd3a2fb8', [Expr1005]=CONVERT_IMPLICIT(nvarchar(250),[@Thread],0), [Expr1006]=CONVERT_IMPLICIT(nvarchar(25),[@Level],0), [Expr1007]=CONVERT_IMPLICIT(nvarchar(250),[@Logger],0), [Expr1008]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],(1),(5))=N'User:' THEN substring([@Message],(7),charindex(N'
',[@Message])-(7)) ELSE NULL END,0), [Expr1009]=CONVERT_IMPLICIT(nvarchar(36),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(2),(10))=N'RequestID:' THEN substring([@Message],charindex(N'
',[@Message])+(13),(36)) ELSE NULL END,0), [Expr1010]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(51),(6))=N'Event:' THEN substring([@Message],charindex(N'
',[@Message])+(58),charindex(N'
',substring([@Message],charindex(N'
',[@Message])+(59),(2147483647)))) ELSE NULL END,0), [Expr1011]=[Test].[Osha].[CurrentSequence](), [Expr1012]=getdate())) 0 2 1 Compute Scalar Compute Scalar DEFINE:([Expr1003]=(1), [Expr1004]=N'7524a82f-a8f7-47bc-ad2c-2674dd3a2fb8', [Expr1005]=CONVERT_IMPLICIT(nvarchar(250),[@Thread],0), [Expr1006]=CONVERT_IMPLICIT(nvarchar(25),[@Level],0), [Expr1007]=CONVERT_IMPLICIT(nvarchar(250),[@Logger],0), [Expr1008]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],(1),(5))=N'User:' THEN substring([@Message],(7),charindex(N'
',[@Message])-(7)) ELSE NULL END,0), [Expr1009]=CONVERT_IMPLICIT(nvarchar(36),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(2),(10))=N'RequestID:' THEN substring([@Message],charindex(N'
',[@Message])+(13),(36)) ELSE NULL END,0), [Expr1010]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(51),(6))=N'Event:' THEN substring([@Message],charindex(N'
',[@Message])+(58),charindex(N'
',substring([@Message],charindex(N'
',[@Message])+(59),(2147483647)))) ELSE NULL END,0), [Expr1011]=[Test].[Osha].[CurrentSequence](), [Expr1012]=getdate()) [Expr1003]=(1), [Expr1004]=N'7524a82f-a8f7-47bc-ad2c-2674dd3a2fb8', [Expr1005]=CONVERT_IMPLICIT(nvarchar(250),[@Thread],0), [Expr1006]=CONVERT_IMPLICIT(nvarchar(25),[@Level],0), [Expr1007]=CONVERT_IMPLICIT(nvarchar(250),[@Logger],0), [Expr1008]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],(1),(5))=N'User:' THEN substring([@Message],(7),charindex(N'
',[@Message])-(7)) ELSE NULL END,0), [Expr1009]=CONVERT_IMPLICIT(nvarchar(36),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(2),(10))=N'RequestID:' THEN substring([@Message],charindex(N'
',[@Message])+(13),(36)) ELSE NULL END,0), [Expr1010]=CONVERT_IMPLICIT(nvarchar(250),CASE WHEN substring([@Message],charindex(N'
',[@Message])+(51),(6))=N'Event:' THEN substring([@Message],charindex(N'
',[@Message])+(58),charindex(N'
',substring([@Message],charindex(N'
',[@Message])+(59),(2147483647)))) ELSE NULL END,0), [Expr1011]=[Test].[Osha].[CurrentSequence](), [Expr1012]=getdate() 1 0 1E-007 1209 1.357E-006 [Expr1002], [Expr1003], [Expr1004], [Expr1005], [Expr1006], [Expr1007], [Expr1008], [Expr1009], [Expr1010], [Expr1011], [Expr1012] PLAN_ROW 0 1
|--Compute Scalar(DEFINE:([Expr1002]=getidentity((1408776126),(5),NULL))) 0 3 2 Compute Scalar Compute Scalar DEFINE:([Expr1002]=getidentity((1408776126),(5),NULL)) [Expr1002]=getidentity((1408776126),(5),NULL) 1 0 1E-007 11 1.257E-006 [Expr1002] PLAN_ROW 0 1
|--Constant Scan
我什至在镜像数据库中都没有看到计划...
答案 0 :(得分:3)
我们遗漏了一个例外,当我在Profiler中打开错误查看功能时发现了这个例外。
The INSERT permission was denied on the object 'Log', database 'TestMirror', schema 'Osha'.
我想有些东西正在抓住它,而没有传递给我们。
感谢@ RBarryYoung,@ user2864740和@ user1443098的帮助。