1 29 30 package com.caucho.jca; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.resource.ResourceException ; 36 import javax.resource.spi.ConnectionEvent ; 37 import javax.resource.spi.ConnectionRequestInfo ; 38 import javax.resource.spi.ManagedConnectionFactory ; 39 import javax.security.auth.Subject ; 40 import javax.transaction.RollbackException ; 41 import java.util.logging.Logger ; 42 43 55 class UserPoolItem { 56 private static final L10N L = new L10N(UserPoolItem.class); 57 private static final Logger log = Log.open(UserPoolItem.class); 58 59 private ConnectionPool _cm; 60 private String _id; 61 62 private PoolItem _ownPoolItem; 64 65 private ManagedConnectionFactory _mcf; 66 private Subject _subject; 67 private ConnectionRequestInfo _info; 68 69 private UserTransactionImpl _transaction; 71 72 private PoolItem _sharePoolItem; 74 75 private UserPoolItem _shareNext; 77 private UserPoolItem _sharePrev; 79 80 private Object _userConn; 81 82 private boolean _hasConnectionError; 83 84 private IllegalStateException _allocationStackTrace; 85 86 public UserPoolItem(ConnectionPool cm) 87 { 88 _cm = cm; 89 _id = _cm.generateId(); 90 91 _transaction = _cm.getTransaction(); 92 93 if (cm.getSaveAllocationStackTrace()) { 94 _allocationStackTrace = new IllegalStateException (L.l("unclosed connection: " + this + " was allocated at")); 95 _allocationStackTrace.fillInStackTrace(); 96 } 97 } 98 99 public UserPoolItem(ConnectionPool cm, PoolItem poolItem) 100 { 101 this(cm); 102 103 _ownPoolItem = poolItem; 104 _sharePoolItem = poolItem; 105 } 106 107 110 public void setManagedConnectionFactory(ManagedConnectionFactory mcf) 111 { 112 _mcf = mcf; 113 } 114 115 118 public ManagedConnectionFactory getManagedConnectionFactory() 119 { 120 return _mcf; 121 } 122 123 126 public void setSubject(Subject subject) 127 { 128 _subject = subject; 129 } 130 131 134 public Subject getSubject() 135 { 136 return _subject; 137 } 138 139 142 public void setInfo(ConnectionRequestInfo info) 143 { 144 _info = info; 145 } 146 147 150 public ConnectionRequestInfo getInfo() 151 { 152 return _info; 153 } 154 155 158 public boolean isActive() 159 { 160 return _userConn != null; 161 } 162 163 166 public void connectionErrorOccurred(ConnectionEvent event) 167 { 168 _hasConnectionError = true; 169 } 170 171 174 public boolean isConnectionError() 175 { 176 return _hasConnectionError; 177 } 178 179 182 public IllegalStateException getAllocationStackTrace() 183 { 184 return _allocationStackTrace; 185 } 186 187 190 public void setSaveAllocationStackTrace(boolean enable) 191 { 192 _cm.setSaveAllocationStackTrace(enable); 193 } 194 195 198 public PoolItem getOwnPoolItem() 199 { 200 return _ownPoolItem; 201 } 202 203 206 public PoolItem getXAPoolItem() 207 { 208 return _sharePoolItem; 209 } 210 211 214 public Object getUserConnection() 215 { 216 return _userConn; 217 } 218 219 222 Object allocateUserConnection() 223 throws ResourceException 224 { 225 if (_userConn == null) 226 _userConn = _sharePoolItem.allocateConnection(); 227 228 return _userConn; 229 } 230 231 234 public void setUserConnection(Object userConn) 235 { 236 _userConn = userConn; 237 } 238 239 242 UserPoolItem getShareNext() 243 { 244 return _shareNext; 245 } 246 247 250 void associatePoolItem(PoolItem poolItem) 251 { 252 if (_ownPoolItem != null) 253 throw new IllegalStateException (L.l("associating with old pool item.")); 254 255 _ownPoolItem = poolItem; 256 257 if (_sharePoolItem != null) 258 removeFromShareList(); 259 260 addToShareList(poolItem); 261 } 262 263 266 void associate(PoolItem poolItem, 267 ManagedConnectionFactory mcf, 268 Subject subject, 269 ConnectionRequestInfo info) 270 { 271 if (_sharePoolItem != null) 272 removeFromShareList(); 273 274 _mcf = mcf; 275 _subject = subject; 276 _info = info; 277 278 addToShareList(poolItem); 279 280 if (_transaction != null) { 281 try { 282 _transaction.enlistResource(this); 283 } catch (RollbackException e) { 284 removeFromShareList(); 285 286 poolItem.toIdle(); 287 288 throw new RuntimeException (e); 289 } catch (Throwable e) { 290 removeFromShareList(); 291 292 poolItem.setConnectionError(); 293 poolItem.toIdle(); 294 295 throw new RuntimeException (e); 296 } 297 } 298 } 299 300 303 void reassociatePoolItem() 304 throws ResourceException 305 { 306 if (_ownPoolItem == null) { 307 UserPoolItem item = _cm.allocatePool(_mcf, _subject, _info, this); 308 309 assert(item == this); 310 311 _ownPoolItem = item.getOwnPoolItem(); 312 } 313 314 if (_sharePoolItem != null) 315 removeFromShareList(); 316 317 addToShareList(_ownPoolItem); 318 } 319 320 323 void abortConnection() 324 throws ResourceException 325 { 326 PoolItem poolItem = _ownPoolItem; 327 _ownPoolItem = null; 328 329 removeFromShareList(); 330 331 if (poolItem != null) 332 poolItem.abortConnection(); 333 } 334 335 338 void close() 339 { 340 PoolItem ownPoolItem = _ownPoolItem; 341 _ownPoolItem = null; 342 343 _userConn = null; 344 345 if (_transaction != null) 346 _transaction.delistResource(this); 347 348 removeFromShareList(); 349 350 if (ownPoolItem != null) 351 ownPoolItem.toIdle(); 352 } 353 354 357 void removeFromShareList() 358 { 359 PoolItem poolItem = _sharePoolItem; 360 _sharePoolItem = null; 361 362 if (poolItem == null) 363 return; 364 365 synchronized (poolItem._shareLock) { 366 UserPoolItem prev = _sharePrev; 367 UserPoolItem next = _shareNext; 368 369 _sharePrev = null; 370 _shareNext = null; 371 372 if (prev != null) 373 prev._shareNext = next; 374 else 375 poolItem._shareHead = next; 376 377 if (next != null) 378 next._sharePrev = prev; 379 } 380 } 381 382 385 void addToShareList(PoolItem poolItem) 386 { 387 if (_sharePoolItem != null) 388 throw new IllegalStateException (); 389 390 synchronized (poolItem._shareLock) { 391 _sharePoolItem = poolItem; 392 393 _sharePrev = null; 394 _shareNext = poolItem._shareHead; 395 396 if (poolItem._shareHead != null) 397 poolItem._shareHead._sharePrev = this; 398 399 poolItem._shareHead = this; 400 } 401 } 402 403 406 boolean isClosed() 407 { 408 return _sharePoolItem == null; 409 } 410 411 413 public String toString() 414 { 415 return "UserPoolItem[" + _cm.getName() + "," + _id + "]"; 416 } 417 } 418 | Popular Tags |