public void testSetFactory() throws Exception {
   KeyedObjectPool pool = new StackKeyedObjectPool();
   try {
     pool.borrowObject("x");
     fail("Expected NoSuchElementException");
   } catch (NoSuchElementException e) {
     // expected
   }
   pool.setFactory(new SimpleFactory());
   Object obj = pool.borrowObject("x");
   assertNotNull(obj);
   pool.returnObject("x", obj);
 }
 public void testCanResetFactoryWithoutActiveObjects() throws Exception {
   KeyedObjectPool pool = new StackKeyedObjectPool();
   {
     pool.setFactory(new SimpleFactory());
     Object obj = pool.borrowObject("x");
     assertNotNull(obj);
     pool.returnObject("x", obj);
   }
   {
     pool.setFactory(new SimpleFactory());
     Object obj = pool.borrowObject("x");
     assertNotNull(obj);
     pool.returnObject("x", obj);
   }
 }
 public void testBorrowFromEmptyPoolWithNullFactory() throws Exception {
   KeyedObjectPool pool = new StackKeyedObjectPool();
   try {
     pool.borrowObject("x");
     fail("Expected NoSuchElementException");
   } catch (NoSuchElementException e) {
     // expected
   }
 }
 /**
  * Create or obtain a {@link PreparedStatement} from my pool.
  *
  * @return a {@link PoolablePreparedStatement}
  */
 public synchronized PreparedStatement prepareStatement(String sql) throws SQLException {
   try {
     return (PreparedStatement) (_pstmtPool.borrowObject(createKey(sql)));
   } catch (NoSuchElementException e) {
     throw new SQLNestedException("MaxOpenPreparedStatements limit reached", e);
   } catch (RuntimeException e) {
     throw e;
   } catch (Exception e) {
     throw new SQLNestedException("Borrow prepareStatement from pool failed", e);
   }
 }
  public void testCantResetFactoryWithActiveObjects() throws Exception {
    KeyedObjectPool pool = new StackKeyedObjectPool();
    pool.setFactory(new SimpleFactory());
    Object obj = pool.borrowObject("x");
    assertNotNull(obj);

    try {
      pool.setFactory(new SimpleFactory());
      fail("Expected IllegalStateException");
    } catch (IllegalStateException e) {
      // expected
    }
  }
 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();
 }
Exemplo n.º 7
0
  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);
      }
    }
  }
  public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
    KeyedObjectPool pool =
        new StackKeyedObjectPool(
            new KeyedPoolableObjectFactory() {
              int counter = 0;

              public Object makeObject(Object key) {
                return new Integer(counter++);
              }

              public void destroyObject(Object key, Object obj) {}

              public boolean validateObject(Object key, Object obj) {
                if (obj instanceof Integer) {
                  return ((((Integer) obj).intValue() % 2) == 1);
                } else {
                  return false;
                }
              }

              public void activateObject(Object key, Object obj) {}

              public void passivateObject(Object key, Object obj) {
                if (obj instanceof Integer) {
                  if ((((Integer) obj).intValue() % 3) == 0) {
                    throw new RuntimeException("Couldn't passivate");
                  }
                } else {
                  throw new RuntimeException("Couldn't passivate");
                }
              }
            });

    Object[] obj = new Object[10];
    for (int i = 0; i < 10; i++) {
      Object object = null;
      int k = 0;
      while (object == null && k < 100) { // bound not really needed
        try {
          k++;
          object = pool.borrowObject("key");
          obj[i] = object;
        } catch (NoSuchElementException ex) {
          // Expected for evens, which fail validation
        }
      }
      assertEquals("Each time we borrow, get one more active.", i + 1, pool.getNumActive());
    }
    // 1,3,5,...,19 pass validation, get checked out
    for (int i = 0; i < 10; i++) {
      pool.returnObject("key", obj[i]);
      assertEquals("Each time we borrow, get one less active.", 9 - i, pool.getNumActive());
    }
    // 3, 9, 15 fail passivation.
    assertEquals(7, pool.getNumIdle());
    assertEquals(new Integer(19), pool.borrowObject("key"));
    assertEquals(new Integer(17), pool.borrowObject("key"));
    assertEquals(new Integer(13), pool.borrowObject("key"));
    assertEquals(new Integer(11), pool.borrowObject("key"));
    assertEquals(new Integer(7), pool.borrowObject("key"));
    assertEquals(new Integer(5), pool.borrowObject("key"));
    assertEquals(new Integer(1), pool.borrowObject("key"));
  }