當前位置: 首頁>>代碼示例>>Java>>正文


Java ObjectPool類代碼示例

本文整理匯總了Java中org.apache.commons.pool.ObjectPool的典型用法代碼示例。如果您正苦於以下問題:Java ObjectPool類的具體用法?Java ObjectPool怎麽用?Java ObjectPool使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ObjectPool類屬於org.apache.commons.pool包,在下文中一共展示了ObjectPool類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: ConnectionPooling

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Constructor 
 *
 * Params:
 *
 *
 */
public ConnectionPooling(String connectionURL, String userName, String password, String driverName) throws ClassNotFoundException, SQLException{
	Class.forName(driverName);
	
	Properties props = new Properties();
	props.setProperty("user", userName);
	props.setProperty("password", password);
	
	ObjectPool connectionPool = new GenericObjectPool(null);
	
	ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURL, props);
	
	PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, 
			connectionPool, null, null, false, true);
	
	
	Class.forName("org.apache.commons.dbcp.PoolingDriver");
       PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(myPoolingDriverName);
       driver.registerPool(myPoolName,connectionPool);
}
 
開發者ID:claresco,項目名稱:Tinman,代碼行數:27,代碼來源:ConnectionPooling.java

示例2: close

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * 關閉
 * 
 * @throws CacheException
 */
public synchronized void close() throws CacheException {
	if (this.closed) {
		throw new CacheException("CacheFactory was already closed");
	}
	//
	try {
		// TODO cacheHolder 未清, 需用反射清除
		this.closed = true;
		ObjectPool<T> oldPool = this.objectPool;
		this.objectPool = null;
		if (oldPool != null) {
			oldPool.close();
		}
	} catch (Exception ex) {
		throw new CacheException("Cannot close pool", ex);
	}
}
 
開發者ID:mixaceh,項目名稱:openyu-commons,代碼行數:23,代碼來源:PoolingCacheFactory.java

示例3: score

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * The the given <code>Object[]</code> with this scorer (thread safe!).
 * <p/>
 * The score in bound by the configuration <code>minScore</code> and <code>maxScore</code>.
 *
 *
 * @param scorable the scorable data to score
 * @return the score value already bound the configuration range
 * @throws FOSException when classification was not possible
 */
@Override
public double[] score(Object[] scorable) throws FOSException {
    /* the pool can change while this is processing (reload) so assign a local variable */
    final ObjectPool<Classifier> localPool = pool;

    Classifier classifier = null;
    try {
        classifier = localPool.borrowObject();

        return WekaUtils.score(classifier, scorable, instanceSetters, instances, attributes);
    } catch (Exception e) {
        throw new FOSException(e);
    } finally {
        returnObject(localPool, classifier);
    }
}
 
開發者ID:feedzai,項目名稱:fos-weka,代碼行數:27,代碼來源:WekaThreadSafeScorerPool.java

示例4: releaseJdbcConnection

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
public void releaseJdbcConnection(JdbcConnectionDescriptor jcd, Connection con)
        throws LookupException
{
    final ObjectPool op = (ObjectPool) poolMap.get(jcd.getPBKey());
    try
    {
        /* mkalen: NB - according to the Commons Pool API we should _not_ perform
         * any additional checks here since we will then break testOnX semantics
         *
         * To enable Connection validation on releaseJdbcConnection,
         * set a validation query and specify testOnRelease=true
         *
         * Destruction of pooled objects is performed by the actual Commons Pool
         * ObjectPool implementation when the object factory's validateObject method
         * returns false. See ConPoolFactory#validateObject.
         */
        op.returnObject(con);
    }
    catch (Exception e)
    {
        throw new LookupException(e);
    }
}
 
開發者ID:KualiCo,項目名稱:ojb,代碼行數:24,代碼來源:ConnectionFactoryPooledImpl.java

示例5: releaseAllResources

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Closes all managed pools.
 */
public void releaseAllResources()
{
    synchronized (poolSynch)
    {
        Collection pools = poolMap.values();
        poolMap = new HashMap(poolMap.size());
        ObjectPool op = null;
        for (Iterator iterator = pools.iterator(); iterator.hasNext();)
        {
            try
            {
                op = ((ObjectPool) iterator.next());
                op.close();
            }
            catch (Exception e)
            {
                log.error("Exception occured while closing pool " + op, e);
            }
        }
    }
    super.releaseAllResources();
}
 
開發者ID:KualiCo,項目名稱:ojb,代碼行數:26,代碼來源:ConnectionFactoryPooledImpl.java

示例6: getDataSource

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
private DataSource getDataSource(LinkDataSource lds){
	try{
		String driver, url, user, passwd;
		String[] access = lds.getAccess().split(",");
		driver = access[0];
		url = access[1];
		user = access[2];
		passwd = access[3];
		
		Class.forName(driver);
		
		ObjectPool connectionPool = new GenericObjectPool(null);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, passwd);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
        PoolingDataSource data_source = new PoolingDataSource(connectionPool);
        
        return data_source;
	}
	catch(Exception e){
		
	}
	
	return null;
}
 
開發者ID:openmrs,項目名稱:openmrs-module-patientmatching,代碼行數:25,代碼來源:ReaderProvider.java

示例7: invalidateConnection

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Invalidates the given connection.
 * 
 * @param conn connection to invalidate
 * @throws SQLException if the connection is not a 
 * <code>PoolGuardConnectionWrapper</code> or an error occurs invalidating
 * the connection
 * @since 1.2.2
 */
public void invalidateConnection(Connection conn) throws SQLException {
    if (conn instanceof PoolGuardConnectionWrapper) { // normal case
        PoolGuardConnectionWrapper pgconn = (PoolGuardConnectionWrapper) conn;
        ObjectPool pool = pgconn.pool;
        Connection delegate = pgconn.delegate;
        try {
            pool.invalidateObject(delegate);
        } 
        catch (Exception e) { 
        }
        pgconn.delegate = null;
    }
    else {
        throw new SQLException("Invalid connection class");
    }
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:26,代碼來源:PoolingDriver.java

示例8: PoolableConnectionFactory

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Create a new <tt>PoolableConnectionFactory</tt>.
 * @param connFactory the {@link ConnectionFactory} from which to obtain base {@link Connection}s
 * @param pool the {@link ObjectPool} in which to pool those {@link Connection}s
 * @param stmtPoolFactory the {@link KeyedObjectPoolFactory} to use to create {@link KeyedObjectPool}s for pooling {@link java.sql.PreparedStatement}s, or <tt>null</tt> to disable {@link java.sql.PreparedStatement} pooling
 * @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s.  Should return at least one row. Using <tt>null</tt> turns off validation.
 * @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s
 * @param defaultAutoCommit the default "auto commit" setting for returned {@link Connection}s
 * @param config the AbandonedConfig if tracing SQL objects
 */
public PoolableConnectionFactory(
    ConnectionFactory connFactory,
    ObjectPool pool,
    KeyedObjectPoolFactory stmtPoolFactory,
    String validationQuery,
    boolean defaultReadOnly,
    boolean defaultAutoCommit,
    AbandonedConfig config) {

    _connFactory = connFactory;
    _pool = pool;
    _config = config;
    _pool.setFactory(this);
    _stmtPoolFactory = stmtPoolFactory;
    _validationQuery = validationQuery;
    _defaultReadOnly = defaultReadOnly ? Boolean.TRUE : Boolean.FALSE;
    _defaultAutoCommit = defaultAutoCommit;
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:29,代碼來源:PoolableConnectionFactory.java

示例9: testStmtPool

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
public void testStmtPool() throws Exception {
    new TesterDriver();
    ConnectionFactory connFactory = new DriverManagerConnectionFactory(
            "jdbc:apache:commons:testdriver","u1","p1");

    ObjectPool connPool = new GenericObjectPool();
    KeyedObjectPoolFactory stmtPoolFactory = new GenericKeyedObjectPoolFactory(null);

    new PoolableConnectionFactory(connFactory, connPool, stmtPoolFactory,
            null, false, true);

    DataSource ds = new PoolingDataSource(connPool);

    Connection conn = ds.getConnection();
    Statement stmt1 = conn.prepareStatement("select 1 from dual");
    Statement ustmt1 = ((DelegatingStatement) stmt1).getInnermostDelegate();
    stmt1.close();
    Statement stmt2 = conn.prepareStatement("select 1 from dual");
    Statement ustmt2 = ((DelegatingStatement) stmt2).getInnermostDelegate();
    stmt2.close();
    assertSame(ustmt1, ustmt2);
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:23,代碼來源:TestPStmtPooling.java

示例10: testPoolWithNullFactory

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * @deprecated - to be removed in pool 2.0
 */
public void testPoolWithNullFactory() throws Exception {
    ObjectPool pool = new StackObjectPool(10);
    for(int i=0;i<10;i++) {
        pool.returnObject(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());
            assertNotNull(borrowed);
            assertTrue(!found.get(borrowed[i].intValue()));
            found.set(borrowed[i].intValue());
        }
        for(int i=0;i<10;i++) {
            pool.returnObject(borrowed[i]);
        }
    }
    pool.invalidateObject(pool.borrowObject());
    pool.invalidateObject(pool.borrowObject());
    pool.clear();        
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:26,代碼來源:TestStackObjectPool.java

示例11: testBorrowReturnWithSometimesInvalidObjects

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Verifies that validation and passivation failures returning objects are handled
 * properly - instances destroyed and not returned to the pool, but no exceptions propagated.
 */
public void testBorrowReturnWithSometimesInvalidObjects() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 20);

    Object[] obj = new Object[10];
    for(int i=0;i<10;i++) {
        obj[i] = pool.borrowObject();
        assertEquals("Each time we borrow, get one more active.", i+1, pool.getNumActive());
        
    }
    
    factory.setValidateSelectively(true);  // Even numbers fail validation
    factory.setPassivateSelectively(true); // Multiples of 3 fail passivation

    for(int i=0;i<10;i++) {
        pool.returnObject(obj[i]);
        assertEquals("Each time we return, get one less active.", 9-i, pool.getNumActive());
    }
    // 0,2,4,6,8 fail validation, 3, 9 fail passivation - 3 left.
    assertEquals(3,pool.getNumIdle());
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:26,代碼來源:TestStackObjectPool.java

示例12: testExceptionOnActivate

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Verifies that exceptions thrown by factory activate method are not propagated to
 * the caller.  Objects that throw on activate are destroyed and if none succeed,
 * the caller gets NoSuchElementException.
 */
public void testExceptionOnActivate() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory);
    pool.addObject();
    pool.addObject();
    factory.setThrowOnActivate(true);
    try {
        pool.borrowObject();
        fail("Expecting NoSuchElementException");
    } catch (NoSuchElementException ex) {
        // expected
    }
    assertEquals(0, pool.getNumIdle());
    assertEquals(0, pool.getNumActive());
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:21,代碼來源:TestStackObjectPool.java

示例13: testExceptionOnDestroy

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Verifies that exceptions thrown by factory destroy are swallowed
 * by both addObject and returnObject.
 */
public void testExceptionOnDestroy() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 2);
    factory.setThrowOnDestroy(true);
    for (int i = 0; i < 3; i++) {
        pool.addObject(); // Third one will destroy, exception should be swallowed
    }
    assertEquals(2, pool.getNumIdle());
    
    Object[] objects = new Object[3];
    for (int i = 0; i < 3; i++) {
        objects[i] = pool.borrowObject();
    }
    for (int i = 0; i < 3; i++) {
        pool.returnObject(objects[i]); // Third triggers destroy
    } 
    assertEquals(2, pool.getNumIdle());
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:23,代碼來源:TestStackObjectPool.java

示例14: testExceptionOnPassivate

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
/**
 * Verifies that addObject propagates exceptions thrown by
 * factory passivate, but returnObject swallows these.
 */
public void testExceptionOnPassivate() throws Exception {
    SelectiveFactory factory = new SelectiveFactory();
    ObjectPool pool = new StackObjectPool(factory, 2);
    factory.setThrowOnPassivate(true);
    
    // addObject propagates
    try {
        pool.addObject();
        fail("Expecting IntegerFactoryException");
    } catch (IntegerFactoryException ex) {
        assertEquals("passivateObject", ex.getType());
        assertEquals(0, ex.getValue());
    }
    assertEquals(0, pool.getNumIdle());
    
    // returnObject swallows 
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    assertEquals(0, pool.getNumIdle());
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:25,代碼來源:TestStackObjectPool.java

示例15: testAddObject

import org.apache.commons.pool.ObjectPool; //導入依賴的package包/類
public void testAddObject() throws Exception {
    assertEquals("should be zero idle", 0, pool.getNumIdle());
    pool.addObject();
    assertEquals("should be one idle", 1, pool.getNumIdle());
    assertEquals("should be zero active", 0, pool.getNumActive());
    Object obj = pool.borrowObject();
    assertEquals("should be zero idle", 0, pool.getNumIdle());
    assertEquals("should be one active", 1, pool.getNumActive());
    pool.returnObject(obj);
    assertEquals("should be one idle", 1, pool.getNumIdle());
    assertEquals("should be zero active", 0, pool.getNumActive());

    ObjectPool op = new GenericObjectPool();
    try {
        op.addObject();
        fail("Expected IllegalStateException when there is no factory.");
    } catch (IllegalStateException ise) {
        //expected
    }
    op.close();
}
 
開發者ID:WhiteBearSolutions,項目名稱:WBSAirback,代碼行數:22,代碼來源:TestGenericObjectPool.java


注:本文中的org.apache.commons.pool.ObjectPool類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。