List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory
public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool, KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly, boolean defaultAutoCommit)
From source file:DBCPDemo.java
public static void main(String args[]) throws Exception { // create a generic pool GenericObjectPool pool = new GenericObjectPool(null); // use the connection factory which will wraped by // the PoolableConnectionFactory DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory( "jdbc:jtds:sqlserver://myserver:1433/tandem", "user", "pass"); PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db", false, true);//from w ww .ja v a2s . co m // register our pool and give it a name new PoolingDriver().registerPool("myPool", pool); // get a connection and test it Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:myPool"); // now we can use this pool the way we want. System.err.println("Are we connected? " + !conn.isClosed()); System.err.println("Idle Connections: " + pool.getNumIdle() + ", out of " + pool.getNumActive()); }
From source file:ConnectionPoolBasics.java
public static void main(String args[]) throws Exception { GenericObjectPool gPool = new GenericObjectPool(); /*Class.forName("com.mysql.jdbc.Driver"); /*www. j a va 2 s. com*/ 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:com.tethrnet.manage.util.DSPool.java
/** * register the data source for H2 DB/*from w w w.j a va2s.co m*/ * * @return pooling database object */ private static PoolingDataSource registerDataSource() { // create a database connection String user = "tethrnetbox"; String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf"; String connectionURI = "jdbc:h2:" + DB_PATH + "/tethrnetbox;CIPHER=AES"; String validationQuery = "select 1"; try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException ex) { log.error(ex.toString(), ex); } GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setMaxActive(25); connectionPool.setTestOnBorrow(true); connectionPool.setMinIdle(2); connectionPool.setMaxWait(15000); connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true); return new PoolingDataSource(connectionPool); }
From source file:com.ewcms.plugin.externalds.generate.service.dbcp.DbcpDataSource.java
protected void createPoolableConnectionFactory(String url, String username, String password) { ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, true, false); }
From source file:com.hangum.tadpole.tajo.core.connections.manager.ConnectionPoolManager.java
private static DataSource makePool(UserDBDAO userDB) { GenericObjectPool connectionPool = new GenericObjectPool(); connectionPool.setMaxActive(5);/*from w w w . ja va 2 s . c o m*/ // connectionPool.setWhenExhaustedAction((byte)1); // connectionPool.setMaxWait(1000 * 60); // 1. // connectionPool.setTimeBetweenEvictionRunsMillis(3 * 1000); connectionPool.setTestWhileIdle(true); String passwdDecrypt = ""; try { passwdDecrypt = CipherManager.getInstance().decryption(userDB.getPasswd()); } catch (Exception e) { passwdDecrypt = userDB.getPasswd(); } ConnectionFactory cf = new DriverManagerConnectionFactory(userDB.getUrl(), userDB.getUsers(), passwdDecrypt); PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, connectionPool, null, null, false, true); DataSource ds = new PoolingDataSource(connectionPool); mapDataSource.put(getKey(userDB), ds); return ds; }
From source file:jongo.jdbc.JDBCConnectionFactory.java
/** * Instantiates a new JDBCConnectionFactory if required and creates a connections pool for every database. * @return the instance of the singleton. *///from ww w . j av a 2 s .com private static JDBCConnectionFactory instanceOf() { if (instance == null) { instance = new JDBCConnectionFactory(); for (DatabaseConfiguration db : configuration.getDatabases()) { l.debug("Registering Connection Pool for {}", db.getDatabase()); GenericObjectPool pool = new GenericObjectPool(null, db.getMaxConnections()); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(db.toJdbcURL(), db.getUsername(), db.getPassword()); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, pool, null, null, db.isReadOnly(), true); poolableConnectionFactory.hashCode(); instance.connectionPool.put(db.getDatabase(), pool); } } return instance; }
From source file:com.claresco.tinman.servlet.ConnectionPooling.java
/** * Constructor /* w w w . j av a 2 s .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.keybox.manage.util.DSPool.java
/** * register the data source for H2 DB//from www. j ava2 s . co m * * @return pooling database object */ private static PoolingDataSource registerDataSource() { // create a database connection String user = "keybox"; String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf"; String connectionURI = "jdbc:h2:" + getDBPath() + "/keybox;CIPHER=AES"; String validationQuery = "select 1"; try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException ex) { log.error(ex.toString(), ex); } GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setMaxActive(MAX_ACTIVE); connectionPool.setTestOnBorrow(TEST_ON_BORROW); connectionPool.setMinIdle(MIN_IDLE); connectionPool.setMaxWait(MAX_WAIT); connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true); return new PoolingDataSource(connectionPool); }
From source file:com.markhwood.jndi.EphemeralContext.objectProviders.CommonsDBCPDataSource.java
public Object interpret(String uri, String localName, String qName, Attributes attributes) { // FIXME where does wrapped datasource come in? GenericObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(attributes.getValue("url"), attributes.getValue("user"), attributes.getValue("password")); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); PoolingDataSource dataSource = new PoolingDataSource(connectionPool); return dataSource; }
From source file:de.fct.companian.analyze.helper.DbHelper.java
public static DataSource createDataSource(Properties properties) { String dbDriver;/*www. j av a 2 s . c o m*/ String dbUrl; String dbUser; String dbPass; String dbDriverDefault = "com.mysql.jdbc.Driver"; String dbUrlDefault = "jdbc:mysql://localhost:3306/cpanalyze"; String dbUserDefault = "cpanalyze"; String dbPassDefault = "ezylanapc"; if (properties != null) { dbDriver = properties.getProperty("database.driver"); if (dbDriver == null) { dbDriver = dbDriverDefault; } dbUrl = properties.getProperty("database.url"); if (dbUrl == null) { dbUrl = dbUrlDefault; } dbUser = properties.getProperty("database.username"); if (dbUser == null) { dbUser = dbUserDefault; } dbPass = properties.getProperty("database.password"); if (dbPass == null && dbUser.equals("cpanalyze")) { dbPass = dbPassDefault; } } else { dbDriver = dbDriverDefault; dbUrl = dbUrlDefault; dbUser = dbUserDefault; dbPass = dbPassDefault; } logger.debug("createDataSource() loading underlying JDBC driver."); try { Class.forName(dbDriver); } catch (ClassNotFoundException e) { e.printStackTrace(); } ObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbUrl, dbUser, dbPass); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); PoolingDataSource ds = new PoolingDataSource(connectionPool); if (ds == null) { logger.error("createDataSource() could not create data source"); } return ds; }