Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 1997-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
  * 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;
 import  javax.transaction.Transaction;
Associates a resource with the thread. When the same thread is used again, it checks whether the resource associated with the thread can serve the request.

Aditya Gore, Jagadish Ramu
 public class AssocWithThreadResourcePool extends ConnectionPool {
             new ThreadLocal<AssocWithThreadResourceHandle>();
     public AssocWithThreadResourcePool(PoolInfo poolInfoHashtable env)
             throws PoolingException {
     protected void initializePoolDataStructure() throws PoolingException {
          = DataStructureFactory.getDataStructure(

Prefetch is called to check whether there there is a free resource is already associated with the thread Only when prefetch is unable to find a resource, normal routine (getUnenlistedResource) will happen.

spec ResourceSpec
alloc ResourceAllocator
tran Transaction
ResourceHandle resource associated with the thread, if any
     protected ResourceHandle prefetch(ResourceSpec spec,
                                       ResourceAllocator alloc, Transaction tran) {
         if (ar != null) {
             //synch on ar and do a quick-n-dirty check to see if the local
             //resource is usable at all
             synchronized (ar.lock) {
                 if ((ar.getThreadId() != Thread.currentThread().getId()) ||
                         ar.hasConnectionErrorOccurred() ||
                         ar.isDirty() || !ar.isAssociated()) {
                     //we were associated with someone else or resource error 
                    //occurred or resource was disassociated and used by some one else. So evict
                    //NOTE: We do not setAssociated to false here since someone
                    //else has associated this resource to themself. Also, if
                    //the eviction is because of a resourceError, the resource is
                    //not going to be used anyway.
                    return null;
                if (ar.getResourceState().isFree() &&
                        ar.getResourceState().isUnenlisted()) {
                    if () {
                        if (!alloc.matchConnection(ar)) {
                            //again, since the credentials of the caller don't match
                            //evict from ThreadLocal
                            //also, mark the resource as unassociated and make this resource
                            //potentially usable
                            if( != null){
                            return null;
                        if( != null){
                    if (!isConnectionValid(aralloc)) {
                        // disassociating the connection from the thread.
                        // validation failure will mark the connectionErrorOccurred flag
                        // and the connection will be removed whenever it is retrieved again
                        // from the pool.
                        return null;
                    if ( > 0) {
                    if( != null) {
                        //Decrement numConnFree
                    return ar;
        return null;
    protected Resizer initializeResizer() {
        return new AssocWithThreadPoolResizer(thisthis,

to associate a resource with the thread

h ResourceHandle
        if (h != null) {
            synchronized (h.lock) {

check whether the resource is unused

h ResourceHandle
boolean representing resource usefullness
    protected boolean isResourceUnused(ResourceHandle h) {
        return h.getResourceState().isFree() && !((AssocWithThreadResourceHandleh).isAssociated();
    // this is the RI getResource() with some modifications
return resource in free list. If none is found, returns null
                                                   ResourceAllocator alloc, Transaction tranthrows PoolingException {
        ResourceHandle result;
        result = super.getUnenlistedResource(specalloctran);
        //It is possible that Resizer might have marked the resource for recycle
        //and hence we should not use this resource.
        if(result != null) {
            synchronized(result.lock) {
                if(.getAllResources().contains(result) &&
                        ((AssocWithThreadResourceHandle)result).isDirty()) {
                    //Remove the resource and set to null
                    result = null;
        //If we came here, that's because free doesn't have anything
        //to offer us. This could be because:
        //1. All free resources are associated
        //2. There are no free resources
        //3. We cannot create anymore free resources
        //Handle case 1 here
        if (result == null) {
            synchronized (this) {
                for (ResourceHandle resource : .getAllResources()) {
                    synchronized (resource.lock) {
                        //though we are checking resources from within the free list,
                        //we could have a situation where the resource was free upto
                        //this point, put just before we entered the synchronized block,
                        //the resource "h" got used by the thread that was associating it
                        //so we need to check for isFree also
                        if (resource.getResourceState().isUnenlisted() &&
                                resource.getResourceState().isFree() && 
                                !(((AssocWithThreadResourceHandleresource).isDirty())) {
                            if (!matchConnection(resourcealloc)) {
                            if (resource.hasConnectionErrorOccurred()) {
                            result = resource;
        if (.get() == null) {
        return result;

return the resource back to pool only if it is not associated with the thread.

h ResourceHandle
    protected synchronized void freeUnenlistedResource(ResourceHandle h) {
        if (this.cleanupResource(h)) {
            if (h instanceof AssocWithThreadResourceHandle) {
                //Only when resource handle usage count is more than maxConnUsage
                if ( > 0 &&
                        h.getUsageCount() >= ) {
                } else {
                    if (!((AssocWithThreadResourceHandleh).isAssociated()) {
                    //update monitoring data
                    if ( != null) {
                //for both the cases of free.add and maxConUsageOperation, a free resource is added.
                // Hence notify waiting threads

destroys the resource

resourceHandle resource to be destroyed
    public void deleteResource(ResourceHandle resourceHandle) {
        try {
        } finally {
            //Note: here we are using the connectionErrorOccurred flag to indicate
            //that this resource is no longer usable. This flag would be checked while
            //getting from ThreadLocal
            //The main intention of marking this is to handle the case where 
            //failAllConnections happens
            //Note that setDirty only happens here - i.e during destroying of a 
            synchronized (resourceHandle.lock) {
New to GrepCode? Check out our FAQ X