1 23 package com.sun.enterprise.resource; 24 25 import java.util.*; 26 27 import javax.resource.ResourceException ; 28 import javax.resource.spi.ManagedConnection ; 29 import javax.transaction.*; 30 import java.util.logging.*; 31 import com.sun.logging.*; 32 33 import javax.naming.Context ; 34 import javax.naming.NamingException ; 35 import com.sun.enterprise.Switch; 36 import com.sun.enterprise.connectors.*; 37 import com.sun.enterprise.distributedtx.*; 38 import com.sun.enterprise.util.i18n.StringManager; 39 40 41 42 49 public class AssocWithThreadResourcePool extends AbstractResourcePool { 50 51 private static ThreadLocal <ResourceHandle> localResource = 52 new ThreadLocal <ResourceHandle>(); 53 54 public AssocWithThreadResourcePool( String poolName ) 55 throws PoolingException 56 { 57 super( poolName ); 58 } 59 60 protected ResourceHandle prefetch( ResourceSpec spec, 61 ResourceAllocator alloc, Transaction tran) 62 { 63 ResourceHandle ar = localResource.get(); 64 if (ar != null) { 65 synchronized( ar.lock ) { 68 if ( (ar.getThreadId() != Thread.currentThread().getId()) || 69 ar.hasConnectionErrorOccurred() || 70 ar.isDirty() || !ar.isAssociated() ) { 71 78 localResource.remove(); 79 return null; 80 } 81 82 if (ar.getResourceState().isFree() && 83 ar.getResourceState().isUnenlisted()) { 84 if (matchConnections) { 85 if (! alloc.matchConnection( ar ) ) { 86 localResource.remove(); 91 ar.setAssociated( false ); 92 if ( monitoringEnabled ) { 93 poolCounters.incrementNumConnNotSuccessfullyMatched(); 94 } 95 return null; 96 } 97 if (monitoringEnabled) { 98 poolCounters.incrementNumConnSuccessfullyMatched(); 99 } 100 } 101 102 103 ar.getResourceState().setBusy( true ); 104 105 return ar; 106 } 107 } 108 } 109 110 return null; 111 } 112 113 private void setInThreadLocal( ResourceHandle h ) { 114 if ( h != null ) { 115 h.setThreadId( Thread.currentThread().getId() ); 116 h.setAssociated( true ); 117 localResource.set( h ); 118 } 119 120 } 121 122 protected boolean isResourceUnused(ResourceHandle h){ 123 return h.getResourceState().isFree() && !h.isAssociated() ; 124 } 125 126 127 132 synchronized protected ResourceHandle getUnenlistedResource(ResourceSpec spec, 133 ResourceAllocator alloc, 134 Transaction tran) throws PoolingException { 135 136 ResourceHandle result = null; 137 result = super.getUnenlistedResource( spec, alloc, tran ); 138 139 146 if ( result == null ) { 148 Iterator tempIter = resources.iterator(); 149 while( tempIter.hasNext() ) { 150 ResourceHandle h = (ResourceHandle) tempIter.next(); 151 synchronized( h.lock ) { 152 158 if (h.getResourceState().isUnenlisted() && 159 h.getResourceState().isFree()) { 160 if (matchConnections) { 161 if (! alloc.matchConnection(h) ) { 162 if (monitoringEnabled) { 163 poolCounters.incrementNumConnNotSuccessfullyMatched(); 164 } 165 continue; 166 } 167 if ( monitoringEnabled ) { 168 poolCounters.incrementNumConnSuccessfullyMatched(); 169 } 170 } 171 172 if ( h.hasConnectionErrorOccurred() ) { 173 continue; 174 } 175 result = h; 176 result.getResourceState().setBusy( true ); 177 result.setAssociated( false ); 178 break; 179 } 180 } 181 } 182 } 183 184 if ( localResource.get() == null ) { 185 setInThreadLocal( result ); 186 } 187 return result; 188 189 } 190 191 protected synchronized void freeUnenlistedResource(ResourceHandle h) { 192 if ( ! h.isAssociated() ) { 193 free.add( h ); 194 } 195 notifyWaitingThreads(); 196 } 197 198 protected void destroyResource(ResourceHandle resourceHandle) { 199 try { 200 super.destroyResource( resourceHandle ); 201 } finally { 202 210 synchronized( resourceHandle.lock ) { 211 resourceHandle.setDirty(); 212 } 213 } 214 } 215 216 } 217 | Popular Tags |