コード例 #1
0
 internal void UseTransaction([InstantHandle] Action <SessionWrapper> action,
                              IsolationLevel?isolationLevel = null)
 {
     UseTransaction(session =>
     {
         action(session);
         return(true);
     }, isolationLevel ?? Options.TransactionIsolationLevel);
 }
コード例 #2
0
        public void SaveChanged()
        {
            //生成数据库适配器
            foreach (var x in p_ComponentServices)
            {
                p_ComponentDatabaseContainer.GetDatabaseAdptor(x);
            }

            //开始事务
            var xxx = p_ComponentDatabaseContainer.GeneratesAdptor;

            if (xxx != null)
            {
                IsolationLevel?al = TransactionLevel;
                foreach (var g in xxx)
                {
                    if (al != null && al.HasValue)
                    {
                        g.BeginTransaction(al.Value);
                    }
                    else
                    {
                        g.BeginTransaction();
                    }
                }
            }

            try
            {
                //保存更新
                foreach (var x in p_ComponentServices)
                {
                    SaveChanged(x);
                    x.SuccessUpdate();
                }
                //提交事务
                if (xxx != null)
                {
                    foreach (var g in xxx)
                    {
                        g.CommitTran();
                    }
                }
            }
            catch (Exception e)
            {
                //回滚事务
                if (xxx != null)
                {
                    foreach (var g in xxx)
                    {
                        g.RollbackTran();
                    }
                }
                throw new Exception(e.Message, e);
            }
        }
コード例 #3
0
 /// <summary> 执行事务(新开连接) </summary>
 /// <param name="action"></param>
 /// <param name="level"></param>
 /// <param name="connectionName"></param>
 protected void Transaction(Action <IDbConnection, IDbTransaction> action, string connectionName = null,
                            IsolationLevel?level = null)
 {
     Transaction((conn, trans) =>
     {
         action.Invoke(conn, trans);
         return(true);
     }, connectionName, level);
 }
コード例 #4
0
        public NoOpTransaction(IsolationLevel?isolation = null)
        {
            this.isolation = isolation ?? IsolationLevel.Unspecified;

            if (isolation != null)
            {
                this.isolation = isolation.Value;
            }
        }
コード例 #5
0
 public SessionConnectionCollection(bool readOnly, IsolationLevel?isolationLevel, ISessionConnectionFactory factory, ISessionExceptionHandler exceptionHandler)
 {
     _readOnly              = readOnly;
     _isolationLevel        = isolationLevel;
     _factory               = factory;
     _exceptionHandler      = exceptionHandler;
     InitializedConnections = new Dictionary <Type, ISessionConnection>();
     _transactions          = new Dictionary <ISessionConnection, IDbTransaction>();
 }
 public void RunInTransaction(
     Action <DbConnection, DbTransaction> action, IsolationLevel?isolationLevel = null)
 {
     RunInTransaction((connection, transaction) =>
     {
         action(connection, transaction);
         return(true);
     }, isolationLevel);
 }
 public Task RunInTransactionAsync(
     Func <DbConnection, DbTransaction, Task> func, IsolationLevel?isolationLevel = null)
 {
     return(RunInTransactionAsync(async(connection, transaction) =>
     {
         await func(connection, transaction);
         return true;
     }, isolationLevel));
 }
コード例 #8
0
 public DbContextHolder(bool isReadonly, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _isReadonly       = isReadonly;
     _isolationLevel   = isolationLevel;
     _dbContextFactory = dbContextFactory;
     _isDisposed       = false;
     _instantiatedDbContextCollection  = new Dictionary <Type, DbContext>();
     _instantiatedDbContextTransaction = new Dictionary <DbContext, IDbContextTransaction>();
 }
コード例 #9
0
 internal NonAmbientDbContextScope(DbContextOption.Mode mode,
                                   IsolationLevel?isolationLevel,
                                   DbTransaction dbTransaction,
                                   DbConnection sqlConnection,
                                   AmbientDbContextHouseKeeper ambientDbContextHouseKeeper)
     : base(mode, isolationLevel, dbTransaction, sqlConnection)
 {
     _ambientDbContextHouseKeeper = ambientDbContextHouseKeeper;
 }
コード例 #10
0
ファイル: UnitOfWorkBase.cs プロジェクト: lulzzz/Virgo
 /// <summary>
 /// 开启事务
 /// </summary>
 /// <param name="isolationLevel"></param>
 public void BeginTransaction(IsolationLevel?isolationLevel = null)
 {
     IsBeginCalled();
     Transaction = Connection.BeginTransaction();
     if (isolationLevel.HasValue)
     {
         Transaction = Connection.BeginTransaction(isolationLevel.Value);
     }
 }
コード例 #11
0
 public IContextBuilder UseTransaction(IsolationLevel il)
 {
     if (IsOpen)
     {
         throw ExecutionContextException.ConnectionAlreadyOpen();
     }
     _isolationLevel = il;
     return(this);
 }
コード例 #12
0
 public void RunInTransaction(
     Action <DbConnection, DbTransaction> action, IsolationLevel?isolationLevel)
 {
     RunInTransaction((_1, _2) =>
     {
         action(_1, _2);
         return(true);
     }, isolationLevel);
 }
コード例 #13
0
 public Task RunInTransactionAsync(
     Func <DbConnection, DbTransaction, Task> func, IsolationLevel?isolationLevel)
 {
     return(RunInTransactionAsync((_1, _2) =>
     {
         func(_1, _2).GetAwaiter().GetResult();
         return Task.FromResult(true);
     }, isolationLevel));
 }
コード例 #14
0
        public NHibernateTransaction(ISession session, IsolationLevel?isolation = null)
        {
            Assertion.NotNull(session);

            this.session     = session;
            this.transaction = isolation != null?session.BeginTransaction(isolation.Value) : session.BeginTransaction();

            this.isolation = isolation ?? IsolationLevel.Unspecified;
        }
コード例 #15
0
 private TransactionScope CreateTransaction(IsolationLevel?isolationLevel)
 {
     return(isolationLevel != null
         ? new TransactionScope(TransactionScopeOption.Required,
                                new TransactionOptions {
         IsolationLevel = isolationLevel.Value, Timeout = _options.TransactionTimeout
     })
         : new TransactionScope());
 }
コード例 #16
0
        public DbContextCollection(IsolationLevel?isolationLevel = null)
        {
            _disposed  = false;
            _completed = false;

            InitializedDbContexts = new Dictionary <Type, DbContext>();
            _transactions         = new Dictionary <DbContext, DbContextTransaction>();

            _isolationLevel = isolationLevel;
        }
コード例 #17
0
        internal T UseTransaction <T>(
            [InstantHandle] Func <MySqlConnection, T> func, IsolationLevel?isolationLevel)
        {
            return(UseConnection(connection =>
            {
                T result = func(connection);

                return result;
            }));
        }
コード例 #18
0
            /// <summary>
            /// Get system isolation level by data isolation level
            /// </summary>
            /// <param name="dataIsolationLevel">Data isolation level</param>
            /// <returns>Return system data isolation level</returns>
            internal static IsolationLevel?GetSystemIsolationLevel(DataIsolationLevel?dataIsolationLevel)
            {
                IsolationLevel?isolationLevel = null;

                if (dataIsolationLevel.HasValue && SystemDataIsolationLevelMaps.ContainsKey(dataIsolationLevel.Value))
                {
                    isolationLevel = SystemDataIsolationLevelMaps[dataIsolationLevel.Value];
                }
                return(isolationLevel);
            }
コード例 #19
0
ファイル: DatabaseContext.cs プロジェクト: youye00/higlabo
 private void Initialize(Database database, String transactionKey, IsolationLevel?isolationLevel)
 {
     this.TransactionKey = transactionKey;
     this.Database       = database;
     DatabaseContext.SetDatabaseContext(this.TransactionKey, this);
     if (isolationLevel.HasValue == true)
     {
         this.BeginTransaction(isolationLevel.Value);
     }
 }
コード例 #20
0
 internal T UseTransaction <T>([InstantHandle] Func <SessionWrapper, T> func,
                               IsolationLevel?isolationLevel = null)
 {
     using (var transaction = CreateTransaction(isolationLevel ?? Options.TransactionIsolationLevel))
     {
         var result = UseSession(func);
         transaction.Complete();
         return(result);
     }
 }
コード例 #21
0
 private IsolationLevel?FixIsolationLevelForOracle(IsolationLevel?isolationLevel)
 {
     if ((ProviderType == ProviderTypeEnum.OracleClient9 || ProviderType == ProviderTypeEnum.OracleClient9Managed ||
          ProviderType == ProviderTypeEnum.OracleClient10 || ProviderType == ProviderTypeEnum.OracleClient10Managed) &&
         (isolationLevel != IsolationLevel.ReadCommitted || isolationLevel != IsolationLevel.Serializable))
     {
         isolationLevel = IsolationLevel.ReadCommitted;
     }
     return(isolationLevel);
 }
コード例 #22
0
        /// <summary>
        /// get system isolation level by data isolation level
        /// </summary>
        /// <param name="dataIsolationLevel">data isolation level</param>
        /// <returns></returns>
        public static IsolationLevel?GetSystemIsolationLevel(DataIsolationLevel?dataIsolationLevel)
        {
            IsolationLevel?isolationLevel = null;

            if (dataIsolationLevel.HasValue && DataIsolationLevelMapCollection.ContainsKey(dataIsolationLevel.Value))
            {
                isolationLevel = DataIsolationLevelMapCollection[dataIsolationLevel.Value];
            }
            return(isolationLevel);
        }
コード例 #23
0
        public DbContextCollection(bool readOnly = false, IsolationLevel?isolationLevel = null, IDbContextFactory dbContextFactory = null)
        {
            _disposed = _completed = false;
            _initializedDbContexts = new Dictionary <Type, DbContext>();
            _transactions          = new Dictionary <DbContext, DbContextTransaction>();

            _readOnly         = readOnly;
            _isolationLevel   = isolationLevel;
            _dbContextFactory = dbContextFactory;
        }
コード例 #24
0
        internal T UseTransaction <T>([InstantHandle] Func <SqlConnection, T> func, IsolationLevel?isolationLevel)
        {
            using (var transaction = CreateTransaction(isolationLevel ?? _options.TransactionIsolationLevel))
            {
                var result = UseConnection(func);
                transaction.Complete();

                return(result);
            }
        }
コード例 #25
0
ファイル: Service.cs プロジェクト: geffzhang/core
        public DbContext NewDbContext(IsolationLevel?level = null)
        {
            DbContext dc = new DbContext(this);

            if (level != null)
            {
                dc.Begin(level.Value);
            }
            return(dc);
        }
コード例 #26
0
        public static IsolationLevel?GetIsolationLevelFromObject <T>(object obj)
        {
            IsolationLevel?isolationLevel = null;

            if (obj is T)
            {
                isolationLevel = ((dynamic)(T)obj).IsolationLevel;
            }
            return(isolationLevel);
        }
コード例 #27
0
        public IDbTransaction BeginTransaction(IsolationLevel?isolationLevel = null)
        {
            IDbTransaction transaction = isolationLevel.HasValue
                ? _connection.BeginTransaction(isolationLevel.Value)
                : _connection.BeginTransaction();

            _transactions.Push(transaction);

            return(new TransactionScope(transaction, () => _transactions.Pop()));
        }
コード例 #28
0
        /// <summary>
        /// 初始化 <see cref="DbContextBase"/> 类的新实例
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="commandTimeout">执行命令超时时间</param>
        /// </summary>
        public DbContextBase(string connString, int?commandTimeout)
        {
            XFrameworkException.Check.NotNull(connString, nameof(connString));

            _connString     = connString;
            _commandTimeout = commandTimeout;
            _isDebug        = false;
            _isolationLevel = null;
            _provider       = (DbQueryProvider)this.Provider;
        }
コード例 #29
0
        /// <summary>
        /// Creates a new <see cref="UnitOfWorkAttribute"/> object.
        /// </summary>
        /// <param name="scope">Transaction scope</param>
        /// <param name="isolationLevel">Transaction isolation level</param>
        /// <param name="isTransactional"/>
        /// <param name="timeout">Transaction  timeout as milliseconds</param>
        public UnitOfWorkAttribute(TransactionScopeOption scope, IsolationLevel?isolationLevel, bool isTransactional, int timeout)
        {
            Scope = scope;
            if (isolationLevel.HasValue)
            {
                this.IsolationLevel = isolationLevel;
            }

            IsTransactional = isTransactional;
            Timeout         = TimeSpan.FromMilliseconds(timeout);
        }
コード例 #30
0
ファイル: AdoExtension.cs プロジェクト: lulzzz/WCloud
 /// <summary>
 /// 开启事务
 /// </summary>
 /// <param name="con"></param>
 /// <param name="level"></param>
 /// <returns></returns>
 public static IDbTransaction StartTransaction(this IDbConnection con, IsolationLevel?level = null)
 {
     if (level == null)
     {
         return(con.BeginTransaction());
     }
     else
     {
         return(con.BeginTransaction(level.Value));
     }
 }
コード例 #31
0
        public SQLiteWriteOnlyTransaction(
            IDbConnection connection,
            IsolationLevel? isolationLevel,
            PersistentJobQueueProviderCollection queueProviders)
        {
            if (connection == null) throw new ArgumentNullException("connection");
            if (queueProviders == null) throw new ArgumentNullException("queueProviders");

            _connection = connection;
            _isolationLevel = isolationLevel;
            _queueProviders = queueProviders;
        }
コード例 #32
0
		public SQLiteConnection(
			IDbConnection connection,
			IsolationLevel? isolationLevel,
			PersistentJobQueueProviderCollection queueProviders,
			bool ownsConnection)
		{
			if (connection == null) throw new ArgumentNullException("connection");
			if (queueProviders == null) throw new ArgumentNullException("queueProviders");

			_connection = connection;
			_isolationLevel = isolationLevel;
			_queueProviders = queueProviders;

			OwnsConnection = ownsConnection;
		}
コード例 #33
0
 /// <summary>
 /// Constructor. Sets DBMS type and connection string.
 /// </summary>
 /// <param name="dbmsType">DBMS type to which this provider connects to. Only Oracle compatibile values are allowed.</param>
 /// <param name="connectionString">Connection string.</param>
 /// <param name="defaultTransactionIsolationLevel">Isolation level for transactions started by the parameterless <see cref="BeginTransaction()"/> method overload.</param>
 public OracleConnectionProvider(DbmsType dbmsType, string connectionString,
     IsolationLevel defaultTransactionIsolationLevel)
     : this(dbmsType, connectionString)
 {
     this.defaultTransactionIsolationLevel = defaultTransactionIsolationLevel;
 }
コード例 #34
0
        /// <summary>
        /// Creates a new instance of <see cref="DatabaseUploadStreamProviderBase" /> class with the specified settings.
        /// </summary>
        /// <param name="settings">The <see cref="UploadStreamProviderElement" /> object that holds the configuration settings.</param>
        protected DatabaseUploadStreamProviderBase(UploadStreamProviderElement settings)
            : base(settings)
        {
            string connectionStringName = Settings.Parameters["connectionStringName"];

            if (!string.IsNullOrEmpty(connectionStringName))
                _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            else
                _connectionString = Settings.Parameters["connectionString"];

            if (string.IsNullOrEmpty(_connectionString))
                throw new Exception("uploadStreamProvider connectionString or connectionStringName attribute must be specified.");
                
            _table = Settings.Parameters["table"];
            _keyField = Settings.Parameters["keyField"];
            _dataField = Settings.Parameters["dataField"];
            _fileNameField = Settings.Parameters["fileNameField"];

            if (string.IsNullOrEmpty(_table))
                throw new Exception("uploadStreamProvider table attribute must be specified.");
            if (string.IsNullOrEmpty(_keyField))
                throw new Exception("uploadStreamProvider keyField attribute must be specified.");
            if (string.IsNullOrEmpty(_dataField))
                throw new Exception("uploadStreamProvider dataField attribute must be specified.");

            // TODO: throw exception for missing parameters

            bool.TryParse(Settings.Parameters["useInsertTransaction"], out _useInsertTransaction);

            string isolationLevelString = Settings.Parameters["useInsertTransaction"];

            if (!string.IsNullOrEmpty(isolationLevelString))
                _isolationLevel = (IsolationLevel)Enum.Parse(typeof(IsolationLevel), isolationLevelString, true);
        }
コード例 #35
0
 public IDbTransaction BeginTransaction(IsolationLevel il)
 {
     transaction = new ReplicationTransaction(this, il);
     this.il = il;
     return transaction;
 }