Example #1
0
        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;
        }
Example #3
0
 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));
     }
 }
Example #4
0
 public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
     : base(innerReader)
 {
     connectionWrapper = connection;
     connectionWrapper.AddRef();
 }
 public RefCountingDataReader(DatabaseConnectionWrapper connection, IDataReader innerReader)
     : base(innerReader)
 {
     connectionWrapper = connection;
     connectionWrapper.AddRef();
 }
Example #6
0
 protected virtual IDataReader CreateWrapperReader(DatabaseConnectionWrapper wrapper, IDataReader innerReader)
 {
     return new RefCountingDataReader(wrapper, innerReader);
 }
Example #7
0
        protected DatabaseConnectionWrapper GetOpenConnection()
        {
            DatabaseConnectionWrapper con = TransactionScopeConnections.GetConnection(this);

            return(con ?? GetWrappedConnection());
        }
Example #8
0
 protected virtual IDataReader CreateWrapperReader(DatabaseConnectionWrapper wrapper, IDataReader innerReader)
 {
     return(new RefCountingDataReader(wrapper, innerReader));
 }