Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory) 

Source Link

Document

Create a new GenericObjectPool using the specified factory.

Usage

From source file:org.kchine.rpf.db.ServantProxyPoolSingletonDB.java

public static GenericObjectPool getInstance(String poolName, String driver, final String url, final String user,
        final String password) {
    String key = driver + "%" + poolName + "%" + url + "%" + user + "%" + password;
    if (_pool.get(key) != null)
        return _pool.get(key);
    synchronized (lock) {
        if (_pool.get(key) == null) {
            Connection conn = null;
            try {
                Class.forName(driver);
                final Vector<Object> borrowedObjects = new Vector<Object>();
                DBLayerInterface dbLayer = DBLayer.getLayer(getDBType(url), new ConnectionProvider() {
                    public Connection newConnection() throws SQLException {
                        return DriverManager.getConnection(url, user, password);
                    }//from   www  . j a  v a 2 s . c o  m
                });
                final GenericObjectPool p = new GenericObjectPool(
                        new ServantProxyFactoryDB(poolName, dbLayer)) {
                    @Override
                    public synchronized Object borrowObject() throws Exception {
                        if (_shuttingDown)
                            throw new NoSuchElementException();
                        Object result = super.borrowObject();
                        borrowedObjects.add(result);
                        return result;
                    }

                    @Override
                    public synchronized void returnObject(Object obj) throws Exception {
                        super.returnObject(obj);
                        borrowedObjects.remove(obj);
                    }
                };

                if (System.getProperty("pools.dbmode.shutdownhook.enabled") != null
                        && System.getProperty("pools.dbmode.shutdownhook.enabled").equalsIgnoreCase("false")) {

                } else {
                    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                        public void run() {
                            synchronized (p) {

                                final Vector<Object> bo = (Vector<Object>) borrowedObjects.clone();

                                _shuttingDown = true;
                                try {
                                    for (int i = 0; i < bo.size(); ++i)
                                        p.returnObject(bo.elementAt(i));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }));
                }

                _pool.put(key, p);
                p.setMaxIdle(0);
                p.setTestOnBorrow(true);
                p.setTestOnReturn(true);
            } catch (Exception e) {
                throw new RuntimeException(getStackTraceAsString(e));
            }

        }
        return _pool.get(key);
    }
}

From source file:org.kchine.rpf.db.ServantProxyPoolSingletonDB.java

public static GenericObjectPool getInstance(String poolName, DBLayerInterface dbLayer) {
    String key = poolName + "%" + dbLayer.toString();

    if (_pool.get(key) != null)
        return _pool.get(key);
    synchronized (lock) {
        if (_pool.get(key) == null) {
            Connection conn = null;
            try {
                final Vector<Object> borrowedObjects = new Vector<Object>();

                final GenericObjectPool p = new GenericObjectPool(
                        new ServantProxyFactoryDB(poolName, dbLayer)) {
                    @Override/*from   w w  w.  j  a  va  2s.  c om*/
                    public synchronized Object borrowObject() throws Exception {
                        if (_shuttingDown)
                            throw new NoSuchElementException();
                        Object result = super.borrowObject();
                        borrowedObjects.add(result);
                        return result;
                    }

                    @Override
                    public synchronized void returnObject(Object obj) throws Exception {
                        super.returnObject(obj);
                        borrowedObjects.remove(obj);
                    }
                };

                if (System.getProperty("pools.dbmode.shutdownhook.enabled") != null
                        && System.getProperty("pools.dbmode.shutdownhook.enabled").equalsIgnoreCase("false")) {

                } else {
                    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                        public void run() {
                            synchronized (p) {

                                final Vector<Object> bo = (Vector<Object>) borrowedObjects.clone();

                                _shuttingDown = true;
                                try {
                                    for (int i = 0; i < bo.size(); ++i)
                                        p.returnObject(bo.elementAt(i));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }));
                }

                _pool.put(key, p);
                p.setMaxIdle(0);
                p.setTestOnBorrow(true);
                p.setTestOnReturn(true);
            } catch (Exception e) {
                throw new RuntimeException(getStackTraceAsString(e));
            }

        }
        return _pool.get(key);
    }
}

From source file:org.kchine.rpf.reg.ServantProxyPoolSingletonReg.java

public static GenericObjectPool getInstance(String registryHost, int registryPort, String poolNamingPrefix) {

    String key = registryHost + '/' + registryPort + '/' + poolNamingPrefix;
    if (_pool.get(key) != null)
        return _pool.get(key);

    synchronized (lock) {

        if (_pool.get(key) == null) {
            GenericObjectPool p = new GenericObjectPool(
                    new ServantsProxyFactoryReg(registryHost, registryPort, poolNamingPrefix));
            _pool.put(key, p);/*from   w ww . j a v  a 2s .c o m*/
            p.setTestOnBorrow(true);
            p.setTestOnReturn(true);
        }
        return _pool.get(key);
    }
}

From source file:org.kitodo.data.database.persistence.apache.ConnectionManager.java

/**
 *
 * @param connectURI/*from  ww w .j  a va2s  . co  m*/
 *            - JDBC Connection URI.
 * @param username
 *            - JDBC Connection username.
 * @param password
 *            - JDBC Connection password.
 * @param minIdle
 *            - Minimum number of idel connection in the connection pool.
 * @param maxActive
 *            - Connection Pool Maximum Capacity (Size).
 */
public static DataSource setupDataSource(String connectURI, String username, String password, int minIdle,
        int maxActive) {
    //
    // First, we'll need a ObjectPool that serves as the actual pool of
    // connections.
    //
    // We'll use a GenericObjectPool instance, although any ObjectPool
    // implementation will suffice.
    //
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMinIdle(minIdle);
    connectionPool.setMaxActive(maxActive);

    ConnectionManager._pool = connectionPool;
    // we keep it for two reasons
    // #1 We need it for statistics/debugging
    // #2 PoolingDataSource does not have getPool() method, for some
    // obscure, weird reason.

    //
    // Next, we'll create a ConnectionFactory that the pool will use to
    // create Connections.
    // We'll use the DriverManagerConnectionFactory, using the connect
    // string from configuration
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps the
    // "real" Connections created by
    // the ConnectionFactory with the classes that implement the pooling
    // functionality.
    //
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:org.methodize.nntprss.feed.db.JdbcChannelDAO.java

private void initializeDatabasePool(Document config) throws Exception {
    Element rootElm = config.getDocumentElement();
    Element dbConfig = (Element) rootElm.getElementsByTagName("db").item(0);
    String connectString = dbConfig.getAttribute("connect");

    if (log.isInfoEnabled()) {
        log.info("Initializing JDBC, connection string = " + connectString);
    }/*from   www .  j  a  va2 s . c o m*/

    ObjectPool connectionPool = new GenericObjectPool(null);

    String dbDriver = dbConfig.getAttribute("driverClass");
    if (dbDriver != null && dbDriver.length() > 0) {
        Class.forName(dbDriver);
    } else {
        // Default to HSSQLDB
        Class.forName("org.hsqldb.jdbcDriver");
    }

    String user = dbConfig.getAttribute("user");
    String password = dbConfig.getAttribute("password");
    if (user == null) {
        user = "sa";
    }
    if (password == null) {
        password = "";
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectString, user, password);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    //
    // Finally, we create the PoolingDriver itself...
    //
    PoolingDriver driver = new PoolingDriver();

    //
    // ...and register our pool with it.
    //
    driver.registerPool("nntprss", connectionPool);
}

From source file:org.monansrill.ConfluenceDBBasedUserInfoGetter.java

public static PoolingDataSource setupDataSource() {
    try {/*w  w  w  .j a va  2  s. c  o  m*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    ObjectPool connectionPool = new GenericObjectPool(null);
    String connectionURI;
    Properties secretProperties = getSecretProperties();

    if (secretProperties.getProperty("dbpass") == null) {
        System.err.println(
                "sorry you need to specifiby dbpass and dbuser as properties with $HOME/.ictask.properties");
        System.exit(-1);
    }
    connectionURI = "jdbc:mysql://localhost/confluence?zeroDateTimeBehavior=convertToNull";
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI,
            secretProperties.getProperty("dbuser"), secretProperties.getProperty("dbpass"));
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:org.mule.module.dxpath.DxTransformer.java

public DxTransformer() {
    super();/*from   ww  w .  ja  va 2s .  c o m*/

    transformerPool = new GenericObjectPool(new PooledDxTransformerFactory());
    transformerPool.setMinIdle(MIN_IDLE_TRANSFORMERS);
    transformerPool.setMaxIdle(MAX_IDLE_TRANSFORMERS);
    transformerPool.setMaxActive(MAX_ACTIVE_TRANSFORMERS);

    registerSourceType(DataTypeFactory.create(org.w3c.dom.Node.class));
    registerSourceType(DataTypeFactory.create(InputSource.class));
    registerSourceType(DataTypeFactory.create(NullPayload.class));

    contextProperties = new HashMap<String, Object>();
}

From source file:org.mule.module.xml.transformer.XQueryTransformer.java

public XQueryTransformer() {
    super();//  w  ww.j  av  a2 s .c  om
    transformerPool = new GenericObjectPool(new PooledXQueryTransformerFactory());
    transformerPool.setMinIdle(MIN_IDLE_TRANSFORMERS);
    transformerPool.setMaxIdle(MAX_IDLE_TRANSFORMERS);
    transformerPool.setMaxActive(MAX_ACTIVE_TRANSFORMERS);

    registerSourceType(DataTypeFactory.STRING);
    registerSourceType(DataTypeFactory.BYTE_ARRAY);
    registerSourceType(DataTypeFactory.create(DocumentSource.class));
    registerSourceType(DataTypeFactory.create(org.dom4j.Document.class));
    registerSourceType(DataTypeFactory.create(Document.class));
    registerSourceType(DataTypeFactory.create(Element.class));
    registerSourceType(DataTypeFactory.INPUT_STREAM);
    setReturnDataType(DataTypeFactory.create(Element.class));
}

From source file:org.mule.module.xml.transformer.XsltTransformer.java

public XsltTransformer() {
    super();/*from  ww  w.  j a v  a2s  . c om*/
    transformerPool = new GenericObjectPool(new PooledXsltTransformerFactory());
    transformerPool.setMinIdle(MIN_IDLE_TRANSFORMERS);
    transformerPool.setMaxIdle(MAX_IDLE_TRANSFORMERS);
    transformerPool.setMaxActive(MAX_ACTIVE_TRANSFORMERS);
    contextProperties = new HashMap<String, Object>();
}

From source file:org.mule.transformers.xml.xquery.XQueryTransformer.java

public XQueryTransformer() {
    super();/*from w w  w. ja  v a2 s .  c  om*/
    transformerPool = new GenericObjectPool(new PooledXQueryTransformerFactory());
    transformerPool.setMinIdle(MIN_IDLE_TRANSFORMERS);
    transformerPool.setMaxIdle(MAX_IDLE_TRANSFORMERS);
    transformerPool.setMaxActive(MAX_ACTIVE_TRANSFORMERS);

    registerSourceType(String.class);
    registerSourceType(byte[].class);
    registerSourceType(DocumentSource.class);
    registerSourceType(org.dom4j.Document.class);
    registerSourceType(Document.class);
    registerSourceType(Element.class);
    registerSourceType(InputStream.class);
    setReturnClass(Element.class);
}