Udostępnij za pośrednictwem


Managing Transactions in Object Services (Entity Framework)

Object Services supports automatic transaction enlistment. This means that activities performed within an object context, such as executing queries and saving changes to data in the data source, can be isolated in the data source by executing the operation within a System.Transactions transaction. Transactions are used with Object Services to do the following:

  • To execute multiple operations against the data source that must be highly consistent, such as queries that depend on the successful completion of object changes.

  • To coordinate changes in the object context with other distributed operations, such as sending an e-mail notification or writing to a message queue.

Transactions that require the enlistment of additional resource managers are called distributed transactions. Distributed transactions use a distributed transaction coordinator (DTC) to manage the resources required to complete the transaction. Promotion of a transaction to a DTC can be a relatively expensive process to establish and complete. Some resource managers, like SQL Server 2005, support the Promotable Single Phase Enlistment (PSPE) transaction protocol. This allows a resource manager to host a transaction that can later be escalated to be managed by the distributed transaction coordinator (DTC) if necessary.

For more information about System.Transactions, see Transaction Processing. For more information about using System.Transactions with SQL Server, see System.Transactions Integration with SQL Server (ADO.NET).

The following considerations apply when you use transactions with Object Services:

  • Only operations against the data source are transacted. Changes made to objects in the object context are not transacted. Changes to objects in the context are visible outside the transaction scope.

  • When you call SaveChanges, if a current transaction exists Object Services uses this transaction for operations against the data source. Otherwise, it creates a new transaction for the operation. You can define transactions by using EntityTransaction, Transaction, or TransactionScope.

    Note

    To enlist in an existing transaction, Object Services might close and reopen the connection.

  • When Object Services creates a new transaction for a SaveChanges operation, changes to objects in the object context are not accepted until the transaction completes. This ensures that the state of the object context and the data source are consistent.

  • Promotion of a transaction to a DTC may occur when a connection is closed and reopened within a single transaction. Because Object Services opens and closes the connection automatically, you should consider manually opening and closing the connection to avoid transaction promotion. For more information, see How to: Manually Open the Connection from the Object Context (Entity Framework).

  • When you plan to retry operations in a transaction, you must ensure that the status of objects in the context is not reset before the transaction is completed. To do this, you must call SaveChanges with a value of false for the acceptChangesDuringSave parameter, and then call AcceptAllChanges only after other operations in the transaction have completed successfully. For more information, see How to: Manage Object Services Transactions (Entity Framework).

  • As part of a retry operation with coordinated transactions, you might call SaveChanges a second time without first calling AcceptAllChanges. In this case, Object Services will attempt to reapply the same changes to the data source.

See Also

Concepts

EntityClient Provider for the Entity Framework
Managing Connections in Object Services (Entity Framework)

Other Resources

Managing the Object Context (Entity Framework)