poolitem.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,127 行 · 第 1/2 页
JAVA
1,127 行
/** * Notifies that a local transaction has rolled back. */ public void localTransactionRolledback(ConnectionEvent event) { if (_xid != null) throw new IllegalStateException(L.l("attempted to rollback() local transaction from an active XA transaction.")); else if (! _isLocalTransaction) throw new IllegalStateException(L.l("attempted to rollback() with no active local transaction.")); // #2627, server/30c4 _isLocalTransaction = false; } /** * Notifies that a connection error has occurred. */ public void connectionErrorOccurred(ConnectionEvent event) { _hasConnectionError = true; } /** * Notifies that a connection error has occurred. */ public void setConnectionError() { _hasConnectionError = true; } /** * Returns true if there was a connection error. */ public boolean isConnectionError() { return _hasConnectionError; } /** * Returns the allocation stack trace. */ public IllegalStateException getAllocationStackTrace() { return _allocationStackTrace; } /** * Returns true if there is a connection error. */ // XAResource stuff /** * identity of resources */ public boolean isSameRM(XAResource resource) throws XAException { if (! (resource instanceof PoolItem)) return false; PoolItem poolItem = (PoolItem) resource; //if (_cm == poolItem._cm) // return true; if (_xaResource == null) return false; boolean isSameRM = _xaResource.isSameRM(poolItem._xaResource); if (log.isLoggable(Level.FINER)) log.finer("isSameRM->" + isSameRM + " " + _xaResource); return isSameRM; } /** * starts work on a transaction branch */ public void start(Xid xid, int flags) throws XAException { if (_xid != null) { if (log.isLoggable(Level.FINER)) log.finer("connection pool start XA: rejoin " + this); return; } if (flags == TMJOIN && _xid == null) { // TMJOIN means the resource manager is managing more than one // connection. The delegates tie the PoolItems managed by // the same resource manager together. _xid = xid; UserTransactionImpl trans = _cm.getTransaction(); if (trans != null) { PoolItem xaHead = trans.findJoin(this); if (xaHead != null) { _xaNext = xaHead._xaNext; _xaHead = xaHead; xaHead._xaNext = this; } } /* XXX: is this still an issue? if (_xaDelegate != this) throw new IllegalStateException("pool state exception"); PoolItem delegate = _cm.getDelegatePoolItem(xid); // set to the delegate _xaDelegate = delegate._xaDelegate; // single link list of parents _xaDelegateNext = _xaDelegate._xaDelegateNext; _xaDelegate._xaDelegateNext = this; */ /* if (log.isLoggable(Level.FINER)) log.finer("start XA: using delegate " + _xaDelegate + " for XID " + xid); return; */ } // local transaction optimization if (! _isXATransaction && flags != TMJOIN && _localTransaction != null) { // XXX: server/1810, etc // && _xaResource == null) { // XXX: temp disable for ActiveMQ try { if (log.isLoggable(Level.FINER)) log.finer("begin-local-XA: " + xid + " " + _localTransaction); _localTransaction.begin(); } catch (ResourceException e) { throw new XAExceptionWrapper(e); } _xid = xid; return; } if (_xaResource != null) { if (log.isLoggable(Level.FINER)) log.finer("start-XA: " + xid + " " + _xaResource); _xaResource.start(xid, flags); _isXATransaction = true; } else { if (log.isLoggable(Level.FINER)) log.finer("start-XA with non XA resource: " + xid + " " + _xaResource); } _xid = xid; } /** * Sets the transaction timeout */ public boolean setTransactionTimeout(int seconds) throws XAException { if (seconds == _transactionTimeout) return true; XAResource xaResource = _xaResource; _transactionTimeout = seconds; if (xaResource == null) return true; else if (seconds == 0) return xaResource.setTransactionTimeout(_defaultTransactionTimeout); else return xaResource.setTransactionTimeout(seconds); } /** * Returns the timeout of the underlying resource. */ public int getTransactionTimeout() throws XAException { return _transactionTimeout; } /** * forget about the transaction */ public void forget(Xid xid) throws XAException { try { if (_isXATransaction) _xaResource.forget(xid); } finally { clearXid(); } } /** * Vote using phase-1 of the 2-phase commit. */ public int prepare(Xid xid) throws XAException { if (_endFlags != -1) { int endFlags = _endFlags; _endFlags = -1; if (_isXATransaction) endResource(xid, endFlags); } if (_isXATransaction) { try { if (log.isLoggable(Level.FINER)) log.finer("prepare-XA: " + xid + " " + _xaResource); int result = _xaResource.prepare(xid); if (result == XA_RDONLY) { if (_xaResource != null) _isXATransaction = true; clearXid(); } return result; } catch (XAException e) { if (log.isLoggable(Level.FINER)) log.finer("failed prepare-XA: " + xid + " " + _xaResource + " " + e); throw e; } } else return XA_OK; } /** * recover the transaction */ public Xid[]recover(int flag) throws XAException { if (_isXATransaction) return _xaResource.recover(flag); else return null; } /** * Ends work with the resource. Called before commit/rollback. */ public void end(Xid xid, int flags) throws XAException { /* XXX: if (_xid == null) throw new IllegalStateException("ending with no transaction"); */ //if (log.isLoggable(Level.FINER)) // log.finer("connection pool end XA: " + this + " xa=" + xid + " flags=" + flags); _endFlags = flags; // XXX: In theory, drop the _xid. The underlying XADataSource // can handle combining the connections itself. // Don't call the underlying _xaResource.end. The commit or rollback // will handle that automatically. } /** * rollback the resource */ public void rollback(Xid xid) throws XAException { try { if (_endFlags != -1) { try { int endFlags = _endFlags; _endFlags = -1; if (_isXATransaction) endResource(xid, endFlags); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); if (_isXATransaction) _xaResource.rollback(xid); return; } } if (log.isLoggable(Level.FINER)) log.finer("connection pool rollback XA: " + this); if (_isXATransaction) _xaResource.rollback(xid); else if (_localTransaction != null) { try { _isLocalTransaction = false; _localTransaction.rollback(); } catch (ResourceException e) { throw new XAExceptionWrapper(e); } } } finally { if (_xaResource != null) _isXATransaction = true; clearXid(); } } /** * commit the resource */ public void commit(Xid xid, boolean onePhase) throws XAException { boolean logFiner = log.isLoggable(Level.FINER); try { if (_endFlags != -1) { try { int endFlags = _endFlags; _endFlags = -1; if (_isXATransaction) endResource(xid, endFlags); } catch (XAException e) { log.log(Level.WARNING, e.toString(), e); _xaResource.rollback(xid); throw e; } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); _xaResource.rollback(xid); throw new XAException(XAException.XA_RBOTHER); } } if (_isXATransaction) { if (logFiner) { log.finer("commit-XA" + (onePhase ? "-1p: " : ": ") + xid + " " + _xaResource); } try { _xaResource.commit(xid, onePhase); } catch (XAException e) { if (logFiner) log.finer("commit-XA failed: " + _xaResource + " " + e); throw e; } } else if (_localTransaction != null) { if (logFiner) log.finer("commit-local: " + _localTransaction); try { _isLocalTransaction = false; _localTransaction.commit(); } catch (ResourceException e) { if (logFiner) log.finer("commit failed: " + _localTransaction + " " + e); throw new XAExceptionWrapper(e); } } else { if (logFiner) log.finer("commit for resource with no XA support: " + this); } } finally { if (_xaResource != null) _isXATransaction = true; clearXid(); } } /** * Ends the resource. */ private void endResource(Xid xid, int flags) throws XAException { PoolItem xaPtr = this; for (; xaPtr != null; xaPtr = xaPtr._xaNext) { if (xaPtr._xaResource != null) xaPtr._xaResource.end(xid, flags); } } /** * Restores the delegation for the entire chain. */ private void clearXid() { _xid = null; UserPoolItem shareHead = _shareHead; // _shareHead is nullified at end for timing reasons PoolItem xaPtr = _xaNext; _xaHead = null; _xaNext = null; boolean isClosed = true; UserPoolItem ptr = shareHead; while (ptr != null) { UserPoolItem next = ptr.getShareNext(); if (ptr.getOwnPoolItem() == this) isClosed = false; try { ptr.reassociatePoolItem(); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } ptr = next; } while (xaPtr != null) { PoolItem next = xaPtr._xaNext; xaPtr._xaNext = null; xaPtr._xaHead = null; xaPtr.clearXid(); xaPtr = next; } if (! isClosed) { } else if (_hasConnectionError) { toDead(); } else { toIdle(); } } /** * Changes the state to idle. */ void toIdle() { if (_shareHead != null) return; else if (_xid != null || _isLocalTransaction) return; else if (_hasConnectionError) { toDead(); return; } UserTransactionImpl transaction = _transaction; _transaction = null; if (transaction != null) { try { transaction.delistPoolItem(this, XAResource.TMSUCCESS); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); } } _isLocalTransaction = false; if (log.isLoggable(Level.FINE)) log.fine("idle " + this); _poolEventTime = Alarm.getCurrentTime(); _cm.toIdle(this); } /** * Closes the connection. */ void abortConnection() { toDead(); } /** * Kills the connection. */ private void toDead() { _cm.toDead(this); } /** * Closes the connection. */ void destroy() throws ResourceException { ManagedConnection mConn = _mConn; _mConn = null; UserTransactionImpl transaction = _transaction; _transaction = null; if (mConn == null) return; UserPoolItem userItem = _shareHead; if (log.isLoggable(Level.FINE)) log.fine("connection pool destroy " + this); try { while (userItem != null) { UserPoolItem next = userItem.getShareNext(); userItem.close(); userItem = next; } if (transaction != null) transaction.delistPoolItem(this, XAResource.TMFAIL); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); } mConn.destroy(); } public String toString() { if (_mConn != null) { return ("PoolItem[" + _cm.getName() + "," + _id + "," + _mConn.getClass().getSimpleName() + "]"); } else { return ("PoolItem[" + _cm.getName() + "," + _id + ",null]"); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?