/// <summary>
        /// Takes care of freeing the managed and unmanaged resources that
        /// this class is responsible for.
        /// </summary>
        /// <param name="isDisposing">Indicates if this AdoTransaction is being Disposed of or Finalized.</param>
        /// <remarks>
        /// If this AdoTransaction is being Finalized (<c>isDisposing==false</c>) then make sure not
        /// to call any methods that could potentially bring this AdoTransaction back to life.
        /// </remarks>
        protected virtual void Dispose(bool isDisposing)
        {
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                if (_isAlreadyDisposed)
                {
                    // don't dispose of multiple times.
                    return;
                }
                _isAlreadyDisposed = true;

                // free managed resources that are being managed by the AdoTransaction if we
                // know this call came through Dispose()
                if (isDisposing)
                {
                    if (trans != null)
                    {
                        trans.Dispose();
                        trans = null;
                        log.Debug("DbTransaction disposed.");
                    }

                    if (IsActive && session != null)
                    {
                        // Assume we are rolled back
                        AfterTransactionCompletion(false);
                    }
                    // nothing for Finalizer to do - so tell the GC to ignore it
                    GC.SuppressFinalize(this);
                }

                // free unmanaged resources here
            }
        }
 /// <inheritdoc />
 public override async Task <IList <T> > ListAsync <T>(CancellationToken cancellationToken = default)
 {
     using (SessionIdLoggingContext.CreateOrNull(Session.SessionId))
     {
         return((IList <T>) await ListAsyncImpl <T>(null, cancellationToken));
     }
 }
        public override IEnumerable <T> Enumerable <T>()
        {
            using (SessionIdLoggingContext.CreateOrNull(Session.SessionId))
            {
                //implement an interator which keep the id/class for each hit and get the object on demand
                //cause I can't keep the searcher and hence the hit opened. I dont have any hook to know when the
                //user stop using it
                //scrollable is better in this area

                //find the directories
                IndexSearcher searcher = BuildSearcher();
                if (searcher == null)
                {
                    return(new IteratorImpl <T>(new List <EntityInfo>(), noLoader).Iterate());
                }

                try
                {
                    var infos = ExtractEntityInfos(searcher);
                    return(new IteratorImpl <T>(infos, GetLoader((ISession)Session)).Iterate());
                }
                catch (IOException e)
                {
                    throw new HibernateException("Unable to query Lucene index", e);
                }
                finally
                {
                    CloseSearcher(searcher);
                }
            }
        }
 public override IEnumerable Enumerable()
 {
     using (SessionIdLoggingContext.CreateOrNull(Session.SessionId))
     {
         return(Enumerable <object>());
     }
 }
Example #5
0
        /// <summary>
        /// Takes care of freeing the managed and unmanaged resources that
        /// this class is responsible for.
        /// </summary>
        /// <param name="isDisposing">Indicates if this AdoTransaction is being Disposed of or Finalized.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <remarks>
        /// If this AdoTransaction is being Finalized (<c>isDisposing==false</c>) then make sure not
        /// to call any methods that could potentially bring this AdoTransaction back to life.
        /// </remarks>
        protected virtual async Task DisposeAsync(bool isDisposing, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                if (_isAlreadyDisposed)
                {
                    // don't dispose of multiple times.
                    return;
                }
                _isAlreadyDisposed = true;

                // free managed resources that are being managed by the AdoTransaction if we
                // know this call came through Dispose()
                if (isDisposing)
                {
                    if (trans != null)
                    {
                        trans.Dispose();
                        trans = null;
                        log.Debug("DbTransaction disposed.");
                    }

                    if (IsActive && session != null)
                    {
                        // Assume we are rolled back
                        await(AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
                    }
                    // nothing for Finalizer to do - so tell the GC to ignore it
                    GC.SuppressFinalize(this);
                }

                // free unmanaged resources here
            }
        }
 internal static IDisposable BeginContext(this ISessionImplementor session)
 {
     if (session == null)
     {
         return(null);
     }
     return(session is AbstractSessionImpl impl
                         ? impl.BeginContext()
                         : SessionIdLoggingContext.CreateOrNull(session.SessionId));
 }
 public void Dispose()
 {
     _context?.Dispose();
     _context = null;
     if (_session == null)
     {
         throw new ObjectDisposedException("The session process helper has been disposed already");
     }
     _session._processing = false;
     _session             = null;
 }
 internal static IDisposable BeginProcess(this ISessionImplementor session)
 {
     if (session == null)
     {
         return(null);
     }
     return(session is AbstractSessionImpl impl
                         ? impl.BeginProcess()
            // This method has only replaced bare call to setting the id, so this fallback is enough for avoiding a
            // breaking change in case a custom session implementation is used.
                         : SessionIdLoggingContext.CreateOrNull(session.SessionId));
 }
Example #9
0
        public void Disabled()
        {
            var guid = Guid.Empty;

            using (SessionIdLoggingContext.CreateOrNull(guid))
            {
                Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
                using (SessionIdLoggingContext.CreateOrNull(guid))
                {
                    Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
                }
                Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
            }
            Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
        }
Example #10
0
        /// <summary>
        /// Takes care of freeing the managed and unmanaged resources that
        /// this class is responsible for.
        /// </summary>
        /// <param name="isDisposing">Indicates if this AdoTransaction is being Disposed of or Finalized.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <remarks>
        /// If this AdoTransaction is being Finalized (<c>isDisposing==false</c>) then make sure not
        /// to call any methods that could potentially bring this AdoTransaction back to life.
        /// </remarks>
        protected virtual async Task DisposeAsync(bool isDisposing, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                if (_isAlreadyDisposed)
                {
                    // don't dispose of multiple times.
                    return;
                }
                _isAlreadyDisposed = true;

                // free managed resources that are being managed by the AdoTransaction if we
                // know this call came through Dispose()
                if (isDisposing)
                {
                    try
                    {
                        if (trans != null)
                        {
                            trans.Dispose();
                            trans = null;
                            log.Debug("DbTransaction disposed.");
                        }

                        if (IsActive)
                        {
                            // Assume we are rolled back
                            rolledBack = true;
                            if (session != null)
                            {
                                await(AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
                            }
                        }
                        // nothing for Finalizer to do - so tell the GC to ignore it
                        GC.SuppressFinalize(this);
                    }
                    finally
                    {
                        // Do not leave the object in an inconsistent state in case of disposal failure: we should assume
                        // the DbTransaction is either no more ongoing or unrecoverable.
                        begun = false;
                    }
                }

                // free unmanaged resources here
            }
        }
 public ProcessHelper(AbstractSessionImpl session)
 {
     _session = session;
     _context = new SessionIdLoggingContext(session.SessionId);
     try
     {
         _session.CheckAndUpdateSessionStatus();
         _session._processing = true;
     }
     catch
     {
         _context.Dispose();
         _context = null;
         throw;
     }
 }
Example #12
0
        public void Enabled()
        {
            var guid = Guid.NewGuid();

            using (SessionIdLoggingContext.CreateOrNull(guid))
            {
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
                var guid2 = Guid.NewGuid();
                using (SessionIdLoggingContext.CreateOrNull(guid2))
                {
                    Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid2));
                }
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
            }
            Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
        }
Example #13
0
        /// <summary>
        /// Rolls back the <see cref="ITransaction"/> by calling the method <c>Rollback</c>
        /// on the underlying <see cref="DbTransaction"/>.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Rollback()</c> on
        /// the underlying <see cref="DbTransaction"/>.
        /// </exception>
        public async Task RollbackAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                CheckNotDisposed();
                CheckBegun();
                CheckNotZombied();

                log.Debug("Rollback");

                if (!commitFailed)
                {
                    try
                    {
                        trans.Rollback();
                        log.Debug("DbTransaction RolledBack");
                        rolledBack = true;
                        Dispose();
                    }
                    catch (HibernateException e)
                    {
                        log.Error(e, "Rollback failed");
                        // Don't wrap HibernateExceptions
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error(e, "Rollback failed");
                        throw new TransactionException("Rollback failed with SQL Exception", e);
                    }
                    finally
                    {
                        await(AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
                        CloseIfRequired();
                    }
                }
            }
        }
Example #14
0
        public async Task DisabledAsync()
        {
            var guid = Guid.Empty;

            using (SessionIdLoggingContext.CreateOrNull(guid))
            {
                Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
                await Task.Delay(1).ConfigureAwait(false);

                Assert.That(SessionIdLoggingContext.SessionId, Is.Null);

                using (SessionIdLoggingContext.CreateOrNull(guid))
                {
                    Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
                    await Task.Delay(1).ConfigureAwait(false);

                    Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
                }
                Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
            }
            Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
        }
Example #15
0
        /// <summary>
        /// Rolls back the <see cref="ITransaction"/> by calling the method <c>Rollback</c>
        /// on the underlying <see cref="DbTransaction"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Rollback()</c> on
        /// the underlying <see cref="DbTransaction"/>.
        /// </exception>
        public void Rollback()
        {
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                CheckNotDisposed();
                CheckBegun();
                CheckNotZombied();

                log.Debug("Rollback");

                if (!commitFailed)
                {
                    try
                    {
                        trans.Rollback();
                        log.Debug("DbTransaction RolledBack");
                        rolledBack = true;
                        Dispose();
                    }
                    catch (HibernateException e)
                    {
                        log.Error(e, "Rollback failed");
                        // Don't wrap HibernateExceptions
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error(e, "Rollback failed");
                        throw new TransactionException("Rollback failed with SQL Exception", e);
                    }
                    finally
                    {
                        AfterTransactionCompletion(false);
                        CloseIfRequired();
                    }
                }
            }
        }
Example #16
0
        public async Task EnabledAsync()
        {
            var guid = Guid.NewGuid();

            using (SessionIdLoggingContext.CreateOrNull(guid))
            {
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
                await Task.Delay(1).ConfigureAwait(false);

                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));

                var guid2 = Guid.NewGuid();
                using (SessionIdLoggingContext.CreateOrNull(guid2))
                {
                    Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid2));
                    await Task.Delay(1).ConfigureAwait(false);

                    Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid2));
                }
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
            }
            Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
        }