Example #1
0
        /// <summary>
        /// create a new managed connection.
        /// </summary>
        /// <param name="connection">connection</param>
        /// <param name="attemptToPool">true if we are going to try and reuse the
        /// connection if possible</param>
        public ManagedConnection(SqlOlapConnectionInfoBase sourceConnection, bool attemptToPool)
        {
            // parameter check
            if (sourceConnection == null)
            {
                throw new ArgumentNullException("sourceConnection");
            }

            // see if the connection can restrict access (single user mode)
            IRestrictedAccess access = sourceConnection as IRestrictedAccess;
            // see if it is cloneable
            ICloneable cloneable = sourceConnection as ICloneable;

            lock (ActiveConnections)
            {
                // if it's not single user mode then we can see if the object can be cloned
                if (access == null || access.SingleConnection == false)
                {
                    // if we are going to attempt to pool, see if the connection is in use
                    if (attemptToPool && !ActiveConnections.Contains(SharedConnectionUtil.GetConnectionKeyName(sourceConnection)))
                    {
                        // add it to the hashtable to indicate use.
                        ActiveConnections.Add(SharedConnectionUtil.GetConnectionKeyName(sourceConnection), sourceConnection);
                        this.connection     = sourceConnection;
                        this.closeOnDispose = false;
                        this.connectionAddedToActiveConnections = true;
                    }
                    else if (cloneable != null)
                    {
                        this.connection     = (SqlOlapConnectionInfoBase)cloneable.Clone();
                        this.closeOnDispose = true;
                    }
                    else if (sourceConnection is SqlConnectionInfoWithConnection)
                    {
                        this.connection     = ((SqlConnectionInfoWithConnection)sourceConnection).Copy();
                        this.closeOnDispose = true;
                    }
                }
            }
            // if everything else has failed just use to passed in connection.
            if (this.connection == null)
            {
                this.connection = sourceConnection;
            }

            // always set the lock timeout to prevent the shell from not responding
            if (this.connection is SqlConnectionInfoWithConnection)
            {
                // set lock_timeout to 10 seconds
                ((SqlConnectionInfoWithConnection)this.connection).ServerConnection.LockTimeout = 10;
            }
        }
Example #2
0
        /// <summary>
        /// Close the current connection if applicable.
        /// </summary>
        public void Close()
        {
            if (this.closed)
            {
                return;
            }

            if (this.closeOnDispose)
            {
                IDisposable disp = this.connection as IDisposable;
                if (disp != null)
                {
                    disp.Dispose();
                }
            }
            else
            {
                // if we are not closing the connection and it is a sql connection then ensure it
                // is left in the master database.
                SqlConnectionInfoWithConnection sqlConnection = this.connection as SqlConnectionInfoWithConnection;
                if (sqlConnection != null && sqlConnection.ServerConnection.DatabaseEngineType == DatabaseEngineType.Standalone)
                {
                    try
                    {
                        sqlConnection.ServerConnection.ExecuteNonQuery("use [master]");
                    }
                    // don't error if this fails
                    catch
                    { }
                }
            }
            if (this.connectionAddedToActiveConnections)
            {
                lock (ActiveConnections)
                {
                    ActiveConnections.Remove(SharedConnectionUtil.GetConnectionKeyName(connection));
                }
            }

            this.connection = null;
            this.closed     = true;
        }