The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

Session.BeginTransaction() Method

Starts a transaction.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v19.1.dll


public virtual void BeginTransaction()
Public Overridable Sub BeginTransaction
public virtual void BeginTransaction()
Public Overridable Sub BeginTransaction


Calling the BeginTransaction method marks the starting point of a transaction - the point at which the data referenced by a session is consistent. All data modifications made after calling this method can be rolled back. This allows data to be returned to this known state of consistency.

Each transaction lasts until either the Session.CommitTransaction (Session.CommitTransactionAsync) or Session.RollbackTransaction method is called. The Session.CommitTransaction or Session.CommitTransactionAsync method commits the modifications. The Session.RollbackTransaction method discards all the changes made.

After the BeginTransaction method is called, the Session.BeforeBeginTransaction event is raised.


The following example demonstrates how to work with transactions.

In this example, the transaction begins when a persistent object is saved. The Session.BeginTransaction method marks the starting point of the transaction. If the value of the persistent object's Amount property is negative, the Session.RollbackTransaction method is called which completes the transaction discarding all the changes made since the transaction was started.

Otherwise, if the Amount property's value isn't negative, the Session.CommitTransaction method is called to complete the transaction and save all the data modifications made.

using System;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace ConsoleApplication1 {
    class Account : XPObject {
        public const double DefaultAmount = 100;
        private double amount = DefaultAmount;
        public Account(Session session) : base(session) { }
        public double Amount {
            get { return amount; }
            set { SetPropertyValue<double>("Amount", ref amount, value); }
        protected override void OnSaving() {
            if (!IsDeleted) {
                if (Amount < 0) {
                    throw new Exception("Negative amount!");

    class Program {
        static void Main(string[] args) {
            string connectionString = MSSqlConnectionProvider.GetConnectionString("(local)", "ConsoleApplication1");
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
            Console.WriteLine("Press <Enter> to exit");

        static void TransferAmount(double amount) {
            using (Session session = new Session()) {
                Console.WriteLine("Creating Account1 with amount = " + Account.DefaultAmount);
                Account account1 = new Account(session);
                Console.WriteLine("Creating Account2 with amount = " + Account.DefaultAmount);
                Account account2 = new Account(session);
                Console.WriteLine("Beginning the transaction...");
                try {
                    Console.WriteLine(String.Format(" Withdrawing {0} from Account1", amount));
                    account1.Amount -= amount;
                    Console.WriteLine(String.Format(" Transferring {0} to Account2", amount));
                    account2.Amount += amount;
                    Console.WriteLine("Committing the transaction...");
                catch (Exception e) {
                    Console.WriteLine("Exception message: " + e.Message);
                    Console.WriteLine("Rolling back the transaction");
                    Console.WriteLine("Reloading the accounts from database...");
                Console.WriteLine("Account1 amount: " + account1.Amount);
                Console.WriteLine("Account2 amount: " + account2.Amount);
See Also