Example usage for org.apache.commons.pool KeyedObjectPool invalidateObject

List of usage examples for org.apache.commons.pool KeyedObjectPool invalidateObject

Introduction

In this page you can find the example usage for org.apache.commons.pool KeyedObjectPool invalidateObject.

Prototype

void invalidateObject(Object key, Object obj) throws Exception;

Source Link

Document

Invalidates an object from the pool By contract, obj must have been obtained using #borrowObject borrowObject or a related method as defined in an implementation or sub-interface using a key that is equivalent to the one used to borrow the Object in the first place.

Usage

From source file:au.org.intersect.dms.wn.impl.TransportConnectionTemplate.java

public void purgeKey(ConnectionParams key) {
    if (key == null) {
        throw new ConnectionClosedError("The connection to this server has timed out. "
                + "Please close the current tab and re-establish the connection");
    }//ww  w  . j av a  2 s . c o  m
    // TODO CHECKSTYLE-OFF: IllegalCatch
    KeyedObjectPool pool = protoMapping.get(key.getProtocol());
    Object conn = null;
    try {
        conn = pool.borrowObject(key);
        pool.invalidateObject(key, conn);
    } catch (TransportException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.error("Failed to perform operation. Exception:", e);
        throw new ConnectionClosedError("This server has stopped responding. Please close the current tab and"
                + " re-establish the connection. "
                + "If the problem persists please contact your system administrator.");
    }
    // CHECKSTYLE-ON: IllegalCatch
}

From source file:au.org.intersect.dms.wn.impl.TransportConnectionTemplate.java

/**
 * Runs action with transport connection from pool and does proper pool handling.
 * //from  ww w .jav a2  s. c  o  m
 * @param <T>
 * @param key
 * @param action
 * @return
 */
public <T> T execute(ConnectionParams key, TransportConnectionCallback<T> action) {
    if (key == null) {
        throw new ConnectionClosedError("The connection to this server has timed out. "
                + "Please close the current tab and re-establish the connection");
    }
    // TODO CHECKSTYLE-OFF: IllegalCatch
    KeyedObjectPool pool = protoMapping.get(key.getProtocol());
    Object conn = null;
    try {
        conn = pool.borrowObject(key);
        return action.performWith((TransportConnection) conn);
    } catch (TransportException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.error("Failed to perform operation. Exception:", e);
        if (conn != null) {
            try {
                pool.invalidateObject(key, conn);
            } catch (Exception e1) {
                LOGGER.error("Failed to remove closed connection from the pool. Exception:", e1);
            }
        }
        conn = null;
        throw new ConnectionClosedError("This server has stopped responding. Please close the current tab and"
                + " re-establish the connection. "
                + "If the problem persists please contact your system administrator.");
    } finally {
        if (conn != null) {
            try {
                pool.returnObject(key, conn);
            } catch (Exception e) {
                LOGGER.error("Failed to return connection to the pool. Exception:", e);
            }
        }
    }
    // CHECKSTYLE-ON: IllegalCatch
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestKeyedObjectPool.java

public void testKPOFInvalidateObjectUsages() throws Exception {
    final FailingKeyedPoolableObjectFactory factory = new FailingKeyedPoolableObjectFactory();
    final KeyedObjectPool pool;
    try {// w  ww  .  j  a va2 s.c  om
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths

    obj = pool.borrowObject(KEY);
    clear(factory, expectedMethods);

    // invalidated object should be destroyed
    pool.invalidateObject(KEY, obj);
    expectedMethods.add(new MethodCall("destroyObject", KEY, obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of invalidateObject
    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject(KEY);
    clear(factory, expectedMethods);
    factory.setDestroyObjectFail(true);
    try {
        pool.invalidateObject(KEY, obj);
        fail("Expecting destroy exception to propagate");
    } catch (PrivateException ex) {
        // Expected
    }
    Thread.sleep(250); // could be defered
    TestObjectPool.removeDestroyObjectCall(factory.getMethodCalls());
    assertEquals(expectedMethods, factory.getMethodCalls());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestKeyedObjectPool.java

public void testClosedPoolBehavior() throws Exception {
    final KeyedObjectPool pool;
    try {//from www  . ja  v  a  2s .c o m
        pool = makeEmptyPool(new BaseKeyedPoolableObjectFactory() {
            public Object makeObject(final Object key) throws Exception {
                return new Object();
            }
        });
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }

    Object o1 = pool.borrowObject(KEY);
    Object o2 = pool.borrowObject(KEY);

    pool.close();

    try {
        pool.addObject(KEY);
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject(KEY);
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle(KEY));
    assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    pool.getNumActive();
    pool.getNumActive(KEY);
    pool.returnObject(KEY, o1);
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle(KEY));
    assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
            pool.getNumIdle());
    pool.invalidateObject(KEY, o2);
    pool.clear(KEY);
    pool.clear();
    pool.close();
}