Hi,

Our code is used in several places like: The web, A background service, a standalone application.

Now we would like to be able to use just 1 code base for that. On the web we use the SessionManager which creates a Session and a transaction for us and handles all failures and successes. So when we enter our code a Session and a Transaction are active.

When however we enter our code from for instance a background service this is not the case. No Session and Transaction are active so we have to do this ourselves.

I would like to automate this process and make it transparent for the developer. For that I need to know if a Sesseion and Transaction are active or not and act accordingly.

Is there a way to achieve that?

Regards Paul

asked Jul 10 '14 at 02:13

Paul%20Sinnema's gravatar image

Paul Sinnema
261888896


2 Answers:

I created the class below to solve this for the moment. With it I can do the following:

using(new SessionObject())
{
    // DO code goes here...
}

or

using(new SessionObject(true))
{
    // DO code with autocommit goes here...
}

which automatically saves and commits any changes

using System;
using Xtensive.Orm;

namespace Common.DataObjects.Library
{
    /// <summary>
    /// Class for maintaining sessions and transactions
    /// </summary>
    public class SessionObject : IDisposable
    {
        private TransactionScope m_TransactionScope;
        private Session m_Session;
        private bool m_AutoComplete;

        /// <summary>
        /// Creates a Session and Transaction if not already present
        /// </summary>
        public SessionObject(bool autoComplete = false)
        {
            if(Session.Current == null)
            {
                m_Session = DataContext.Instance().OpenSession();

                m_TransactionScope = m_Session.OpenTransaction();

                m_AutoComplete = autoComplete;
            }
        }

        /// <summary>
        /// Did I open the session myself?
        /// </summary>
        private bool OpenedSessionMySelf
        {
            get
            {
                return m_Session != null;
            }
        }

        /// <summary>
        /// Dispose the Session and Transaction
        /// </summary>
        public void Dispose()
        {
            DisposeTransaction();
        }

        /// <summary>
        /// Dispose the Session and Transaction when I opened it myself.
        /// </summary>
        private void DisposeTransaction()
        {
            if (OpenedSessionMySelf)
            {
                if (m_AutoComplete)
                {
                    m_Session.SaveChanges();
                    m_TransactionScope.Complete();
                }

                m_TransactionScope.Dispose();
                m_TransactionScope = null;
                m_Session.Dispose();
                m_Session = null;
            }
        }
    }
}

answered Jul 10 '14 at 03:59

Paul%20Sinnema's gravatar image

Paul Sinnema
261888896

edited Jul 10 '14 at 04:06

This coding tell us that how to detect transaction easily through learning some programming skills. But i can't understand why you use Xtensive.Orm session for it.

answered Jul 24 '14 at 01:12

stevens's gravatar image

stevens
3

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA