Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.resource.pool.resizer;
 
 
 import  javax.resource.ResourceException;
 import java.util.Set;
Resizer for Associate With Thread type pools to remove unusable connections and maintain steady pool size.

Author(s):
Shalini M
 
 public class AssocWithThreadPoolResizer extends Resizer {
 
     public AssocWithThreadPoolResizer(PoolInfo poolInfoDataStructure ds,
             PoolProperties ppResourceHandler handler,
             boolean preferValidateOverRecreate) {
         super(poolInfodspphandlerpreferValidateOverRecreate);
     }

    
Scale down pool by a size <= pool-resize-quantity

Parameters:
forced scale-down only when forced
scaleDownQuantity no. of resources to remove
 
     @Override
     protected void scaleDownPool(int scaleDownQuantityboolean forced) {
         if (.getResizeQuantity() > 0 && forced) {
 
             scaleDownQuantity = (scaleDownQuantity <=
                     (.getResourcesSize() - .getSteadyPoolSize())) ? scaleDownQuantity : 0;
 
             debug("Scaling down pool by quantity : " + scaleDownQuantity);
             Set<ResourceHandleresourcesToRemove = new HashSet<ResourceHandle>();
             try {
                 for (ResourceHandle h : .getAllResources()) {
                     if (scaleDownQuantity > 0) {
                         synchronized (h.lock) {
                             if (!h.isBusy()) {
                                 resourcesToRemove.add(h);
                                 ((AssocWithThreadResourceHandleh).setDirty();
                                 scaleDownQuantity--;
                             }
                         }
                     }
                 }
             } finally {
                 for (ResourceHandle resourceToRemove : resourcesToRemove) {
                     if (.getAllResources().contains(resourceToRemove)) {
                        .removeResource(resourceToRemove);
                    }
                }
            }
        }
    }

    
Get the free connections list from the pool, remove idle-timed-out resources and then invalid resources.

Returns:
int number of resources removed
    @Override
    protected int removeIdleAndInvalidResources() {
        int poolSizeBeforeRemoval = .getResourcesSize();
        int noOfResourcesRemoved = 0;
        // let's cache the current time since precision is not required here.
        long currentTime = System.currentTimeMillis();
        int validConnectionsCounter = 0;
        int idleConnKeptInSteadyCounter = 0;
        ResourceState state;
        Set<ResourceHandleresourcesToValidate = new HashSet<ResourceHandle>();
        Set<ResourceHandleresourcesToRemove = new HashSet<ResourceHandle>();
        try {
            //iterate through all the resources to find idle-time lapsed ones.
            for (ResourceHandle h : .getAllResources()) {
                synchronized (h.lock) {
                    state = h.getResourceState();
                    if (!state.isBusy()) {
                        if (currentTime - state.getTimestamp() < .getIdleTimeout()) {
                            //Should be added for validation.
                            if (state.isUnenlisted() && state.isFree()) {
                                if (((AssocWithThreadResourceHandleh).isAssociated()) {
                                    ((AssocWithThreadResourceHandleh).setAssociated(false);
                                    validConnectionsCounter++;
                                    resourcesToValidate.add(h);
                                }
                            }
                        } else {
                            boolean isResourceEligibleForRemoval =
                                    isResourceEligibleForRemoval(hvalidConnectionsCounter);
                            if (!isResourceEligibleForRemoval) {
                                //preferValidateOverrecreate true and connection is valid within SPS
                                validConnectionsCounter++;
                                idleConnKeptInSteadyCounter++;
                                debug("PreferValidateOverRecreate: Keeping idle resource "
                                        + h + " in the steady part of the free pool "
                                        + "as the RA reports it to be valid (" + validConnectionsCounter
                                        + " <= " + .getSteadyPoolSize() + ")");
                            } else {
                                //Add this to remove later
                                resourcesToRemove.add(h);
                                ((AssocWithThreadResourceHandleh).setDirty();
                            }
                        }
                    }
                }
            }
        } finally {
            for (ResourceHandle resourceToRemove : resourcesToRemove) {
                if (.getAllResources().contains(resourceToRemove)) {
                    .removeResource(resourceToRemove);
                }
            }
        }
        //remove invalid resources from the free (active) resources list.
        //Since the whole pool is not locked, it may happen that some of these 
        //resources may be given to applications.
        int noOfInvalidResources = removeInvalidResources(resourcesToValidate);
        //These statistic computations will work fine as long as resizer
        //locks the pool throughout its operations.
        if () {
            debug("Idle resources validated and kept in the steady pool for pool [ "
                    +  + " ] - " + idleConnKeptInSteadyCounter);
            debug("Number of Idle resources freed for pool [ " +  + " ] - "
                    + (resourcesToRemove.size()));
            debug("Number of Invalid resources removed for pool [ " +  + " ] - "
                    + noOfInvalidResources);
        } else {
            debug("Number of Idle resources freed for pool [ " +  + " ] - "
                    + resourcesToRemove.size());
            debug("Number of Invalid resources removed for pool [ " +  + " ] - "
                    + noOfInvalidResources);
        }
        noOfResourcesRemoved = poolSizeBeforeRemoval - .getResourcesSize();
        return noOfResourcesRemoved;
    }

    
Removes invalid resource handles in the pool while resizing the pool. Uses the Connector 1.5 spec 6.5.3.4 optional RA feature to obtain invalid ManagedConnections

Parameters:
freeConnectionsToValidate Set of free connections
    private int removeInvalidResources(Set<ResourceHandlefreeConnectionsToValidate) {
        int invalidConnectionsCount = 0;
        try {
            debug("Sending a set of free connections to RA, "
                    + "of size : " + freeConnectionsToValidate.size());
            try {
                for (ResourceHandle handle : freeConnectionsToValidate) {
                    if (handle != null) {
                        Set connectionsToTest = new HashSet();
                        connectionsToTest.add(handle.getResource());
                        Set invalidConnections = .getInvalidConnections(connectionsToTest);
                        if (invalidConnections != null && invalidConnections.size() > 0) {
                            invalidConnectionsCount = validateAndRemoveResource(handleinvalidConnections);
                        } else {
                            //valid resource
                        }
                    }
                }
            } finally {
                debug("No. of invalid connections received from RA : " + invalidConnectionsCount);
            }
        } catch (ResourceException re) {
            if (.isLoggable(.)) {
                .log(."ResourceException while trying to get invalid connections from MCF"re);
            }
        } catch (Exception e) {
            if(.isLoggable(.)) {
                .log(."Exception while trying to get invalid connections from MCF"e);
            }
        }
        return invalidConnectionsCount;
    }
New to GrepCode? Check out our FAQ X