Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 630187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:49:42+00:00 2026-05-13T19:49:42+00:00

I keep getting the following error: Cannot access a disposed object. Object name: ‘AdoTransaction’.

  • 0

I keep getting the following error:

Cannot access a disposed object.
Object name: ‘AdoTransaction’.

The setup follows the example given at http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html

here is the IUnitOfWork class (exactly the same as the one in the link):

public class UnitOfWork : IUnitOfWork
{
    private readonly ISessionFactory _sessionFactory;
    private readonly ITransaction _transaction;

    public UnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
        CurrentSession = _sessionFactory.OpenSession();
        _transaction = CurrentSession.BeginTransaction();
    }

    public ISession CurrentSession { get; private set; }

    public void Dispose()
    {
        CurrentSession.Close();
        CurrentSession = null;
    }

    public void Commit()
    {
        _transaction.Commit();
    }

}

here is the NHibernateModule (again exactly the same!):

public class NHibernateModule : IHttpModule, IDisposable
{
    private IUnitOfWork _unitOfWork;

    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
        context.EndRequest += ContextEndRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
        _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
    }

    private void ContextEndRequest(object sender, EventArgs e)
    {
        Dispose();
    }

    public void Dispose()
    {
        if (_unitOfWork == null) return;
        _unitOfWork.Commit();
        _unitOfWork.Dispose();
    }
}

here is my repo:

 public class Repository<T> : IRepository<T>
{
    public readonly IUnitOfWork _uow;

    public Repository(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public Repository()
    {

    }

    #region IRepository<T> Members

    public IList<T> GetAll()
    {
        using (var session = _uow.CurrentSession)
        {
            return session.CreateCriteria(typeof (T)).List<T>();
        }
    }

    public IList<T> FindAll<T>(IList<Expression<Func<T, bool>>> criteria)
    {
        var session = _uow.CurrentSession;

        var query = from item in session.SessionFactory.OpenSession().Query<T>()
                              select item;
        foreach (var criterion in criteria)
        {
            query = query.Where(criterion);
        }
        return query.ToList();
    }

    public T FindFirst<T>(IList<Expression<Func<T, bool>>> criteria)
    {

        var col = FindAll(criteria);

        if (col.Count > 0)
        {
            return col.First();
        }
        else
        {
            return default(T);
        }
    }

    public T Get(int id)
    {
        using (var session = _uow.CurrentSession)
        {
            return session.Get<T>(id);
        }
    }

    public void Save(T entity)
    {
        using (var session = _uow.CurrentSession)
        {
            session.Save(entity);
        }
    }

    public void Update(T entity)
    {
        using (var session = _uow.CurrentSession)
        {
            session.Update(entity);
            session.Flush();
        }
    }

    #endregion
}
}

here is my bootstrapper:

 public class BootStrapper : IBootstrapper
{
    private static bool _hasStarted;

    public virtual void BootstrapStructureMap()
    {
        ObjectFactory.Initialize(x =>
        {
            x.Scan(s =>
            {
                s.TheCallingAssembly();
                s.AssemblyContainingType<User>();
                s.AssemblyContainingType<UserRepository>();
                s.AssemblyContainingType<NHibernateRegistry>();
                s.LookForRegistries();
            });

            // Repositories
            x.For<WmcStar.Data.IUserRepository>()
                .CacheBy(InstanceScope.HttpContext)
                .TheDefault.Is.OfConcreteType<UserRepository>();

            x.For<IDatabaseBuilder>().TheDefaultIsConcreteType<DatabaseBuilder>(); 

        });
    }

    public static void Restart()
    {
        if (_hasStarted)
        {
            ObjectFactory.ResetDefaults();
        }
        else
        {
            Bootstrap();
            _hasStarted = true;
        }
    }

    public static void Bootstrap()
    {
        new BootStrapper().BootstrapStructureMap();
    }

}

here is my NHibernateRegistry:

public class NHibernateRegistry : Registry
{
    public NHibernateRegistry()
    {
        var cfg = new Configuration()
             .SetProperty(NHibernate.Cfg.Environment.ReleaseConnections, "on_close")
             .SetProperty(NHibernate.Cfg.Environment.Dialect, typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName)
             .SetProperty(NHibernate.Cfg.Environment.ConnectionDriver, typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName)
             .SetProperty(NHibernate.Cfg.Environment.ConnectionString, @"my connstring")
             .SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
             .AddAssembly(typeof(User).Assembly);

        var sessionFactory = cfg.BuildSessionFactory();

        For<Configuration>().Singleton().Use(cfg);

        For<ISessionFactory>().Singleton().Use(sessionFactory);


        For<ISession>().HybridHttpOrThreadLocalScoped()
            .Use(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());

        For<IUnitOfWork>().HybridHttpOrThreadLocalScoped()
            .Use<UnitOfWork>();

        For<IDatabaseBuilder>().Use<DatabaseBuilder>();
        SetAllProperties(x => x.OfType<IUnitOfWork>());
    }
}

and finaly here is my global.asax:

    public class Global : System.Web.HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        BootStrapper.Bootstrap();
        new SchemaExport(ObjectFactory.GetInstance<Configuration>()).Execute(false, true, false);
        ObjectFactory.GetInstance<IDatabaseBuilder>().RebuildDatabase();

        AutoMapper.Mapper.CreateMap<WmcStar.Model.User, WmcStar.Data.Dto.User>();
    }
}

Anyone got any clues as to what would cause this?

w://

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:49:42+00:00Added an answer on May 13, 2026 at 7:49 pm

    I don’t know what the exact problem is that causes the exception, but I have found some potential issues:

    1. Commit the unit of work at the end of the request, and roll it back when commit fails.
    2. Rollback the unit of work when an exception is thrown during the request.
    3. Don’t call dispose at the end of the request, it’s abusing the IDisposable of the class.
    4. Dispose the unit of work at the end of the request, when not already disposed.
    5. Do not dispose the session in every method in your repository.
    6. Dispose the session in the unit of work dispose method.
    7. You don’t have to explicitly close the session. disposing is enough.
    8. Dispose the transaction before the session in the dispose method.
    9. Use checks in the dispose of the unit of work method to prevent memory leaks and make the dispose method garbage collector proof.

    After typing this list, I think the exception is caused by the using blocks in the repository.

    If you want an explanation for one of these things, please ask me in the comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Mock framework is a good option for unit testing.… May 13, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer Thanks everybody, Finally I solved it changing all plugin configurations… May 13, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer I didn't understand 'when' you want to open the popup,… May 13, 2026 at 10:49 pm

Related Questions

im calling an actionscript class from my main mxml file. the actionscript class is
I have the following code which takes an improperly saved Image from the database
I am trying to load an image in the background and then update the
I'm diong a typical Windows Integrated Connection from a .Net Winforms Application: Dim sqlConnetion
I currently have a listbox that has its selected item bound to a property

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.