org.apache.commons.dbcp.PoolableConnectionFactory Java Examples
The following examples show how to use
org.apache.commons.dbcp.PoolableConnectionFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestPersistence.java From development with Apache License 2.0 | 6 votes |
private DataSource createManagedDataSource(DataSource ds) { // wrap it with a LocalXAConnectionFactory XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory( transactionManager, new DataSourceConnectionFactory(ds)); GenericObjectPool pool = new GenericObjectPool(); // create the pool object factory PoolableConnectionFactory factory = new PoolableConnectionFactory( xaConnectionFactory, pool, null, "SELECT DUMMY FROM DUAL", false, false); pool.setFactory(factory); ManagedDataSource managedDs = new ManagedDataSource(pool, xaConnectionFactory.getTransactionRegistry()); managedDs.setAccessToUnderlyingConnectionAllowed(true); return managedDs; }
Example #2
Source File: DBase.java From openemm with GNU Affero General Public License v3.0 | 5 votes |
@Override public DataSource newDataSource (String driver, String connect, String login, String password) { ObjectPool <Object> connectionPool = new GenericObjectPool<>(null, dsPoolsize, (dsPoolgrow ? GenericObjectPool.WHEN_EXHAUSTED_GROW : GenericObjectPool.WHEN_EXHAUSTED_BLOCK), 0); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory (connect, login, password); // THIS IS REQUIRED DUE TO INTERNAL SIDE EFFECTS, DO NOT REMOVE THIS (AGAIN)! @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory (connectionFactory, connectionPool, null, null, false, true); log.out (Log.INFO, "nds", "New data source for " + driver + " using " + connect + " with " + login + " created (poolsize " + dsPoolsize + ", grow is " + dsPoolgrow + ")"); return new PoolingDataSource (connectionPool); }
Example #3
Source File: DataSourceProviderDbcpImpl.java From ralasafe with MIT License | 5 votes |
public void setup( Properties prop ) { this.prop=prop; GenericObjectPool.Config conPoolCfg = new GenericObjectPool.Config(); conPoolCfg.maxActive = Integer.parseInt( prop.getProperty( "connectionPoolMaxSize", "15" ) ); conPoolCfg.maxIdle = Integer.parseInt( prop.getProperty( "connectionPoolMaxIdle", "8" ) ); conPoolCfg.maxWait = Integer.parseInt( prop.getProperty( "connectionPoolMaxWait", "60000" ) ); conPoolCfg.minIdle = Integer.parseInt( prop.getProperty( "connectionPoolMinSize", "2" ) ); ObjectPool connectionPool = new GenericObjectPool( null, conPoolCfg ); try { Class.forName( prop.getProperty( "jdbcDriver" ) ); } catch( ClassNotFoundException e ) { e.printStackTrace(); throw new RuntimeException(); } ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( prop.getProperty( "jdbcUrl" ), prop.getProperty( "jdbcUser" ), prop.getProperty( "jdbcPassword" ) ); new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); PoolingDataSource dataSource = new PoolingDataSource(connectionPool); ds = dataSource; }
Example #4
Source File: DBUtils.java From tribaltrouble with GNU General Public License v2.0 | 5 votes |
public final static void initConnection(String address, String user, String password) throws ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); GenericObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(address, user, password); KeyedObjectPoolFactory keyed_factory = new StackKeyedObjectPoolFactory(); new PoolableConnectionFactory(connectionFactory, connectionPool, keyed_factory, null, false, true); ds = new PoolingDataSource(connectionPool); }
Example #5
Source File: DatabaseConnectionBuilder.java From oodt with Apache License 2.0 | 5 votes |
public static DataSource buildDataSource(String user, String pass, String driver, String url) { DataSource ds; try { Class.forName(driver); } catch (ClassNotFoundException ignore) { } GenericObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( url, user, pass); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, connectionPool, null, null, false, true); return new PoolingDataSource(connectionPool); }
Example #6
Source File: DataSourceWorkflowRepositoryFactory.java From oodt with Apache License 2.0 | 5 votes |
/** * <p> * Default Constructor * </p>. */ public DataSourceWorkflowRepositoryFactory() throws WorkflowException { String jdbcUrl, user, pass, driver; jdbcUrl = System .getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.url"); user = System .getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.user"); pass = System .getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.pass"); driver = System .getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.driver"); try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new WorkflowException("Cannot load driver: " + driver); } GenericObjectPool connectionPool = new GenericObjectPool(null); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( jdbcUrl, user, pass); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, connectionPool, null, null, false, true); dataSource = new PoolingDataSource(connectionPool); }
Example #7
Source File: JDBCConnectionFactory.java From amforeas with GNU General Public License v3.0 | 5 votes |
/** * Instantiates a new JDBCConnectionFactory if required and creates a connections pool for every database. * @return the instance of the singleton. */ public void load () { 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(); this.connectionPool.put(db.getDatabase(), pool); } }
Example #8
Source File: ElexisPoolingDataSource.java From elexis-3-core with Eclipse Public License 1.0 | 5 votes |
private ObjectPool<Connection> createConnectionPool(DBConnection dbConnection) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ String driverName = StringUtils.defaultString(dbConnection.rdbmsType.driverName); String username = StringUtils.defaultString(dbConnection.username); String password = StringUtils.defaultString(dbConnection.password); String jdbcString = StringUtils.defaultString(dbConnection.connectionString); Driver driver = (Driver) Class.forName(driverName).newInstance(); Properties properties = new Properties(); properties.put("user", username); properties.put("password", password); log.info("db connection pool [" + driver + ", " + jdbcString + ", " + username + "] initialization"); ConnectionFactory connectionFactory = new DriverConnectionFactory(driver, jdbcString, properties); GenericObjectPool<Connection> connectionPool = new GenericObjectPool<>(null); connectionPool.setMaxActive(32); connectionPool.setMinIdle(8); connectionPool.setMaxWait(10000); connectionPool.setTestOnBorrow(true); new PoolableConnectionFactory(connectionFactory, connectionPool, null, "SELECT 1;", false, true); return connectionPool; }
Example #9
Source File: PoolConnectionAccessor.java From neoscada with Eclipse Public License 1.0 | 4 votes |
public PoolConnectionAccessor ( final DataSourceFactory dataSourceFactory, final Properties paramProperties ) throws SQLException { logger.debug ( "Creating pool connection accessor : {}", paramProperties ); // first remove all our properties this.connectionPool = new GenericObjectPool<Object> ( null ); this.connectionPool.setMaxActive ( getInteger ( paramProperties, PREFIX + "maxActive", 8 ) ); this.connectionPool.setMaxIdle ( getInteger ( paramProperties, PREFIX + "maxIdle", 8 ) ); this.connectionPool.setMinIdle ( getInteger ( paramProperties, PREFIX + "minIdle", 1 ) ); this.connectionPool.setTestOnBorrow ( getBoolean ( paramProperties, PREFIX + "testOnBorrow", true ) ); this.connectionPool.setTestOnReturn ( getBoolean ( paramProperties, PREFIX + "testOnReturn", true ) ); this.connectionPool.setTimeBetweenEvictionRunsMillis ( getLong ( paramProperties, PREFIX + "timeBetweenEvictionRunsMillis", -1 ) ); this.connectionPool.setMinEvictableIdleTimeMillis ( getLong ( paramProperties, PREFIX + "minEvictableIdleTimeMillis", 30 * 60 * 1000 ) ); this.connectionPool.setTestWhileIdle ( getBoolean ( paramProperties, PREFIX + "testWhileIdle", false ) ); this.connectionPool.setSoftMinEvictableIdleTimeMillis ( getLong ( paramProperties, PREFIX + "softMinEvictableIdleTimeMillis", -1 ) ); this.connectionPool.setNumTestsPerEvictionRun ( getInteger ( paramProperties, PREFIX + "numTestsPerEvictionRun", 3 ) ); final String connectionInitSql = getString ( paramProperties, PREFIX + "connectionInitSql", null ); final String validationQuery = getString ( paramProperties, PREFIX + "validationQuery", null ); final Integer validationQueryTimeout = getInteger ( paramProperties, PREFIX + "validationQueryTimeout", -1 ); this.driverDataSource = dataSourceFactory.createDataSource ( paramProperties ); final ConnectionFactory connectionFactory = new DataSourceConnectionFactory ( this.driverDataSource ); this.poolableConnectionFactory = new PoolableConnectionFactory ( connectionFactory, this.connectionPool, null, null, false, true ); if ( connectionInitSql != null ) { this.poolableConnectionFactory.setConnectionInitSql ( Arrays.asList ( connectionInitSql ) ); } if ( validationQuery != null ) { this.poolableConnectionFactory.setValidationQuery ( validationQuery ); } if ( validationQueryTimeout != null ) { this.poolableConnectionFactory.setValidationQueryTimeout ( validationQueryTimeout ); } this.dataSource = new PoolingDataSource ( this.connectionPool ); }
Example #10
Source File: DbWrapper.java From wisp with Apache License 2.0 | 4 votes |
private DataSource setupDataSource(String driver, String connectURI, String userName, String passwd) throws ClassNotFoundException { // driver Class.forName(driver); // // 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); // 设置在getConnection时验证Connection是否有效 connectionPool.setTestOnBorrow(true); // // 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, userName, passwd); // null can be used as parameter because this parameter is set in // PoolableConnectionFactory when creating a new PoolableConnection KeyedObjectPoolFactory statementPool = new GenericKeyedObjectPoolFactory( null); // // 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, statementPool, "select now()", false, true); // // Finally, we create the PoolingDriver itself, // passing in the object pool we created. // PoolingDataSource dataSource = new PoolingDataSource(connectionPool); return dataSource; }
Example #11
Source File: JdbcRepositoryProvider.java From sqoop-on-spark with Apache License 2.0 | 4 votes |
@Override public void configurationChanged() { LOG.info("Begin JdbcRepository reconfiguring."); JdbcRepositoryContext oldRepoContext = repoContext; repoContext = new JdbcRepositoryContext(SqoopConfiguration.getInstance().getContext()); // reconfigure jdbc handler String newJdbcHandlerClassName = repoContext.getHandlerClassName(); if (newJdbcHandlerClassName == null || newJdbcHandlerClassName.trim().length() == 0) { throw new SqoopException(RepositoryError.JDBCREPO_0001, newJdbcHandlerClassName); } String oldJdbcHandlerClassName = oldRepoContext.getHandlerClassName(); if (!newJdbcHandlerClassName.equals(oldJdbcHandlerClassName)) { LOG.warn("Repository JDBC handler cannot be replaced at the runtime. " + "You might need to restart the server."); } // reconfigure jdbc driver String newJdbcDriverClassName = repoContext.getDriverClass(); if (newJdbcDriverClassName == null || newJdbcDriverClassName.trim().length() == 0) { throw new SqoopException(RepositoryError.JDBCREPO_0003, newJdbcDriverClassName); } String oldJdbcDriverClassName = oldRepoContext.getDriverClass(); if (!newJdbcDriverClassName.equals(oldJdbcDriverClassName)) { LOG.warn("Repository JDBC driver cannot be replaced at the runtime. " + "You might need to restart the server."); } // reconfigure max connection connectionPool.setMaxActive(repoContext.getMaximumConnections()); // reconfigure the url of repository String connectUrl = repoContext.getConnectionUrl(); String oldurl = oldRepoContext.getConnectionUrl(); if (connectUrl != null && !connectUrl.equals(oldurl)) { LOG.warn("Repository URL cannot be replaced at the runtime. " + "You might need to restart the server."); } // if connection properties or transaction isolation option changes boolean connFactoryChanged = false; // compare connection properties if (!connFactoryChanged) { Properties oldProp = oldRepoContext.getConnectionProperties(); Properties newProp = repoContext.getConnectionProperties(); if (newProp.size() != oldProp.size()) { connFactoryChanged = true; } else { for (Object key : newProp.keySet()) { if (!newProp.getProperty((String) key).equals(oldProp.getProperty((String) key))) { connFactoryChanged = true; break; } } } } // compare the transaction isolation option if (!connFactoryChanged) { String oldTxOption = oldRepoContext.getTransactionIsolation().toString(); String newTxOption = repoContext.getTransactionIsolation().toString(); if (!newTxOption.equals(oldTxOption)) { connFactoryChanged = true; } } if (connFactoryChanged) { // try to reconfigure connection factory try { LOG.info("Reconfiguring Connection Factory."); Properties jdbcProps = repoContext.getConnectionProperties(); ConnectionFactory connFactory = new DriverManagerConnectionFactory(connectUrl, jdbcProps); new PoolableConnectionFactory(connFactory, connectionPool, statementPool, handler.validationQuery(), false, false, repoContext.getTransactionIsolation().getCode()); } catch (IllegalStateException ex) { // failed to reconfigure connection factory LOG.warn("Repository connection cannot be reconfigured currently. " + "You might need to restart the server."); } } // ignore the create schema option, because the repo url is not allowed to change LOG.info("JdbcRepository reconfigured."); }
Example #12
Source File: DbcpPoolDataSource.java From XPagesExtensionLibrary with Apache License 2.0 | 4 votes |
/** * */ public DbcpPoolDataSource(final String dataSourceName, final String driverClass, final String url, final String username, final String password, final int minIdle, final int maxIdle, final int maxActive, final long maxWait) throws PoolException{ try { ds = AccessController.doPrivileged( new PrivilegedExceptionAction<DataSource>() { public DataSource run() throws Exception { // create a driver connection factory driver = JDBCDriverLoader.loadDriver(driverClass); Properties properties = new Properties(); properties.setProperty("user", username); // $NON-NLS-1$ properties.setProperty("password", (StringUtil.isEmpty(password) ? "":password)); // $NON-NLS-1$ ConnectionFactory connectionFactory = new DriverConnectionFactory(driver, url, properties); // create the pool pool = new GenericObjectPool(); pool.setMaxActive(maxActive); pool.setMaxWait(maxWait); pool.setMaxIdle(maxIdle); pool.setMinIdle(minIdle); // create the pool object factory PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null, null, false, true); pool.setFactory(factory); // finally create the datasource PoolingDataSource bds = new PoolingDataSource(pool); return bds; } }); } catch (Exception e) { String msg = "Unable to initialize the shared connection pool DataSource"; // $NLX-DbcpPoolDataSource.Unabletoinitializethesharedconnec-1$[["connection pool" is a technical term related to databases]] // Note, this resource key is used elsewhere in this plugin // "Unable to initialize the shared connection pool DataSource" //String msg = com.ibm.xsp.extlib.relational.ResourceHandler.getSpecialAudienceString("DbcpPoolDataSource.Unabletoinitializethesharedconnec");//$NON-NLS-1$ throw new PoolException( e, msg); } }
Example #13
Source File: DB.java From jelectrum with MIT License | 3 votes |
public static void openConnectionPool(String pool_name, String driver_class, String uri, String user, String pass, int max_active, int max_idle) throws SQLException { Properties props = new Properties(); props.put("autoReconnect","true"); props.put("user",user); props.put("password",pass); props.put("socketTimeout", "15"); loadDriver(driver_class); loadDriver("org.apache.commons.dbcp.PoolingDriver"); GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setMaxActive(max_active); connectionPool.setMaxIdle(max_idle); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, props); new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); driver.registerPool(pool_name,connectionPool); }