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, Properties props) 

Source Link

Document

Constructor for DriverManagerConnectionFactory.

Usage

From source file:com.yahoo.athenz.common.server.db.DataSourceFactory.java

public static PoolableDataSource create(String url, Properties mysqlConnectionProperties) {

    String driver = null;/*from  w w w. j a  v  a  2 s . c o m*/
    try {
        if (url.indexOf(":mysql:") > 0) {

            driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);

            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url,
                    mysqlConnectionProperties);

            return create(connectionFactory);

        } else {
            throw new RuntimeException("Cannot figure out how to instantiate this data source: " + url);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot load driver class: " + driver);
    } catch (Exception exc) {
        throw new RuntimeException("Failed to create database source(" + url + ") with driver(" + driver + ")",
                exc);
    }
}

From source file:br.com.hslife.orcamento.repository.ConnectionFactory.java

private ConnectionFactory() {
    try {/*from  www.  j  ava  2s  .com*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        e.printStackTrace();
    }

    Properties properties = new Properties();
    properties.setProperty("user", "orcamento");
    properties.setProperty("password", "d1nh31r0"); // or get properties from some configuration file

    DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://localhost:3306/orcamento", properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);

    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    this.dataSource = dataSource;
}

From source file:io.dockstore.common.BasicPostgreSQL.java

public BasicPostgreSQL(HierarchicalINIConfiguration settings) {
    if (dataSource == null) {
        try {/* w  w w . j av  a  2s.c o  m*/
            String nullConfigs = "";
            String host = settings.getString(Constants.POSTGRES_HOST);
            if (host == null) {
                nullConfigs += "postgresHost ";
            }

            String user = settings.getString(Constants.POSTGRES_USERNAME);
            if (user == null) {
                nullConfigs += "postgresUser ";
            }

            String pass = settings.getString(Constants.POSTGRES_PASSWORD);
            if (pass == null) {
                nullConfigs += "postgresPass ";
            }

            String db = settings.getString(Constants.POSTGRES_DBNAME);
            if (db == null) {
                nullConfigs += "postgresDBName ";
            }

            String maxConnections = settings.getString(Constants.POSTGRES_MAX_CONNECTIONS, "5");

            if (!nullConfigs.trim().isEmpty()) {
                throw new NullPointerException("The following configuration values are null: " + nullConfigs
                        + ". Please check your configuration file.");
            }

            Class.forName("org.postgresql.Driver");

            String url = "jdbc:postgresql://" + host + "/" + db;
            LOG.debug("PostgreSQL URL is: " + url);
            Properties props = new Properties();
            props.setProperty("user", user);
            props.setProperty("password", pass);
            // props.setProperty("ssl","true");
            props.setProperty("initialSize", "5");
            props.setProperty("maxActive", maxConnections);

            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, props);
            PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                    connectionFactory, null);
            poolableConnectionFactory.setValidationQuery("select count(*) from container;");
            ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
            poolableConnectionFactory.setPool(connectionPool);
            dataSource = new PoolingDataSource<>(connectionPool);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:kr.co.bitnine.octopus.frame.ConnectionManager.java

private ObjectPool<PoolableConnection> createPool(String connectionString) {
    // A ConnectionFactory that the pool will use to create Connections.
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionString, null);
    // PoolableConnectionFactory wraps the real Connections with the
    // classes that implement the pooling functionality.
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);//from   w  w  w.  j  a v  a 2 s  .c o m
    poolableConnectionFactory.setValidationQuery("SELECT 1");

    // Actual pool of connections.
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    int connMax = getConfig().getInt(OctopusConfiguration.MASTER_CONNECTION_POOL_MAX, 8);
    connectionPool.setMaxTotal(connMax);
    connectionPool.setTestOnBorrow(true);
    // Set the factory's pool property to the owning pool.
    poolableConnectionFactory.setPool(connectionPool);

    return connectionPool;
}

From source file:edumsg.core.PostgresConnection.java

public static void initSource() {
    try {//from   w w  w . j  a  va 2  s  .co  m
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
            LOGGER.log(Level.SEVERE, "Error loading Postgres driver: " + ex.getMessage(), ex);
        }
        try {
            readConfFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Properties props = new Properties();
        //  System.out.println(DB_USERNAME);
        props.setProperty("user", DB_USERNAME);
        props.setProperty("password", DB_PASSWORD);
        props.setProperty("initialSize", DB_INIT_CONNECTIONS);
        props.setProperty("maxActive", DB_MAX_CONNECTIONS);

        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DB_URL, props);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                null);
        poolableConnectionFactory.setPoolStatements(true);

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(DB_INIT_CONNECTIONS));
        poolConfig.setMaxTotal(Integer.parseInt(DB_MAX_CONNECTIONS));
        ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
                poolConfig);
        poolableConnectionFactory.setPool(connectionPool);

        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        dbDriver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        dbDriver.registerPool(DB_NAME, connectionPool);

        dataSource = new PoolingDataSource<>(connectionPool);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Got error initializing data source: " + ex.getMessage(), ex);
    }
}

From source file:dgw.mt940.db.util.PoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    //// w  ww. j  a va2  s. c  o m
    // 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(connectURI, null);

    //
    // 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.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:PoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ////w w w .  ja  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(connectURI, null);

    //
    // 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> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:info.pancancer.arch3.persistence.PostgreSQL.java

public PostgreSQL(HierarchicalINIConfiguration settings) {
    if (dataSource == null) {
        try {//from  w ww.  j a v  a  2s.  c om
            String nullConfigs = "";
            String host = settings.getString(Constants.POSTGRES_HOST);
            if (host == null) {
                nullConfigs += "postgresHost ";
            }

            String user = settings.getString(Constants.POSTGRES_USERNAME);
            if (user == null) {
                nullConfigs += "postgresUser ";
            }

            String pass = settings.getString(Constants.POSTGRES_PASSWORD);
            if (pass == null) {
                nullConfigs += "postgresPass ";
            }

            String db = settings.getString(Constants.POSTGRES_DBNAME);
            if (db == null) {
                nullConfigs += "postgresDBName ";
            }

            String maxConnections = settings.getString(Constants.POSTGRES_MAX_CONNECTIONS, "5");

            if (nullConfigs.trim().length() > 0) {
                throw new NullPointerException("The following configuration values are null: " + nullConfigs
                        + ". Please check your configuration file.");
            }

            Class.forName("org.postgresql.Driver");

            String url = "jdbc:postgresql://" + host + "/" + db;
            LOG.debug("PostgreSQL URL is: " + url);
            Properties props = new Properties();
            props.setProperty("user", user);
            props.setProperty("password", pass);
            // props.setProperty("ssl","true");
            props.setProperty("initialSize", "5");
            props.setProperty("maxActive", maxConnections);

            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, props);
            PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                    connectionFactory, null);
            poolableConnectionFactory.setValidationQuery("select count(*) from job;");
            ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
            poolableConnectionFactory.setPool(connectionPool);
            dataSource = new PoolingDataSource<>(connectionPool);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:PoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ///*  w w  w  . j a  v  a 2 s.  c  o  m*/
    // 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(connectURI, null);

    //
    // 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...
    //
    Class.forName("org.apache.commons.dbcp2.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:JDBCPool.dbcp.demo.offical.PoolingDriverExample.java

/**
 * ?connectURI?PoolingDriver/*from   w w w  .  j  ava 2  s.c o  m*/
 * @param connectURI
 * @throws Exception
 */
public static void setupDriver(String connectURI) throws Exception {

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

    // 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...
    //
    Class.forName("org.apache.commons.dbcp2.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.
    //
}