public static DatabaseConnectionWrapper GetConnection(Database db) { Transaction currentTransaction = Transaction.Current; if (currentTransaction == null) { return(null); } Dictionary <string, DatabaseConnectionWrapper> connectionList; DatabaseConnectionWrapper connection; lock (transactionConnections) { if (!transactionConnections.TryGetValue(currentTransaction, out connectionList)) { connectionList = new Dictionary <string, DatabaseConnectionWrapper>(); transactionConnections.Add(currentTransaction, connectionList); currentTransaction.TransactionCompleted += OnTransactionCompleted; } } lock (connectionList) { if (!connectionList.TryGetValue(db.ConnectionString, out connection)) { var dbConnection = db.GetNewConnection(); connection = new DatabaseConnectionWrapper(dbConnection); connectionList.Add(db.ConnectionString, connection); } connection.AddRef(); } return(connection); }
public static DatabaseConnectionWrapper GetConnection(Database db) { Transaction currentTransaction = Transaction.Current; if (currentTransaction == null) return null; Dictionary<string, DatabaseConnectionWrapper> connectionList; DatabaseConnectionWrapper connection; lock (transactionConnections) { if (!transactionConnections.TryGetValue(currentTransaction, out connectionList)) { connectionList = new Dictionary<string, DatabaseConnectionWrapper>(); transactionConnections.Add(currentTransaction, connectionList); currentTransaction.TransactionCompleted += OnTransactionCompleted; } } lock (connectionList) { if (!connectionList.TryGetValue(db.ConnectionString, out connection)) { var dbConnection = db.GetNewConnection(); connection = new DatabaseConnectionWrapper(dbConnection); connectionList.Add(db.ConnectionString, connection); } connection.AddRef(); } return connection; }
public virtual IDataReader ExecuteReader(DbCommand command) { if (command == null) { throw new ArgumentNullException("command"); } using (DatabaseConnectionWrapper wrapper = GetOpenConnection()) { PrepareCommand(command, wrapper.DbConnection); IDataReader reader = DoExecuteReader(command, CommandBehavior.Default); return(CreateWrapperReader(wrapper, reader)); } }
public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader) : base(innerReader) { connectionWrapper = connection; connectionWrapper.AddRef(); }
protected virtual IDataReader CreateWrapperReader(DatabaseConnectionWrapper wrapper, IDataReader innerReader) { return new RefCountingDataReader(wrapper, innerReader); }
protected DatabaseConnectionWrapper GetOpenConnection() { DatabaseConnectionWrapper con = TransactionScopeConnections.GetConnection(this); return(con ?? GetWrappedConnection()); }
protected virtual IDataReader CreateWrapperReader(DatabaseConnectionWrapper wrapper, IDataReader innerReader) { return(new RefCountingDataReader(wrapper, innerReader)); }