How to add a nhibernate transaction to ninject?
how can I make it so on every http request I start a transaction and at the end I commit my transactions?
I am already using InRequestScope for my sessions and have this for my ninject.
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
I know have ninject set to OnActivation and OnDeactivation
but what I am finding weird is this.
-- statement #1
begin transaction with isolation level: Unspecified
-- statement #2
select TOP ( 1 /* @p0 */ ) student0_.StudentId
-- statement #3
begin transaction with isolation level: Unspecified
-- statement #4
select TOP ( 1 /* @p0 */ ) student0_.StudentId
-- statement #5
select courseperm0_.PermissionId
-- statement #6
begin transaction with isolation level: Unspecified
-- statement #7
commit transaction
-- statement #8
SELECT this_.TaskReminderId as TaskRemi1_13_0_
-- statement #9
SELECT this_.ReminderId as ReminderId0_2_,
-- statement #10
SELECT this_.ReminderId as ReminderId8_2_,
The above is from the profiler but I stripped away most of the query as I did not think is relivent to the problem.
Look how is all of sudden for statement 8,9,10 it does not make a transaction for. But before that it made 3. I don't understand this.
Edit 2
I found this post
.OnActivation(session =>
{
session.BeginTransaction();
session.FlushMode = FlushMode.Commit;
})
It seems this does help a bit with my pro开发者_如何学Cblem(still have the problem with lazy loading). I am wondering why this works though and if something can go wrong by using this.
Add activation/deactivaion actions to your session binding:
.OnActivation(session => session.Transaction.Begin())
.OnDeactivation(CommitTransaction)
public void CommitTransaction(ISession session)
{
try
{
session.Transaction.Commit();
}
catch(Exception e)
{
// Add some exception handling (rollback, show error to user, ...)
throw;
}
}
精彩评论