/** * This will be called if the Connection returned by the getConnection method came from a * PooledConnection, and the user calls the close() method of this connection object. What we need * to do here is to release this PooledConnection from our pool... */ public void connectionClosed(ConnectionEvent event) { PooledConnection pc = (PooledConnection) event.getSource(); // if this event occurred because we were validating, or if this // connection has been marked for removal, ignore it // otherwise return the connection to the pool. if (!muteMap.containsKey(pc)) { PooledConnectionAndInfo info = (PooledConnectionAndInfo) pcMap.get(pc); if (info == null) { throw new IllegalStateException(NO_KEY_MESSAGE); } try { _pool.returnObject(info.getUserPassKey(), info); } catch (Exception e) { System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL"); cleanupMap.put(pc, null); // mark for cleanup muteMap.put(pc, null); // ignore events until listener is removed try { _pool.invalidateObject(info.getUserPassKey(), info); } catch (Exception e3) { System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info); e3.printStackTrace(); } } } }
/** * If a fatal error occurs, close the underlying physical connection so as not to be returned in * the future */ public void connectionErrorOccurred(ConnectionEvent event) { PooledConnection pc = (PooledConnection) event.getSource(); if (cleanupMap.containsKey(pc)) { return; // waiting for listener cleanup - ignore event } try { if (null != event.getSQLException()) { System.err.println( "CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")"); } cleanupMap.put(pc, null); // mark for cleanup muteMap.put(pc, null); // ignore events until listener is removed } catch (Exception ignore) { // ignore } PooledConnectionAndInfo info = (PooledConnectionAndInfo) pcMap.get(pc); if (info == null) { throw new IllegalStateException(NO_KEY_MESSAGE); } try { _pool.invalidateObject(info.getUserPassKey(), info); } catch (Exception e) { System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info); e.printStackTrace(); } }
public void testPoolWithNullFactory() throws Exception { KeyedObjectPool pool = new StackKeyedObjectPool(10); for (int i = 0; i < 10; i++) { pool.returnObject("X", new Integer(i)); } for (int j = 0; j < 3; j++) { Integer[] borrowed = new Integer[10]; BitSet found = new BitSet(); for (int i = 0; i < 10; i++) { borrowed[i] = (Integer) (pool.borrowObject("X")); assertNotNull(borrowed); assertTrue(!found.get(borrowed[i].intValue())); found.set(borrowed[i].intValue()); } for (int i = 0; i < 10; i++) { pool.returnObject("X", borrowed[i]); } } pool.invalidateObject("X", pool.borrowObject("X")); pool.invalidateObject("X", pool.borrowObject("X")); pool.clear("X"); pool.clear(); }
public Object getValue(String serviceURL, String object, String attribute, String key) throws Exception { JMXConnector jmxc = null; try { // pega conector do pool jmxc = (JMXConnector) connectionPool.borrowObject(serviceURL); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ObjectName name = new ObjectName(object); Object value = mbsc.getAttribute(name, attribute); if (value instanceof CompositeData) { if (key == null) { logger.warn( "Attribute {}:{} is CompositeData but resource did not define a 'compositeDataKey'", object, attribute); value = null; } else { CompositeData data = (CompositeData) value; value = data.get(key); } } return value; } catch (Exception e) { if (null != jmxc) { connectionPool.invalidateObject(serviceURL, jmxc); jmxc = null; } throw e; } finally { if (null != jmxc) { connectionPool.returnObject(serviceURL, jmxc); } } }