List of usage examples for org.apache.commons.dbcp PoolingDriver registerPool
public synchronized void registerPool(String name, ObjectPool pool)
From source file:ConnectionPoolBasics.java
public static void main(String args[]) throws Exception { GenericObjectPool gPool = new GenericObjectPool(); /*Class.forName("com.mysql.jdbc.Driver"); /*from w w w . j a va2 s. co m*/ DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory( "jdbc:mysql://localhost/commons", "root", "");*/ Properties props = new Properties(); props.setProperty("Username", "root"); props.setProperty("Password", ""); ConnectionFactory cf = new DriverConnectionFactory(new com.mysql.jdbc.Driver(), "jdbc:mysql://localhost/commons", props); KeyedObjectPoolFactory kopf = new GenericKeyedObjectPoolFactory(null, 8); PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, gPool, kopf, null, false, true); for (int i = 0; i < 5; i++) { gPool.addObject(); } // PoolingDataSource pds = new PoolingDataSource(gPool); PoolingDriver pd = new PoolingDriver(); pd.registerPool("example", gPool); for (int i = 0; i < 5; i++) { gPool.addObject(); } Connection conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.err.println("Connection: " + conn); //": Delegate: " + ((org.apache.commons.dbcp.PoolingConnection)conn).getDelegate()); // do some work with the connection PreparedStatement ps = conn.prepareStatement("Select * from customer where id = ?"); System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle()); conn.close(); System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle()); }
From source file:ManualPoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ////from w w w.ja v a2 s . c o m // 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. // ObjectPool connectionPool = new GenericObjectPool(null); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null); // // 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... // Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:com.jt.dbcp.example.ManualPoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ////from w w w . ja va 2 s . c o m // 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. // ObjectPool connectionPool = new GenericObjectPool(null); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, "root", "root"); // // 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... // Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:com.claresco.tinman.servlet.ConnectionPooling.java
/** * Constructor /*from w ww .j a v a2s. c om*/ * * 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); }
From source file:com.google.acre.keystore.MySQLKeyStore.java
@SuppressWarnings("unused") private void setupDriver(String connectURI, String username, String password) throws SQLException, ClassNotFoundException { GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000 - 13); // check if jdbc connections are still alive every 5 min - 13 msec ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, TEST_QUERY, false, true); Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX); driver.registerPool(DATASOURCE_NAME, connectionPool); active = true;/* ww w . j av a 2 s .c o m*/ // Now we can just use the connect string "jdbc:apache:commons:dbcp:keystore" // to access our pool of Connections. }
From source file:com.iver.utiles.connections.ConnectionDB.java
/** * Registers in the driverpool a new connection * //from w ww.j a v a 2 s .com * @param ct * Data connection * @param driver * Driver * * @throws ConnectionException */ public void setupDriver(ConnectionTrans ct) throws ConnectionException { String url = ct.getConnBeginning() + "//" + ct.getHost() + ":" + ct.getPort() + "/" + ct.getDb(); String user = ct.getUser(); String password = ct.getPassword(); String name = ct.getHost() + "_" + ct.getName(); ObjectPool connectionPool = new GenericObjectPool(); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, password); try { PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); } catch (Exception e) { throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_pool"), e); } try { Class.forName("org.apache.commons.dbcp.PoolingDriver"); } catch (ClassNotFoundException e) { throw new ConnectionException("Clase : " + "org.apache.commons.dbcp.PoolingDriver", e); } PoolingDriver driverPool; try { driverPool = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); } catch (SQLException e) { throw new ConnectionException(JDBCManager.getTranslation("fallo_registrar_conexion"), e); } driverPool.registerPool(name, connectionPool); ct.setConnected(true); }
From source file:ojw28.orm.utils.DbConnectionPool.java
private void createPool() throws Exception { mPool = new GenericObjectPool(); Properties props = new Properties(); props.setProperty("user", "orm"); props.setProperty("password", "openroommap"); ConnectionFactory cf = new DriverConnectionFactory(new org.postgresql.Driver(), "jdbc:postgresql://localhost:5432/openroommap", props); KeyedObjectPoolFactory kopf = new GenericKeyedObjectPoolFactory(null, 10); new PoolableConnectionFactory(cf, mPool, kopf, null, false, false); for (int i = 0; i < 5; i++) { mPool.addObject();// ww w . j ava 2s . c o m } // PoolingDataSource pds = new PoolingDataSource(gPool); PoolingDriver pd = new PoolingDriver(); pd.registerPool("openroommap", mPool); for (int i = 0; i < 5; i++) { mPool.addObject(); } mLogger.info("Created connection pool"); mLogger.info("Active\t" + mPool.getNumActive() + "\tIdle\t" + mPool.getNumIdle()); }
From source file:org.aitools.programd.Core.java
/** * Returns a database connection backed by a pooling driver. * This is initialized lazily, since some people may not be * using any database-based features.// w w w .ja v a 2 s . com * * @return a dbmanager */ public Connection getDBConnection() { if (this._connectionPool == null) { try { Class.forName(this._settings.getDatabaseDriver()); } catch (ClassNotFoundException e) { throw new UserError("Could not find your database driver.", e); } this._connectionPool = new GenericObjectPool(); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( this._settings.getDatabaseURL(), this._settings.getDatabaseUsername(), this._settings.getDatabasePassword()); new PoolableConnectionFactory(connectionFactory, this._connectionPool, null, null, false, true); PoolingDriver driver = new PoolingDriver(); driver.registerPool("programd", this._connectionPool); } try { return DriverManager.getConnection("jdbc:apache:commons:dbcp:programd"); } catch (SQLException e) { this._logger.error("SQL exception when getting a db connection.", e); return null; } }
From source file:org.apache.avalon.dbcp.DbcpConnectionManager.java
/** * Initialization of the component by the container. * //from ww w . j a va 2 s .c om * @throws Exception if an error occurs while initializing * the component */ public void initialize() throws Exception { m_logger.debug("Initializing..."); // First, let's load the DBCP's pooling driver class m_logger.debug("Loading DBCP Pooling Driver class..."); Class.forName("org.apache.commons.dbcp.PoolingDriver"); // loop through all the configured datasources... for (int i = 0; i < m_datasources.length; i++) { // this is the next datasource configuration object to process Configuration datasource = m_datasources[i]; String name = datasource.getAttribute("name"); boolean isDefault = datasource.getAttributeAsBoolean("default", false); m_logger.debug("Processing datasource [" + name + "]" + " (default=" + isDefault + ")"); // create an object pool that will actually hold our connections ObjectPool connectionPool = createObjectPool(); m_logger.debug("Object pool created..."); // load the underlying JDBC driver of the datasource loadJDBCDriver(datasource); m_logger.debug("Underlying JDBC driver loaded..."); // create a driver manager connection factory that will be // used to actually create the database connection(s) ConnectionFactory connectionFactory = createConnectionFactory(datasource); m_logger.debug("Connection factory created..."); // create a poolable connection factory wrapper PoolableConnectionFactory poolableConnectionFactory = createPoolableConnectionFactory(connectionFactory, connectionPool, datasource); m_logger.debug("Poolable connection factory created..."); // get an instance of the DBCP pooling driver... PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); m_logger.debug("Pooling driver instance obtained..."); // register our pool with it... driver.registerPool(name, connectionPool); // is this the default datasource? if (isDefault) { driver.registerPool("default", connectionPool); } } }
From source file:org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCachePoolAccess.java
/** * @param connectURI/* w w w .j a v a 2 s . c o m*/ * @param userName * @param password * @param maxActive max connetions * @throws Exception */ public void setupDriver(String connectURI, String userName, String password, int maxActive) throws Exception { // 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. ObjectPool connectionPool = new GenericObjectPool(null, maxActive); // TODO make configurable // By dfault the size is 8!!!!!!! ((GenericObjectPool) connectionPool).setMaxIdle(-1); // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // Properties props = new Properties(); // props.setProperty( "user", userName ); // props.setProperty( "password", password ); 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. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); // Finally, we create the PoolingDriver itself... Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_NAME); // ...and register our pool with it. driver.registerPool(this.getPoolName(), connectionPool); // Now we can just use the connect string // "jdbc:apache:commons:dbcp:jcs" // to access our pool of Connections. }