Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="parent"></param>
        /// <param name="il"></param>
        /// <param name="option"></param>
        /// <param name="loggerFactory"></param>
        /// <param name="connectionId"></param>
        public TransScope(IDbConnection conn, TransScope parent, IsolationLevel il = IsolationLevel.ReadCommitted, TransScopeOption option = TransScopeOption.Required, ILoggerFactory loggerFactory = null, string connectionId = null)
        {
            if (loggerFactory != null)
            {
                _LoggerFactory = loggerFactory;
                _Logger        = loggerFactory.CreateLogger <TransScope>();
            }

            Guid          = System.Guid.NewGuid().ToString();
            Parent        = parent;
            Option        = option;
            _Conn         = conn;
            _ConnectionId = connectionId;
            //当前事务为内嵌事务
            if (Parent != null && Option == TransScopeOption.Required)
            {
                _Trans = Parent.Trans;
                _State = Parent.State;
                LogDebug("Create Nest TransScope. DbConnectionWrap.Guid={0}, TransScope.Guid={1}", _ConnectionId, Guid);
            }
            else
            {
                LogDebug("Create Root TransScope. DbConnectionWrap.Guid={0}, TransScope.Guid={1}", _ConnectionId, Guid);
            }
            _IsolationLevel = il;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 开启事务
        /// </summary>
        /// <param name="option"></param>
        /// <param name="il"></param>
        /// <returns></returns>
        public TransScope TransScope(TransScopeOption option = TransScopeOption.Required, IsolationLevel il = IsolationLevel.ReadCommitted)
        {
            var conn = CreateConnectionWrap(option == TransScopeOption.RequireNew);

            RegistTransScopeConnection(conn);
            var scope = new TransScope(conn, conn.TransScope, il, option, LoggerFactory, conn.Guid);

            conn.TransScope   = scope;
            scope.OnDisposed += TransScope_OnDisposed;
            return(scope);
        }