- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在sql server中遇到了时间戳数据类型。sql server中时间戳列的实际用途是什么?示例是什么?
最佳答案
我使用了 TIMESTAMP
数据类型(ROWVERSION
,SQL2005+)来避免 lost update problem :
The lost update problem: A second transaction writes a second value of a data-item (datum) on top of a first value written by a first concurrent transaction, and the first value is lost to other transactions running concurrently which need, by their precedence, to read the first value. The transactions that have read the wrong value end with incorrect results.
示例:丢失更新
:
t : User 1 read payment order (PO) #1 (amount 1000)
t+1: User 2 read payment order (PO) #1 (amount 1000)
t+2: User 1 change the amount for PO #1 to 1005
t+3: User 2 change the amount for PO #1 to 1009 (change make by User 1 is lost because is overwritten by change make by User 2)
t+4: The amount is **1009**.
示例:如何防止丢失更新
:
t : User 1 read payment order (PO) #1 (amount 1000, timestamp 0x00000000000007D1)
t+1: User 2 read payment order (PO) #1 (amount 1000, timestamp 0x00000000000007D1)
t+2: User 1 change the amount for PO #1 to 1005 and it checks if row has the same `timestamp` (column `RW` in this case; 0x00000000000007D1). The check succeeds and the change is `COMMIT`ed. This will change, also, the timestamp (column 'RW'). The new timestamp is 0x00000000000007D4.
t+3: User 2 change the amount for PO #1 to 1009 and it checks if row has the same `timestamp` (column `RW` in this case; 0x00000000000007D4). The checks fails because the initial timestamp (@rw=0x00000000000007D1) is <> than current timestamp (column `RW`=0x00000000000007D4). An error is raised the catch block "intercepts" the error and this transaction is cancelled (`ROLLBACK`).
t+4: The amount {remains|is} **1005**.
示例:如何防止更新丢失
的 T-SQL 脚本(警告:您必须使用两个 SSMS 窗口/两个 session )
CREATE DATABASE TestRowVersion;
GO
USE TestRowVersion;
GO
CREATE TABLE dbo.PaymentOrder(
PaymentOrderID INT IDENTITY(1,1) PRIMARY KEY,
PaymentOrderDate DATE NOT NULL,
Amount NUMERIC(18,2) NOT NULL,
CreateDate DATETIME NOT NULL DEFAULT (GETDATE()),
UpdateDate DATETIME NULL,
RW ROWVERSION NOT NULL -- R[ow] V[ersion]
);
GO
INSERT dbo.PaymentOrder (PaymentOrderDate,Amount)
VALUES ('2013-07-21',1000);
INSERT dbo.PaymentOrder (PaymentOrderDate,Amount)
VALUES ('2013-07-22',2000);
INSERT dbo.PaymentOrder (PaymentOrderDate,Amount)
VALUES ('2013-07-23',3000);
GO
SELECT * FROM dbo.PaymentOrder;
/*
PaymentOrderID PaymentOrderDate Amount CreateDate UpdateDate RW
-------------- ---------------- ------- ----------------------- ---------- ------------------
1 2013-07-21 1000.00 2013-07-21 09:35:38.750 NULL 0x00000000000007D1
2 2013-07-22 2000.00 2013-07-21 09:35:38.750 NULL 0x00000000000007D2
3 2013-07-23 3000.00 2013-07-21 09:35:38.750 NULL 0x00000000000007D3
*/
GO
-- User 1 (SQL Server Management Studio/SSMS window #1)
-- [t] Client app, user 1: it loads first PO
SET NOCOUNT ON;
GO
DECLARE @PaymentOrderID INT=1; -- parameter
SELECT po.PaymentOrderID,
po.PaymentOrderDate,
po.Amount,
po.RW
FROM dbo.PaymentOrder po
WHERE po.PaymentOrderID=@PaymentOrderID;
-- Client app, user 1: during 15 seconds it edit the amount from 1000.00 to 1005.00
WAITFOR DELAY '00:00:15';
GO
-- [t+2] Client app, user 1: it sends this change (new amount) from client app to database server
-- with the old row version value
DECLARE @PaymentOrderID INT=1; -- parameter
DECLARE @rw BINARY(8)=0x00000000000007D1; -- parameter
DECLARE @NewAmount NUMERIC(18,2)=1005.00; -- parameter
BEGIN TRY
BEGIN TRANSACTION
UPDATE dbo.PaymentOrder
SET Amount=@NewAmount
WHERE PaymentOrderID=@PaymentOrderID
AND RW=@rw; -- it checks the timestamp (current timestamp versus original timestamp)
DECLARE @rowcount INT=@@ROWCOUNT; -- How many rows were affected by the last statement (UPDATE in this case) ?
SELECT @rowcount AS [@@ROWCOUNT];
IF @rowcount<>1
RAISERROR('Lost update or row deleted.', 16, 1);
COMMIT TRANSACTION
PRINT 'UPDATE succeded';
END TRY
BEGIN CATCH
IF @@TRANCOUNT>0
ROLLBACK;
DECLARE @ErrMsg NVARCHAR(2002);
SET @ErrMsg=ERROR_MESSAGE();
RAISERROR(@ErrMsg,16,1);
END CATCH;
GO
-- [t+4] Client app, user 1: it reloads first PO
DECLARE @PaymentOrderID INT=1; -- parameter
SELECT po.PaymentOrderID,
po.PaymentOrderDate,
po.Amount,
po.RW
FROM dbo.PaymentOrder po
WHERE po.PaymentOrderID=@PaymentOrderID;
GO
-- User 2 (warning: run this script in another SQL Server Management Studio window: File > New Database Engine Query !; SSMS window #2)
-- [t+1] Client app, user 1: it loads first PO
SET NOCOUNT ON;
GO
DECLARE @PaymentOrderID INT=1; -- parameter
SELECT po.PaymentOrderID,
po.PaymentOrderDate,
po.Amount,
po.RW
FROM dbo.PaymentOrder po
WHERE po.PaymentOrderID=@PaymentOrderID;
-- Client app, user 1: during 20 seconds it edit the amount from 1000.00 to 1005.00
WAITFOR DELAY '00:00:20';
GO
-- [t+4] Client app, user 1: it sends this change (new amout) from client app to database server
-- with the old row version value
DECLARE @PaymentOrderID INT=1; -- parameter
DECLARE @rw BINARY(8)=0x00000000000007D1; -- parameter
DECLARE @NewAmount NUMERIC(18,2)=1009.00; -- parameter
BEGIN TRY
BEGIN TRANSACTION
UPDATE dbo.PaymentOrder
SET Amount=@NewAmount
WHERE PaymentOrderID=@PaymentOrderID
AND RW=@rw; -- it checks the timestamp (current timestamp versus original timestamp)
DECLARE @rowcount INT=@@ROWCOUNT; -- How many rows were affected by the last statement (UPDATE in this case) ?
SELECT @rowcount AS [@@ROWCOUNT];
IF @rowcount<>1
RAISERROR('Lost update or row deleted.', 16, 1);
COMMIT TRANSACTION
PRINT 'UPDATE succeded';
END TRY
BEGIN CATCH
IF @@TRANCOUNT>0
ROLLBACK;
DECLARE @ErrMsg NVARCHAR(2002);
SET @ErrMsg=ERROR_MESSAGE();
RAISERROR(@ErrMsg,16,1);
END CATCH;
GO
-- [t+4] Client app, user 1: it reloads first PO
DECLARE @PaymentOrderID INT=1; -- parameter
SELECT po.PaymentOrderID,
po.PaymentOrderDate,
po.Amount,
po.RW
FROM dbo.PaymentOrder po
WHERE po.PaymentOrderID=@PaymentOrderID;
GO
用户 1 的结果(金额 1000 -> 1005):
PaymentOrderID PaymentOrderDate Amount RW
-------------- ---------------- --------------------------------------- ------------------
1 2013-07-21 1000.00 0x00000000000007D1
@@ROWCOUNT <- Timestamp check succeeds
-----------
1
UPDATE succeded
PaymentOrderID PaymentOrderDate Amount RW
-------------- ---------------- --------------------------------------- ------------------
1 2013-07-21 1005.00 0x00000000000007D4
用户 2 的结果(金额 1000 -> 1009):
PaymentOrderID PaymentOrderDate Amount RW
-------------- ---------------- --------------------------------------- ------------------
1 2013-07-21 1000.00 0x00000000000007D1
@@ROWCOUNT <- Timestamp check fails
-----------
0
Msg 50000, Level 16, State 1, Line 27
Lost update.
PaymentOrderID PaymentOrderDate Amount RW
-------------- ---------------- --------------------------------------- ------------------
1 2013-07-21 1005.00 0x00000000000007D4
注意:将错误消息更改为 RAISERROR('丢失更新或行已删除。', 16, 1);
关于sql-server - sql server中时间戳列的实际用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17768608/
XML可以应用于 WEB 开发的许多方面,常用于简化数据的存储和共享 XML 把数据从 HTML 分离 现在的HTML 文档一般都是显示动态数据,每当数据改变时将花费大量的时间来编辑 HTML
在类(class)(方案)中,我们最近学习了允许函数接受任意数量参数的语法,例如; (define len (lambda args (length args))) >(len 1 2 3
在 redis-cli 中执行 info 命令时,会列出与该服务器相关的所有信息。 “evicted_keys”的目的是什么? 最佳答案 Redis 可以配置为根据需要自动清除键。如果这样配置,red
golang 空结构体 struct{} 可以用来节省内存 ? 1
有人可以解释一下资源文件夹中的 Info.plist 是什么吗?我如何在我的应用程序中使用它?提前致谢。 最佳答案 您可以在Info.plist文件中为所有类型的设备指定不同的常规资源和应用程序配置,
我需要减小可执行文件的大小,并且经常在我的可执行文件中将某些“不必要的” uses占用一定的大小。 例如: Unit1连接到Unit2,但是两个单元的导入为“ System.SysUtils”。 然后
我是 OOP 的新手。虽然我了解多态是什么,但我无法真正使用它。我可以有不同名称的函数。为什么我应该尝试在我的应用程序中实现多态性。 最佳答案 在严格类型化的语言中,多态性对于拥有不同类型对象的列表/
Possible Duplicate: What is the difference between a delegate and events? Possible Duplicate: Differ
这个问题在这里已经有了答案: Custom index.html javadoc page? (2 个答案) 关闭 7 年前。 我使用的是eclipse。一般情况下我们可以使用javadoc.exe
你能解释一下吗ValidateAntiForgeryToken目的并向我展示有关 MVC 4 中的 ValidateAntiForgeryToken 的示例? 我找不到任何解释此属性的示例? 最佳答案
我正在学习 RxSwift 并试图了解 Observable 和 PublishSubject aka Sequence 之间的主要区别。 据我了解,Sequences/PublishesSubjec
有时有人说,在使用 Azure 表时,实际上存在第三个关键分区数据 - 表名称本身。 我在执行分段查询时注意到 TableContinuationToken 有一个 NextTableName 属性。
在Apple提供的aurioTouch项目中,听说在performRender()函数下的AudioController文件中的AudioBufferList ioData携带了mic到音频播放器的音
MSDN 文档没有详细解释它的用途。 由于它是可选的,我几乎假设它类似于某种元数据。 我现在如何看待它,我想用它来为特定用户指定缓存,例如: new CacheItem(key: "keyName",
我正在学习 browserify,我正在尝试用它做两件基本的事情: 转换(通过 shim)非 CommonJS 模块以实现易用性和依赖跟踪 捆绑项目特定的库 我找到了一个工作流程,说明如何使用 Gul
我通过按 CTRL + C 访问 eix 1.8.2 的 Break 菜单。它看起来像这样: BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
在 codeLabs 教程(Android - Kotlin - Room with a View)中,他们使用“viewModelScope.launch(Dispatchers.IO)”来调用插入
我通过按 CTRL + C 访问 eix 1.8.2 的 Break 菜单。它看起来像这样: BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
请耐心等待,因为我没有太多地使用压缩算法,所以这对你们中的一些人来说可能是显而易见的。当某些流媒体视频开始滞后时,我总是注意到这一点。当我看到这个问题时我才意识到我很好奇: Twitter image
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 3 年前。 Improv
我是一名优秀的程序员,十分优秀!