示例#1
0
        public HyperstoreTransactionScope(Session session, SessionIsolationLevel sessionIsolationLevel, TimeSpan timeSpan)
        {
            this._sessionIsolationLevel = sessionIsolationLevel;
            this._timeout = timeSpan;
            this._session = session;

            if (TimeSpan.Zero != timeSpan)
            {
                _timer = Hyperstore.Modeling.Utils.Timer.Create(OnTimeOut, timeSpan);
            }
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Constructor.
        /// </summary>
        /// <param name="isolationLevel">
        ///  The isolation level.
        /// </param>
        /// <param name="timeout">
        ///  The timeout.
        /// </param>
        ///-------------------------------------------------------------------------------------------------
        public TransactionScopeWrapper(SessionIsolationLevel isolationLevel, TimeSpan timeout)
        {
            var level   = isolationLevel == SessionIsolationLevel.ReadCommitted ? IsolationLevel.ReadCommitted : IsolationLevel.Serializable;
            var options = new TransactionOptions
            {
                IsolationLevel = Transaction.Current == null ? level : Transaction.Current.IsolationLevel,
                Timeout        = timeout
            };

            _scope = new TransactionScope(TransactionScopeOption.Required, options); //, TransactionScopeAsyncFlowOption.Enabled);
        }
        internal MemoryTransaction(IHyperstoreTrace trace, ITransactionManager transactionManager, long id, SessionIsolationLevel isolationLevel)
        {
            DebugContract.Requires(transactionManager);
            DebugContract.Requires(trace);

            if (isolationLevel != SessionIsolationLevel.ReadCommitted && isolationLevel != SessionIsolationLevel.Serializable)
            {
                throw new ArgumentOutOfRangeException("Only ReadCommitted or Serializable is allowed.");
            }

            Id = id;
            _isolationLevel     = isolationLevel;
            _transactionManager = transactionManager;
            _nestedStatus       = new Stack <TransactionStatus>();
            PushNestedTransaction();
            _currentStatus = TransactionStatus.Active;
        }
示例#4
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Création d'une transaction (Scope = RequiredNested)
        /// </summary>
        /// <param name="isolationLevel">
        ///  .
        /// </param>
        /// <param name="readOnly">
        ///  true to read only.
        /// </param>
        /// <returns>
        ///  The new transaction.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        private MemoryTransaction CreateTransaction(SessionIsolationLevel isolationLevel, bool readOnly)
        {
            var current = CurrentTransaction;

            if (current == null)
            {
                var xid = Interlocked.Increment(ref _transactionNumber);
                if (_trace.IsEnabled(TraceCategory.MemoryStore))
                {
                    _trace.WriteTrace(TraceCategory.MemoryStore, "Create memory transaction {0} - Thread {1}", xid, ThreadHelper.CurrentThreadId);
                }

                current = new MemoryTransaction(_trace, this, xid, isolationLevel);
                if (Session.Current != null)
                {
                    CurrentTransaction = current;
                }

                if (!readOnly)
                {
                    lock (_sync)
                    {
                        _transactions.Add(current.Id, current);
                        lock (_activeTransactions)
                        {
                            _activeTransactions.Add(current);
                        }

                        if (Session.Current != null)
                        {
                            Session.Current.Enlist(current);
                        }
                    }
                }
            }
            else if (!readOnly)
            {
                current.PushNestedTransaction();
            }

            return(current);
        }
示例#5
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Start a new transaction.
        /// </summary>
        /// <param name="isolationLevel">
        ///  .
        /// </param>
        /// <param name="readOnly">
        ///  (Optional) true to read only.
        /// </param>
        /// <returns>
        ///  A MemoryTransaction.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public MemoryTransaction BeginTransaction(SessionIsolationLevel isolationLevel, bool readOnly = false)
        {
            if (isolationLevel == SessionIsolationLevel.Unspecified)
            {
                var s = Session.Current;
                isolationLevel = s != null ? Session.Current.SessionIsolationLevel : SessionIsolationLevel.ReadCommitted;
            }

            var tx = CreateTransaction(isolationLevel, readOnly);

            if (isolationLevel == SessionIsolationLevel.Serializable)
            {
                var list = GetActiveTransactions();
                tx.ActiveTransactionsWhenStarted = list;
                if (_trace.IsEnabled(TraceCategory.MemoryStore))
                {
                    _trace.WriteTrace(TraceCategory.MemoryStore, "Active transaction when tx {0} starts : {1}", tx.Id, list != null
                            ? String.Join(",", list.Select(t => t.Id).ToArray())
                            : "");
                }
            }

            return(tx);
        }
示例#6
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Create a new command context. If there is no current transaction, a transaction is created
        ///  and will be terminated when the context will be disposed.
        /// </summary>
        /// <param name="transactionManager">
        ///  .
        /// </param>
        /// <param name="readOnly">
        ///  true to read only.
        /// </param>
        /// <param name="isolationLevel">
        ///  (Optional)
        /// </param>
        ///-------------------------------------------------------------------------------------------------
        public CommandContext(ITransactionManager transactionManager, bool readOnly = false, SessionIsolationLevel isolationLevel = SessionIsolationLevel.Unspecified)
        {
            DebugContract.Requires(transactionManager, "transactionManager");

            _transactionManager = transactionManager;
            _currentTransaction = _transactionManager.CurrentTransaction ?? (_transaction = transactionManager.BeginTransaction(isolationLevel, readOnly));

            // On s'assure qu' il existe une transaction courante

            if (_currentTransaction.SessionIsolationLevel != SessionIsolationLevel.Serializable)
            {
                _activeTransactionsWhenStarted = transactionManager.GetActiveTransactions();
            }

            // N° de la commande dans le contexte de la transaction courante
            CommandId = _currentTransaction.GetAnIncrementCurrentCommandId();
        }