事务和并发性Transactions and Concurrency

事务由作为包执行的单个命令或一组命令组成。A transaction consists of a single command or a group of commands that execute as a package. 通过事务可以将多个操合并为单个工作单元。Transactions allow you to combine multiple operations into a single unit of work. 如果在事务中的某一点发生故障,则所有更新都可以回滚到其事务前状态。If a failure occurs at one point in the transaction, all of the updates can be rolled back to their pre-transaction state.

事务必须符合 ACID 属性(原子性、一致性、隔离和持久性)才能保证数据的一致性。A transaction must conform to the ACID properties—atomicity, consistency, isolation, and durability—in order to guarantee data consistency. 大多数关系数据库系统(例如 Microsoft SQL Server)都可在客户端应用程序执行更新、插入或删除操作时为事务提供锁定、日志记录和事务管理功能,以此来支持事务。Most relational database systems, such as Microsoft SQL Server, support transactions by providing locking, logging, and transaction management facilities whenever a client application performs an update, insert, or delete operation.

备注

如果锁定持续时间过长,则涉及多个资源的事务可能会降低并发性。Transactions that involve multiple resources can lower concurrency if locks are held too long. 因此,事务应尽量保持简短。Therefore, keep transactions as short as possible.

如果一个事务涉及同一个数据库或服务器中的多个表,则存储过程中的显式事务通常可以更好地执行。If a transaction involves multiple tables in the same database or server, then explicit transactions in stored procedures often perform better. 您可以通过使用 Transact-SQL BEGIN TRANSACTIONCOMMIT TRANSACTIONROLLBACK TRANSACTION 语句在 SQL Server 存储过程中创建事务。You can create transactions in SQL Server stored procedures by using the Transact-SQL BEGIN TRANSACTION, COMMIT TRANSACTION, and ROLLBACK TRANSACTION statements. 有关更多信息,请参见 SQL Server 联机丛书。For more information, see SQL Server Books Online.

涉及不同资源管理器的事务(例如 SQL Server 和 Oracle 之间的事务)需要分布式事务。Transactions involving different resource managers, such as a transaction between SQL Server and Oracle, require a distributed transaction.

本节内容In This Section

本地事务Local Transactions
演示如何对数据库执行事务。Demonstrates how to perform transactions against a database.

分布式事务Distributed Transactions
描述如何在 ADO.NET 中执行分布式事务。Describes how to perform distributed transactions in ADO.NET.

System.Transactions 与 SQL Server 的集成System.Transactions Integration with SQL Server
描述System.Transactions与使用分布式事务的 SQL Server 的集成。Describes System.Transactions integration with SQL Server for working with distributed transactions.

开放式并发Optimistic Concurrency
描述开放式并发和保守式并发,以及如何测试并发冲突。Describes optimistic and pessimistic concurrency, and how you can test for concurrency violations.

请参阅See also