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

  • SEARCH
  • Home
  • 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 5945921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:44:58+00:00 2026-05-22T16:44:58+00:00

I am going threw my site with nhibernate profiler and I got this message

  • 0

I am going threw my site with nhibernate profiler and I got this message

Alert: Use of implicit transactions is
discouraged

http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions

I see they are on every single select statement.

private readonly ISession session;

public OrderHistoryRepo(ISession session)
{
    this.session = session;
}

public void Save(OrderHistory orderHistory)
{
    session.Save(orderHistory);
}

public List<OrderHistory> GetOrderHistory(Guid Id)
{
    List<OrderHistory> orderHistories = session.Query<OrderHistory>().Where(x => x.Id == Id).ToList();
    return orderHistories;
}

public void Commit()
{
    using (ITransaction transaction = session.BeginTransaction())
    {
        transaction.Commit();
    }
}

Should I be wrapping my GetOrderHistory with a transaction like I have with my commit?

Edit

How would I wrap select statements around with a transaction? Would it be like this? But then “transaction” is never used.

    public List<OrderHistory> GetOrderHistory(Guid Id)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {       

 List<OrderHistory> orderHistories = session.Query<OrderHistory>().Where(x => x.Id == Id).ToList();
        return orderHistories;
        }
    }

Edit

Ninject (maybe I can leverage it to help me out like I did with getting a session)

public class NhibernateSessionFactory
    {
        public ISessionFactory GetSessionFactory()
        {
           ISessionFactory fluentConfiguration = Fluently.Configure()
                                                  .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")))
                                                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Map>().Conventions.Add(ForeignKey.EndsWith("Id")))
                                                  .ExposeConfiguration(cfg => cfg.SetProperty("adonet.batch_size", "20"))
                                                  //.ExposeConfiguration(BuidSchema)
                                                  .BuildSessionFactory();

            return fluentConfiguration;
        }

        private static void BuidSchema(NHibernate.Cfg.Configuration config)
        {
            new NHibernate.Tool.hbm2ddl.SchemaExport(config).Create(false, true);
        }
    }


public class NhibernateSessionFactoryProvider : Provider<ISessionFactory>
    {   
        protected override ISessionFactory CreateInstance(IContext context)
        {
            var sessionFactory = new NhibernateSessionFactory();
            return sessionFactory.GetSessionFactory();
        }
    }

  public class NhibernateModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
            Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
        }
    }

Edit 3

If I do this

    public List<OrderHistory> GetOrderHistory(Guid Id)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {       

 List<OrderHistory> orderHistories = session.Query<OrderHistory>().Where(x => x.Id == Id).ToList();
        return orderHistories;
        }
    }

I get this alert

If I do this

    public List<OrderHistory> GetOrderHistory(Guid Id)
    {
        using (ITransaction transaction = session.BeginTransaction())
        {       

 List<OrderHistory> orderHistories = session.Query<OrderHistory>().Where(x => x.Id == Id).ToList().ConvertToLocalTime(timezoneId);
        transaction.Commit();
        return orderHistories;
        }
    }

I can get rid of the errors but can get unexpected results.

For instance when I get orderHistories back I loop through all of them and convert the “purchase date” to the users local time. This is done through an extension method that I created for my list.

Once converted I set it to override the “purchase date” in the object. This way I don’t have to create a new object for one change of a field.

Now if I do this conversion of dates before I call the commit nhibernate thinks I have updated the object and need to commit it.

So I am putting a bounty on this question.

  1. How can I create my methods so I don’t have to wrap each method in a transaction? I am using ninject already for my sessions so maybe I can leverage that however some times though I am forced to do multiple transactions in a single request.

So I don’t know have just one transaction per request is a soultion.

  1. how can I make sure that objects that I am changing for temporary use don’t accidentally get commit?

  2. how can I have lazy loading that I am using in my service layer. I don’t want to surround my lazy loading stuff in a transaction since it usually used in my service layer.

I am finding it very hard to find examples of how to do it when your using the repository pattern. With the examples everything is always written in the same transaction and I don’t want to have transactions in my service layer(it is the job of the repo not my business logic)

  • 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-22T16:44:59+00:00Added an answer on May 22, 2026 at 4:44 pm

    The NHibernate community recommends that you wrap everything in a transaction, regardless of what you’re doing.

    To answer your second question, generally, it depends. If this is a web application, you should look at the session-per-request pattern. In most basic scenarios, what this means is that you’ll create a single session per HTTP request in which the session (and transaction) is created when the request is made and committed/disposed of at the end of the request. I’m not saying that this is the right way for you, but it’s a common approach that works well for most people.

    There are a lot of examples out there showing how this can be done. Definitely worth taking the time to do a search and read through things.


    EDIT: Example of how I do the session/transaction per request:

    I have a SessionModule that loads the session from my dependency resolver (this is a MVC3 feature):

    namespace My.Web
    {
        public class SessionModule : IHttpModule {
            public void Init(HttpApplication context) {
                context.BeginRequest += context_BeginRequest;
                context.EndRequest += context_EndRequest;
            }
    
            void context_BeginRequest(object sender, EventArgs e) {
                var session = DependencyResolver.Current.GetService<ISession>();
                session.Transaction.Begin();
            }
    
            void context_EndRequest(object sender, EventArgs e) {
                var session = DependencyResolver.Current.GetService<ISession>();
                session.Transaction.Commit();
                session.Dispose(); 
            }
    
            public void Dispose() {}
        }
    }
    

    This is how I register the session (using StructureMap):

    new Container(x => {
    
        x.Scan(a => {
            a.AssembliesFromApplicationBaseDirectory();
            a.WithDefaultConventions();
        });
    
        x.For<ISessionFactory>().Singleton().Use(...);
        x.For<ISession>().HybridHttpOrThreadLocalScoped().Use(sf => sf.GetInstance<ISessionFactory>().OpenSession());
        x.For<StringArrayType>().Use<StringArrayType>();
    
    });
    

    Keep in mind that this is something I’ve experimented with and have found to work well for the scenarios where I’ve used NHibernate. Other people may have different opinions (which are, of course, welcome).

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

Sidebar

Related Questions

I recently came across this site. http://beta.rallyinteractive.com/ . The site uses some fairly complex/awesome
I have got a bit of 'brain fade' going on this afternoon, so if
Going from the example given here... http://ericswann.org/blog/archive/2009/04/06/linq-to-sql-datacontext-provider-revisited.aspx I'm trying to use the datacontext between
Going to http://www.example.com/node/NID/edit , where NID is any valid nid referring to a node
I guess this is going to sound like a pretty basic question to any
When I load the homepage of a ZF site I get a message saying
The problem i see with this code, is that it's going to be reused
This is a new one for me. I upgraded a Drupal site from 6.20
The idea for the site is going to be a main page with 6
I am going through our old site files and data that has our 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.