/// <summary> /// Gets the new max key. /// </summary> /// <param name="transactionScopeConnection">The transaction scope connection.</param> /// <param name="tableName">Name of the table.</param> /// <param name="columnName">Name of the column.</param> /// <returns></returns> public override int GetNewMaxKey(System.Data.IDbConnection transactionScopeConnection, string tableName, string columnName) { // this will join in to any root transaction TransactionOptions serializableTransaction = new TransactionOptions(); serializableTransaction.IsolationLevel = System.Transactions.IsolationLevel.Serializable; using (DbTransactionScope scope = new DbTransactionScope(TransactionScopeOption.Required, serializableTransaction)) { if (transactionScopeConnection == null) { using (new DbConnectionScope()) { int newMaxKey = GetNewMaxKey(DbConnectionScope.Current.Connection, tableName, columnName); scope.Complete(); return(newMaxKey); } } else { using (IDataReader reader = ExecuteQueryReader(string.Format("select max({0})+1 from {1}", columnName, tableName), transactionScopeConnection)) { int ret = 1; if (reader.Read() && !reader.IsDBNull(0)) { ret = Database.Current.GetInt32(reader, 0); } scope.Complete(); return(ret); } } } }
/// <summary> /// Ares the transactions nested. /// </summary> /// <returns></returns> public static bool AreTransactionsNested() { if (Log.Enabled) { Log.Entry("AreTransactionsNested", s_scopes); } bool result = false; if (s_scopes != null && s_scopes.Count > 1) { int uniqueTransactionScopes = 1; int l = s_scopes.Count; if (Log.Enabled) { Log.LogDebug10("Transaction scope count = {0}", l); } DbTransactionScope[] ar = new DbTransactionScope[l]; s_scopes.CopyTo(ar, 0); DbTransactionScope x; int j; for (int i = 1; i < l; i++) { x = ar[i]; for (j = 0; j < l; j++) { if (i != j && !x.Transaction.Equals(ar[j].Transaction)) { break; } } if (j < l) { uniqueTransactionScopes++; } } if (Log.Enabled) { Log.LogDebug10("Unique transaction scopes = {0}", uniqueTransactionScopes); } result = uniqueTransactionScopes > 1; } if (Log.Enabled) { Log.Exit("AreTransactionsNested", result); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="DbConnectionScope"/> class. /// If <paramref name="inheritConnections"/> is false, a new Database connection /// will be used in this scope. /// </summary> /// <param name="inheritConnections">if set to <c>true</c> [inherit connections].</param> public DbConnectionScope(bool inheritConnections) { if (Log.Enabled) { Log.Entry("DbConnectionScope", inheritConnections); } // If the database doesn't support MARS, and there is no transaction or a single transaction, // then we will open a new connection each time if (!Database.Current.SupportsFeature(DatabaseFeature.MultipleActiveResultSets)) { if (Log.Enabled) { Log.LogDebug10("Database feature {0} not supported", DatabaseFeature.MultipleActiveResultSets); } if (Transaction.Current == null) { if (Log.Enabled) { Log.LogDebug10("Transaction.Current is null"); } inheritConnections = false; } else if (Database.Current.SupportsFeature(DatabaseFeature.MultipleOpenConnectionsWithinSingleTransaction) && DbTransactionScope.IsInUse) { if (Log.Enabled) { Log.LogDebug10("DbTransactionScope is inuse"); } // If this thread is using DbTransactionScope, then we check to see // if there is only a single transaction or if they are nested if (DbTransactionScope.AreTransactionsNested()) { inheritConnections = false; } } } m_inheritConnections = inheritConnections; if (Log.Enabled) { Log.LogDebug10("InheritConnections = {0}", m_inheritConnections); } if (scopes == null) { if (Log.Enabled) { Log.LogDebug10("Scopes is null, thread {0}", Thread.CurrentThread); } scopes = new Stack <DbConnectionScope>(); } scopes.Push(this); if (Log.Enabled) { Log.Exit("DbConnectionScope"); } }