Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="providerName"></param>
        /// <param name="threadLocal"></param>
        /// <returns>A new connection object if one was created, otherwise null if the specified connection was already open.</returns>
        public static PersistenceConnection Connect(string providerName, string connectionName = null, bool threadLocal = true)
        {
            PersistenceProvider provider = GetOrThrow(providerName);

            // Get the requested connection lists
            PersistenceConnectionManager connections;

            if (!threadLocal)
            {
                connections = _globalConnections;
            }
            else
            {
                if (_localConnections == null)
                {
                    _localConnections = new PersistenceConnectionManager(true);
                }

                connections = _localConnections;
            }

            PersistenceConnection conn = connections.SwitchTo(provider, connectionName);

            // If it's not open, open it and return it, otherwise return null
            if (!conn.IsOpen)
            {
                conn.Open();
                return(conn);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="providerName"></param>
        public static void ForceClose(string providerName, string connectionName = null)
        {
            PersistenceProvider provider = GetOrThrow(providerName);

            PersistenceConnection conn = null;

            if (_localConnections != null)
            {
                conn = _localConnections[providerName, connectionName];
            }
            if (conn == null)
            {
                conn = _globalConnections[providerName, connectionName];
            }

            // Close and remove connection
            if (conn != null)
            {
                if (conn.IsOpen)
                {
                    Close(conn);
                }

                _localConnections.Remove(conn);
            }
        }
        public static PersistenceConnection Open(string providerName)
        {
            // EXCEPTION:
            PersistenceProvider provider;

            if (!_registeredProviders.TryGetValue(providerName, out provider))
            {
                throw new InvalidOperationException(String.Format("There is no provider registered under the name \"{0}\".", providerName));
            }

            List <PersistenceConnection> connections;
            int currentIndex = -1;

            // Try to locate the index of the connection
            if (_activeConnections.TryGetValue(Thread.CurrentThread, out connections) && connections != null && connections.Count > 0)
            {
                for (int i = 0; i < connections.Count; i++)
                {
                    if (connections[i].Provider == provider)
                    {
                        currentIndex = i;
                        break;
                    }
                }
            }

            PersistenceConnection conn;

            if (currentIndex >= 0)
            {
                // Remove from this position, we're going to add it to the end of the list
                conn = connections[currentIndex];
                connections.RemoveAt(currentIndex);
            }
            else
            {
                if (connections == null)
                {
                    connections = new List <PersistenceConnection>();
                    _activeConnections.Add(Thread.CurrentThread, connections);
                }

                conn = new PersistenceConnection(provider);
            }

            // Add the connection to the end of the list
            connections.Add(conn);

            // If it's not open, open it and return it, otherwise return null
            if (!provider.IsOpen)
            {
                provider.Open();
                return(conn);
            }
            else
            {
                return(null);
            }
        }
Пример #4
0
        /*=========================*/
        #endregion

        #region Methods
        /*=========================*/

        /// <summary>
        ///
        /// </summary>
        /// <param name="threadLocal"></param>
        /// <returns></returns>
        internal PersistenceConnection NewConnection(string connectionName, bool threadLocal)
        {
            PersistenceConnection conn = CreateNewConnection();

            conn.IsThreadLocal = threadLocal;
            conn.Name          = connectionName;
            return(conn);
        }
Пример #5
0
        public int IndexOf(string providerName, string connectionName)
        {
            int index;

            if (!_indexes.TryGetValue(PersistenceConnection.FormatConnectionName(providerName, connectionName), out index))
            {
                index = -1;
            }

            return(index);
        }
Пример #6
0
 public void Remove(PersistenceConnection connection)
 {
     lock (_connections)
     {
         lock (_indexes)
         {
             _connections.Remove(connection);
             _indexes.Remove(connection.Provider.Name);
         }
     }
 }
Пример #7
0
        private void SwitchTo(PersistenceConnection connection)
        {
            lock (_connections)
            {
                lock (_indexes)
                {
                    if (_connections.Contains(connection))
                    {
                        _connections.Remove(connection);
                    }

                    _connections.Insert(0, connection);
                    _indexes[connection.FullName] = 0;
                }
            }
        }
Пример #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connection"></param>
        public static void Close(PersistenceConnection connection)
        {
            if (connection == null)
            {
                return;
            }

            // Tell the provider to close this connection
            connection.Close();

            if (connection.IsThreadLocal)
            {
                _localConnections.Remove(connection);
            }
            else
            {
                _globalConnections.Remove(connection);
            }
        }
Пример #9
0
        public PersistenceConnection SwitchTo(PersistenceProvider provider, string connectionName)
        {
            int index = IndexOf(provider.Name, connectionName);

            // Connection is already at top of stack
            //if (index == 0)
            //	return;

            // Either reuse an existing or create a new connection
            PersistenceConnection connection = index < 0 ?
                                               connection = provider.NewConnection(connectionName, _threadLocal) :
                                                            connection = _connections[index];

            if (index != 0)
            {
                SwitchTo(connection);
            }

            return(connection);
        }
        public static void ForceClose(string providerName)
        {
            PersistenceProvider provider;

            if (!_registeredProviders.TryGetValue(providerName, out provider))
            {
                return;
            }

            List <PersistenceConnection> connections;
            int currentIndex = -1;

            // Try to locate the index of the connection
            if (_activeConnections.TryGetValue(Thread.CurrentThread, out connections) && connections != null && connections.Count > 0)
            {
                for (int i = 0; i < connections.Count; i++)
                {
                    if (connections[i].Provider == provider)
                    {
                        currentIndex = i;
                        break;
                    }
                }
            }

            if (currentIndex < -1)
            {
                return;
            }

            PersistenceConnection conn = connections[currentIndex];

            if (conn.IsOpen)
            {
                Close(conn);
            }

            connections.RemoveAt(currentIndex);
        }
 public static void Close(PersistenceConnection connection)
 {
     throw new NotImplementedException();
 }
Пример #12
0
 public int IndexOf(PersistenceConnection connection)
 {
     return(_connections.IndexOf(connection));
 }