Example usage for org.apache.commons.dbcp2 DriverManagerConnectionFactory DriverManagerConnectionFactory

List of usage examples for org.apache.commons.dbcp2 DriverManagerConnectionFactory DriverManagerConnectionFactory

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 DriverManagerConnectionFactory DriverManagerConnectionFactory.

Prototype

public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) 

Source Link

Document

Constructor for DriverManagerConnectionFactory.

Usage

From source file:com.linuxrouter.netcool.test.DbPoolTest.java

public static void main(String[] args) {
    String host = "192.168.0.201";
    String port = "4100";
    String dbName = "alerts";
    String url = "jdbc:sybase:Tds:" + host + ":" + port + "/" + dbName;
    Driver drv = new com.sybase.jdbc3.jdbc.SybDriver();
    try {/*from   w w  w  . j  a  v a 2s  .  c o  m*/
        DriverManager.registerDriver(drv);
    } catch (SQLException ex) {
        Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, "root", "omni12@#");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(connectionPool);
    try {
        Connection con = poolingDataSource.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from alerts.status");
        int x = 0;
        while (rs.next()) {
            //System.out.println(":::" + rs.getString(1));
            x++;
        }
        System.out.println("::::::" + x);
    } catch (SQLException ex) {
        Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.parallax.server.blocklyprop.db.utils.DataSourceSetup.java

public static PoolingDataSource connect(Configuration configuration) throws ClassNotFoundException {
    String driver = configuration.getString("database.driver");
    String url = configuration.getString("database.url");
    String username = configuration.getString("database.username");
    String password = configuration.getString("database.password");

    Class.forName(driver);// w  ww .j ava  2 s  .c  om

    //
    // First, 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(url, username, password);

    //
    // Next 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,
            null);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    poolableConnectionFactory.setMaxConnLifetimeMillis(5000);

    //
    // Now 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<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSourceInstance = new PoolingDataSource<>(connectionPool);

    for (NeedsDataSource dataSourceUser : dataSourceUsers) {
        dataSourceUser.setDataSource(dataSourceInstance);
    }
    DataSourceSetup.dataSource = dataSourceInstance;
    return dataSourceInstance;
}

From source file:com.mirth.connect.donkey.server.data.jdbc.DBCPConnectionPool.java

public DBCPConnectionPool(String url, String username, String password, int maxConnections) {
    this.maxConnections = maxConnections;

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);/*from   www . j a  v a 2  s.c o m*/
    poolableConnectionFactory.setDefaultAutoCommit(false);

    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    connectionPool.setMaxTotal(maxConnections);
    connectionPool.setMaxIdle(maxConnections);

    poolableConnectionFactory.setPool(connectionPool);

    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<PoolableConnection>(
            connectionPool);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    this.dataSource = dataSource;
}

From source file:hu.neuron.java.jdbc.PoolingDataSourceExample.java

private static PoolingDataSource<PoolableConnection> setupDataSource() {
    ////w w  w .j av a  2s  .  com
    // First, 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(URL, USER, PASS);

    //
    // Next 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,
            null);
    connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    connectionPool.setMaxTotal(10);
    connectionPool.setMinIdle(20);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:com.ebay.pulsar.analytics.dao.DBFactory.java

public static void setDs(BasicDataSource datasource) {
    ////www  .j a  v  a2 s  . com
    // First, 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.
    //
    try {
        Class.forName(datasource.getDriverClassName());
    } catch (ClassNotFoundException e) {
    }
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(datasource.getUrl(),
            datasource.getUsername(), datasource.getPassword());

    //
    // Next 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,
            null);

    //
    // Now 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<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> poolingDS = new PoolingDataSource<>(connectionPool);
    ds = poolingDS;
}

From source file:com.tealcube.minecraft.bukkit.facecore.database.MySqlDatabasePool.java

@Override
public boolean initialize() {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);// w w  w . java2 s  .c  o m
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    poolingDataSource = new PoolingDataSource<>(connectionPool);
    return true;
}

From source file:com.xtesoft.xtecuannet.framework.templater.filler.utils.SQLScanner.java

private DataSource setupDataSource() {
    //Loading driver
    loadDriver();//from w  w w .  j a v  a  2 s. c om
    //
    // First, 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(url, user, pass);

    //
    // Next 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,
            null);

    //
    // Now 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<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource1 = new PoolingDataSource<>(connectionPool);

    return dataSource1;
}

From source file:io.seldon.dbcp.DbcpFactory.java

private void createDbcp(DbcpConfig conf) {
    if (!dataSources.containsKey(conf.name)) {
        try {/*from  ww w . ja  v a 2s  .c o  m*/

            Class.forName(conf.driverClassName);

            DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.jdbc, conf.user,
                    conf.password);

            PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);
            pcf.setValidationQuery(conf.validationQuery);
            //, pool, null, conf.validationQuery, false, true,abandondedConfig);

            logger.info("Creating pool " + conf.toString());
            // create a generic pool
            GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf);
            pool.setMaxTotal(conf.maxTotal);
            pool.setMaxIdle(conf.maxIdle);
            pool.setMinIdle(conf.minIdle);
            pool.setMaxWaitMillis(conf.maxWait);
            pool.setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
            pool.setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
            pool.setTestWhileIdle(conf.testWhileIdle);
            pool.setTestOnBorrow(conf.testOnBorrow);

            AbandonedConfig abandonedConfig = new AbandonedConfig();
            abandonedConfig.setRemoveAbandonedOnMaintenance(conf.removeAbanadoned);
            abandonedConfig.setRemoveAbandonedTimeout(conf.removeAbandonedTimeout);
            abandonedConfig.setLogAbandoned(conf.logAbandonded);

            pool.setAbandonedConfig(abandonedConfig);

            pcf.setPool(pool);
            DataSource ds = new PoolingDataSource(pool);
            dataSources.put(conf.name, ds);

        } catch (ClassNotFoundException e) {
            logger.error(
                    "Failed to create datasource for " + conf.name + " with class " + conf.driverClassName);
        }

    } else {
        logger.error("Pool " + conf.name + " already exists. Can't change existing datasource at present.");
    }
}

From source file:ddc.commons.jdbc.PooledDatasourceFactory.java

public DataSource createDataSource(JdbcConnectionFactory conn) throws ClassNotFoundException {
    conn.loadDriver();/*  www  .  j av a 2s  .  com*/
    //
    // First, 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(conn.getUrl(), conn.getUser(),
            conn.getPassword());

    //
    // Next 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,
            null);

    //
    // Now 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<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    DataSource ds = new PoolingDataSource<PoolableConnection>(connectionPool);
    return ds;
}

From source file:ch.ethz.coss.nervous.pulse.sql.SqlConnection.java

private DataSource setup() {

    try {/* w w  w  . j  ava 2s .  c o m*/
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        Log.getInstance().append(Log.FLAG_ERROR, "Error loading the SQL driver");
        return null;
    }

    ConnectionFactory cf = null;
    try {
        cf = new DriverManagerConnectionFactory("jdbc:mysql://" + hostname + ":" + port + "/" + database,
                username, password);
        // System.out.println("CF - " + cf.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);

    ObjectPool<PoolableConnection> connPool = new GenericObjectPool<PoolableConnection>(pcf);

    pcf.setPool(connPool);
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<PoolableConnection>(connPool);
    // System.out.println("DataSource -- " + dataSource);
    return dataSource;
}