multithreading with fluent nhibernate

2019-06-13 15:25发布

问题:

private  ISessionFactory GetSessionFactory(string sessionFactoryConfigPath)

   {
        GetFullSessionFactoryFor(sessionFactoryConfigPath);
        while (!sessionFactoryReady) Thread.Sleep(1000);
        return (ISessionFactory)sessionFactories[sessionFactoryConfigPath];
 }


 private void GetFullSessionFactory(string sessionFactoryConfigPath)
    {       
   ThreadPool.QueueUserWorkItem(state =>
         {
           ISessionFactory sessionFactory=null;
           FluentConfiguration fluentConfiguration = fluentConfiguration.ExposeConfiguration(c => c.SetProperty("sessionfactoryname","somevalue"))
                                         .Mappings(m =>
                                         {
                                             m.FluentMappings
                                                 .AddFromAssembly(Assembly.Load("nameofassembly"))
                                                 .Conventions.Add(DefaultLazy.Always(),
                                                                  OptimisticLock.Is(x => x.All()),
                                                                  DynamicUpdate.AlwaysTrue(),
                                                                  DynamicInsert.AlwaysFalse(),
                                                                  DefaultCascade.None()
                                                                 )
                                                 .Conventions.AddFromAssemblyOf<"SomeConvention">();
                                         }
                                                  );

         sessionFactory = fluentConfiguration.BuildSessionFactory();

          });
}

I am creating minisession factory on main thread(not shown here) and full session factory on second thread. The problem is when it hits buildsessionfactory the code never returns back.Am i doing it right?

public class NHibernateBaseDAO<T>
{
    public NHibernateBaseDAO(string sessionFactoryConfigPath, int sessionId)
    {


        SessionFactoryConfigPath = sessionFactoryConfigPath;
        SessionId = sessionId;
    public bool Save(T entity)
    {
        bool saveSuccessful = true;
        try
        {
            NHibernateSession.Save(entity);
        }
        catch (NHibernate.HibernateException)
        {
            saveSuccessful = false;
        }
        return saveSuccessful;
    }

    public bool SaveOrUpdate(T entity)
    {
        bool saveSuccessful = true;
        try
        {
            NHibernateSession.SaveOrUpdate(entity);
        }
        catch (NHibernate.HibernateException)
        {
            saveSuccessful = false;
        }
        return saveSuccessful;
    }

    public void Delete(T entity)
    {
        NHibernateSession.Delete(entity);

    }

    public void CommitChanges()
    {
        if (NHibernateSessionManager.Instance.HasOpenTransactionOn(SessionFactoryConfigPath, this.SessionId))
        {
            NHibernateSessionManager.Instance.GetSessionFrom(SessionFactoryConfigPath, this.SessionId).Flush();
            NHibernateSessionManager.Instance.CommitTransactionOn(SessionFactoryConfigPath, this.SessionId);
        }
        else
        {
            NHibernateSessionManager.Instance.GetSessionFrom(SessionFactoryConfigPath, this.SessionId).Flush();
        }
    }

    public void BeginTransaction()
    {
        NHibernateSessionManager.Instance.BeginTransactionOn(SessionFactoryConfigPath, this.SessionId);
    }

    public void RollbackTransaction()
    {
        NHibernateSessionManager.Instance.RollbackTransactionOn(SessionFactoryConfigPath, this.SessionId);
    }

    public bool IsDirty()
    {
        return NHibernateSession.IsDirty();
    }

    public IQueryable<T> Query() {
        return (IQueryable<T>)NHibernateSession.Query<T>(); 

    }
    protected ISession NHibernateSession
    {
        get
        {
            return NHibernateSessionManager.Instance.GetSessionFrom(SessionFactoryConfigPath, this.SessionId);
        }
    }


    protected readonly string SessionFactoryConfigPath;
    protected int SessionId;

    protected System.Data.IDbConnection DbConnection
    {
        get { return NHibernateSessionManager.Instance.GetDbConnection(SessionFactoryConfigPath, this.SessionId); }
    }

    /// <summary>
    /// Return a list of object arrays. use this for general queries
    /// </summary>
    public System.Collections.IEnumerable GetSqlQuery(string queryString, IList<Criterion> criterion, Type returnType)
    {
        queryString += CriteriaToSql(criterion);

        return NHibernateSession.CreateQuery(queryString).Enumerable();
    }

    protected ICriteria AddCriteria(IList<Criterion> criterion)
    {
        ICriteria criteria = NHibernateSession.CreateCriteria(persistentType);

        foreach (Criterion criterium in criterion)
        {
            switch (criterium.Comparison)
            {
                case SqlComparison.StartsWith:
                    criteria.Add(Restrictions.InsensitiveLike(criterium.Property, criterium.Value1.ToString(), MatchMode.Start));
                    break;

                case SqlComparison.Contains:
                    criteria.Add(Restrictions.InsensitiveLike(criterium.Property, criterium.Value1.ToString(), MatchMode.Anywhere));
                    break;

                case SqlComparison.Equals:
                    criteria.Add(Restrictions.Eq(criterium.Property, criterium.Value1));
                    break;

                case SqlComparison.Between:
                    criteria.Add(Restrictions.Between(criterium.Property, criterium.Value1, criterium.Value2));
                    break;
                case SqlComparison.MoreThan:
                    criteria.Add(Restrictions.Gt(criterium.Property, criterium.Value1));
                    break;
                case SqlComparison.LessThan:
                    criteria.Add(Restrictions.Lt(criterium.Property, criterium.Value2));
                    break;
                case SqlComparison.InList:
                    criteria.Add(Restrictions.In(criterium.Property, (System.Collections.IList)criterium.Value1));
                    break;

            }
        }
        return criteria;
    }
    protected string CriteriaToSql(IList<Criterion> criterion)
    {

    }
    /// <summary>
    /// Get delimiter for data, defaults to ' unless specifed for data type
    /// </summary>
    protected string[] GetDelimiter(object value)
    {

}
public class Criterion
{
    public Criterion(string property, SqlComparison comparison, object value1)
    {
        Property = property;
        Comparison = comparison;
        Value1 = value1;
    }
    public Criterion(string property, SqlComparison comparison, object value1, object value2)
    {
        Property = property;
        Comparison = comparison;
        Value1 = value1;
        Value2 = value2;
    }
    public Criterion(string property, SqlComparison comparison, object value1, bool not)
    {
        Property = property;
        Comparison = comparison;
        Value1 = value1;
        Not = not;
    }
    public Criterion(string property, SqlComparison comparison, object value1, object value2, bool not)
    {
        Property = property;
        Comparison = comparison;
        Value1 = value1;
        Value2 = value2;
        Not = not;
    }
    public string Property { get; set; }
    public bool Not { get; set; }
    public SqlComparison Comparison { get; set; }
    public object Value1 { get; set; }
    public object Value2 { get; set; }
}
public enum SqlComparison { StartsWith, Contains, Equals, Between, MoreThan, LessThan,   InList }
 }

one last question please. I am using generic class to access sessionfactory so i cannot explicitly access the minisession. with this how do i access minisession based only on certain entities if the full session is not available and full session factory when it is available.

回答1:

you never set sessionfactoryready to true

Update: a more complete example.

void Main()
{
    Database.InitRealFactoryAsync("<sessionFactoryConfigPath>");

    var minifactory = Database.GetMiniFactory("<sessionFactoryConfigPath>");

    // Do some stuff with minifactory

    var realsessionfactory = Database.SessionFactory;

    // Do stuff with real factory
}

static class Database
{
    private static ISessionFactory sessionFactory;

    public void InitRealFactoryAsync(string sessionFactoryConfigPath)
    {
        ThreadPool.QueueUserWorkItem(state =>
        {
            sessionFactory = Fluently.Configure()
                .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load("nameofassembly"))
                    .Conventions.Add(DefaultLazy.Always(),
                        OptimisticLock.Is(x => x.All()),
                        DynamicUpdate.AlwaysTrue(),
                        DynamicInsert.AlwaysFalse(),
                        DefaultCascade.None())
                    .Conventions.AddFromAssemblyOf<FoxproDateConvention>())
                .BuildSessionFactory();
        });
    }

    public ISessionFactory GetMiniFactory(string sessionFactoryConfigPath)
    {
        var assembly = Assembly.Load("nameofassembly");
        return Fluently.Configure()
            .Mappings(m => m.FluentMappings.Add(assembly.GetTypes().Where(Filter).ToArray())
                .Conventions.Add(DefaultLazy.Always(),
                    OptimisticLock.Is(x => x.All()),
                    DynamicUpdate.AlwaysTrue(),
                    DynamicInsert.AlwaysFalse(),
                    DefaultCascade.None())
                .Conventions.AddFromAssemblyOf<FoxproDateConvention>())
                .BuildSessionFactory();
    }

    public static ISessionFactory SessionFactory
    {
        get {
            while (sessionFactory == null) Thread.Sleep(1000);
            return sessionFactory;
        }
    }
}

UpdateUpdate:

void Main()
{
    Database.InitRealFactoryAsync("<sessionFactoryConfigPath>");
    Database.InitMiniFactory("<sessionFactoryConfigPath>");

    using (var session = Database.GetSession(true))
    {
        // Do some stuff where minifactory is enough
    }

    using (var session = Database.GetSession())
    {
        // Do stuff with real factory
    }
    ...
}

// class Database
public ISession GetSession()
{
    return GetSession(false);
}

public ISession GetSession(bool miniFactoryIsEnough)
{
    if (realSessionfactory != null)
        return realSessionfactory.OpenSession();
    if (miniFactoryIsEnough)
        return miniSessionfactory.OpenSession();
    else
    {
        while (realSessionFactory == null) Thread.Sleep(1000);
        return realSessionfactory.OpenSession();
    }
}

Update: "access minisession based only on certain entities"

you need to specify the type you want to use in the session:

public ISession GetSession(Type persistentType)
{
    if (fullSessionfactory != null)
        return realSessionfactory.OpenSession();
    if (miniFactory.GetClassMetadata(persistentType) != null)
        return miniSessionfactory.OpenSession();
    else
    {
        // minifactory doesnt contain the type needed, wait for full factory
        while (fullSessionFactory == null) Thread.Sleep(1000);
        return fullSessionfactory.OpenSession();
    }
}

some additional advice

do not catch (NHibernate.HibernateException)

  • you lose valuable information and the calling code can't really decide what to do when false is returned
  • session state is inconsistent see https://stackoverflow.com/a/1819150/671619

FlushMode should be Flushmode.Commit and public void CommitChanges() can be written as

var session = NHibernateSession;
if (session.Transaction.IsActiv)
{
    session.Transaction.Commit();
}

cut out the whole sessionId stuff as it seems to provide no value. hold the session instead of sessionId instead