本文整理汇总了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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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;
}
示例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");
}
}
示例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;
}
示例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);
}
示例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();
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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();
}