Ejemplo n.º 1
0
 /// <summary>
 /// 构造函数,对缓存边界对象进行初始化
 /// </summary>
 /// <param name="guid"></param>
 /// <param name="trans"></param>
 private Session(string guid, Transaction trans)
 {
     this._entityList = new List<BaseEntity>();
     _trans = trans;
     _sessionGuid = guid;
     this.OrignalDataAuth = this.IsDataAuth = true;
     this.IgnoreRootOrgFilter = false;
     this.IgnoreOrgFilter = false;
     this.IgnoreDataFilter = false;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 浅度的拷贝构造,目的是生成当前transaction的一个副本
        /// </summary>
        /// <param name="trans"></param>
        private Transaction(Transaction trans, TransactionSupport support)
        {
            this._guid = trans._guid;
            this._session = trans.Sessi;

            this._trans = trans.Trans;

            this._newTrans = false;
            this._buildSession = false;
            this._PTrans = trans;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据事物类型生成一个新的事物
        /// </summary>
        /// <param name="cacheGuid"></param>
        /// <param name="ts"></param>
        /// <returns></returns>
        internal static Transaction New(string cacheGuid, TransactionSupport ts, bool? useReadDB)
        {
            SessionCache cache = SessionCache.GetCache(cacheGuid, false);
            if (cache != null)
            {
                if (cache.CurTrans == null || cache.TranStack.Count == 0
                    || cache.CurSession == null || cache.SessionStack.Count == 0
                    || cache.CurTrans._session.SessionFactory.IsClosed)
                {
                    cache.Dispose();
                    cache = null;
                }
            }

            Transaction trans = null;
            ///如果当前外层没有session包裹则从参数中获取,如果外层有事务则
            ///根据外层事务判断当前到底使用哪个数据库
            //首先根据事务来判断是否使用读取数据库
            bool isReadDB = (ts == TransactionSupport.Support ? true : false);
            //如果存在cache则从cache中取
            if (cache != null)
            {
                isReadDB = cache.UseReadDB;
            }
            //如果显示设置了是否需要读取数据库从来源来获取
            if (useReadDB != null)
            {
                isReadDB = useReadDB ?? true;
            }
            if (cache == null)
            {
                //新的事务,重新生成cacheGuid
                cacheGuid = SessionGuid.NewGuid();
                cache = SessionCache.GetCache(cacheGuid, true);
                cache.UseReadDB = isReadDB;
                cache.AuthContext = Auth.AuthContext.GetInstance();
                ISessionFactory factory = SessionCache.GetSessionFactory(isReadDB);
                ISession session = factory.OpenSession();
                session.FlushMode = FlushMode.Commit;
                trans = new Transaction(cacheGuid, session, ts, cache);
                //创建新事务的时候需要初始化authcontext
                //当前是第一个事务
            }
            else
            {
                if (useReadDB == null)
                {
                    useReadDB = cache.UseReadDB;
                }
                if (ts == TransactionSupport.RequiredNew || (ts == TransactionSupport.Required && cache.CurTrans._trans == null) || isReadDB != cache.UseReadDB)
                {
                    ///如果当前存在事务的话并且上面代码没有显式要求使用哪一个数据库的话则
                    ///默认从缓存中获取当前数据库
                    NHExt.Runtime.Auth.AuthContext ctx = cache.AuthContext;
                    //新的事务,重新生成cacheGuid
                    cacheGuid = SessionGuid.NewGuid();
                    cache = SessionCache.GetCache(cacheGuid, true);
                    cache.AuthContext = ctx;
                    cache.UseReadDB = isReadDB;
                    ISessionFactory factory = SessionCache.GetSessionFactory(isReadDB);
                    ISession session = factory.OpenSession();
                    session.FlushMode = FlushMode.Commit;
                    trans = new Transaction(cacheGuid, session, ts, cache);
                }
                else
                {
                    //上层存在事务的话就不用考虑是否重新生成session
                    trans = new Transaction(cache.CurTrans, ts);
                }
            }
            cache.TranStack.Push(trans);
            cache.CurTrans = trans;
            ////生成事物之后自动创建一个session边界
            Session.New(cacheGuid);

            return trans;
        }