Example usage for org.apache.commons.dbcp BasicDataSource BasicDataSource

List of usage examples for org.apache.commons.dbcp BasicDataSource BasicDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource BasicDataSource.

Prototype

BasicDataSource

Source Link

Usage

From source file:com.ibatis.common.jdbc.DbcpConfiguration.java

private BasicDataSource legacyDbcpConfiguration(Map map) {
    BasicDataSource basicDataSource = null;
    if (map.containsKey("JDBC.Driver")) {
        basicDataSource = new BasicDataSource();
        String driver = (String) map.get("JDBC.Driver");
        String url = (String) map.get("JDBC.ConnectionURL");
        String username = (String) map.get("JDBC.Username");
        String password = (String) map.get("JDBC.Password");
        String validationQuery = (String) map.get("Pool.ValidationQuery");
        String maxActive = (String) map.get("Pool.MaximumActiveConnections");
        String maxIdle = (String) map.get("Pool.MaximumIdleConnections");
        String maxWait = (String) map.get("Pool.MaximumWait");

        basicDataSource.setUrl(url);/*ww w .j a v  a 2  s .  com*/
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);

        if (notEmpty(validationQuery)) {
            basicDataSource.setValidationQuery(validationQuery);
        }

        if (notEmpty(maxActive)) {
            basicDataSource.setMaxActive(Integer.parseInt(maxActive));
        }

        if (notEmpty(maxIdle)) {
            basicDataSource.setMaxIdle(Integer.parseInt(maxIdle));
        }

        if (notEmpty(maxWait)) {
            basicDataSource.setMaxWait(Integer.parseInt(maxWait));
        }

        Iterator props = map.keySet().iterator();
        while (props.hasNext()) {
            String propertyName = (String) props.next();
            if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
                String value = (String) map.get(propertyName);
                basicDataSource.addConnectionProperty(propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH),
                        value);
            }
        }
    }
    return basicDataSource;
}

From source file:database.DataSourceBase.java

public DataSource initDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driver);/*w ww.  ja  v  a  2 s .c  om*/
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setUrl(url);
    ds.setMaxActive(maxActive);

    datasource = ds;
    return ds;
}

From source file:com.googlecode.wmbutil.jdbc.DataSourceLocator.java

/**
 * Looks up a named data source/*from  www .  j a  va2  s  .  c om*/
 * 
 * @param dataSourceName
 *            The name of the data source to look for
 * @return The data source
 * @throws RuntimeException
 *             If the configuration can not be loaded or the data source
 *             definition is missing
 */
public synchronized DataSource lookup(String dataSourceName) {
    BasicDataSource ds;
    if (dataSources.containsKey(dataSourceName)) {
        ds = (BasicDataSource) dataSources.get(dataSourceName);

        if (ds == null) {
            // we failed to create it earlier
            throw new RuntimeException("Failed to create data source");
        }
    } else {
        ds = new BasicDataSource();

        try {
            String driver = config.getProperty("jdbc." + dataSourceName + ".class");
            if (driver == null || driver.trim().length() == 0) {
                throw new RuntimeException("Lookup connections configuration file must contain a jdbc."
                        + dataSourceName + ".class value");
            } else {
                ds.setDriverClassName(driver);
            }

            String url = config.getProperty("jdbc." + dataSourceName + ".url");
            if (url == null || url.trim().length() == 0) {
                throw new RuntimeException("Lookup connections configuration file must contain a jdbc."
                        + dataSourceName + ".url value");
            } else {
                ds.setUrl(url);
            }

            String username = config.getProperty("jdbc." + dataSourceName + ".username");
            if (username == null || username.trim().length() == 0) {
                throw new RuntimeException("Lookup connections configuration file must contain a jdbc."
                        + dataSourceName + ".username value");
            } else {
                ds.setUsername(username);
            }

            ds.setPassword(config.getProperty("jdbc." + dataSourceName + ".password"));
            ds.setDefaultReadOnly(true);

            dataSources.put(dataSourceName, ds);
        } catch (RuntimeException e) {
            // mark failed to create
            dataSources.put(dataSourceName, null);
            throw e;
        }
    }

    return ds;

}

From source file:io.springagora.store.ApplicationConfig.java

/**
 * Bean definition./*w w w  .  jav a2s  .co  m*/
 * 
 * Datasource used to retrieve connections for the persistence layer. It's
 * configured following definitions written in {@code application.properties}.
 * 
 * @return
 *      The {@code Datasource} object, that will act as the connection pool,
 *      providing connections to the application. 
 * 
 * @throws SQLException
 *      If any error occurs during the connection attempt, it will be thrown
 *      as a {@code SQLException}.
 */
@Bean
public DataSource dataSource() throws SQLException {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl(connectionUrl);
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setInitialSize(5);
    ds.setMaxActive(20);
    ds.setDefaultAutoCommit(false);
    return ds;
}

From source file:cz.muni.fi.pv168.MainForm.java

private DataSource prepareDataSource() {
    /*//www.j av a 2 s .c  om
     * BasicDataSource ds = new BasicDataSource(); Properties properties =
     * new Properties(); FileInputStream in = null; try { in = new
     * FileInputStream("database.properties"); } catch
     * (FileNotFoundException ex) {
     * Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, "File
     * Not Found", ex); } try { properties.load(in); } catch (IOException
     * ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE,
     * "Can Not Read Properties File", ex); } String drivers =
     * properties.getProperty("jdbc.drivers"); if (null == drivers) {
     * System.setProperty("jdbs.drivers", drivers); } String url =
     * properties.getProperty("jdbc.url"); String username =
     * properties.getProperty("jdbc.username"); String password =
     * properties.getProperty("jdbc.password"); ds.setUrl(url);
     * ds.setUsername(username); ds.setPassword(password); return ds;
     */

    ResourceBundle databaseProperties = ResourceBundle.getBundle("cz.muni.fi.pv168.database");

    String url = databaseProperties.getString("jdbc.url");
    String username = databaseProperties.getString("jdbc.username");
    String password = databaseProperties.getString("jdbc.password");

    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);

    try {
        DBUtils.tryCreateTables(ds);
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(jMenu1, localization.getString("db_connection_failure"),
                localization.getString("error"), JOptionPane.ERROR_MESSAGE);
    }

    carManager.setDataSource(ds);
    customerManager.setDataSource(ds);
    rentManager.setDataSource(ds);

    return ds;
}

From source file:binky.reportrunner.service.impl.DatasourceServiceImpl.java

private DataSource getDs(RunnerDataSource runnerDs)
        throws SecurityException, InstantiationException, IllegalAccessException, ClassNotFoundException,
        PropertyVetoException, NamingException, EncryptionException {

    final String jndiDataSource = runnerDs.getJndiName();

    if (StringUtils.isBlank(jndiDataSource)) {
        EncryptionUtil enc = new EncryptionUtil();
        logger.info("using dbcp pooled connection for: " + runnerDs.getDataSourceName());

        String jdbcUser = runnerDs.getUsername();
        if (StringUtils.isBlank(runnerDs.getPassword()))
            throw new SecurityException("password is empty");
        String jdbcPassword = enc.decrpyt(secureKey, runnerDs.getPassword());

        String jdbcUrl = runnerDs.getJdbcUrl();
        String databaseDriver = runnerDs.getJdbcClass();

        Class.forName(databaseDriver).newInstance();

        BasicDataSource ds1 = new BasicDataSource();
        ds1.setDriverClassName(databaseDriver);
        ds1.setUrl(jdbcUrl);//  w  w  w  .ja  va  2s.com
        ds1.setUsername(jdbcUser);
        ds1.setPassword(jdbcPassword);
        ds1.setInitialSize(runnerDs.getInitialPoolSize());
        ds1.setMaxActive(runnerDs.getMaxPoolSize());

        ds1.setRemoveAbandoned(true);
        ds1.setRemoveAbandonedTimeout(600);

        // do not want anything updating anything
        ds1.setDefaultReadOnly(true);

        ds1.setLogAbandoned(true);
        ds1.setTestOnBorrow(true);
        ds1.setTestOnReturn(true);
        ds1.setTestWhileIdle(true);

        // does this work across all RBMS? - no it doesn't
        //ds1.setValidationQuery("select 1");
        //ds1.setValidationQueryTimeout(300);

        return ds1;
    } else {
        logger.info(
                "getting datasource from JNDI url: " + jndiDataSource + " for " + runnerDs.getDataSourceName());
        Context initContext = new InitialContext();
        DataSource ds = (DataSource) initContext.lookup("java:/comp/env/" + jndiDataSource);
        return ds;
    }
}

From source file:io.apiman.manager.test.server.ManagerApiTestServer.java

/**
 * Creates an in-memory datasource.//from   w w  w.  j  av a  2s. c o m
 * @throws SQLException
 */
private static BasicDataSource createInMemoryDatasource() throws SQLException {
    System.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); //$NON-NLS-1$ //$NON-NLS-2$
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa"); //$NON-NLS-1$
    ds.setPassword(""); //$NON-NLS-1$
    ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); //$NON-NLS-1$
    Connection connection = ds.getConnection();
    connection.close();
    System.out.println("DataSource created and bound to JNDI."); //$NON-NLS-1$
    return ds;
}

From source file:br.com.proj.web.config.WebMvcConfig.java

@Bean
public DataSource dataSource() {
    System.out.println("dataSource");
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getProperty("db.driverClassName"));
    dataSource.setUrl(env.getProperty("db.url"));
    dataSource.setUsername(env.getProperty("db.username"));
    dataSource.setPassword(env.getProperty("db.password"));
    return dataSource;
}

From source file:net.firejack.platform.core.config.installer.database.MySqlDatabaseManager.java

private DataSource getCreatableDataSource(Database database) {
    String databaseUrl = database.getRdbms().getDbSchemaUrlConnection(database.getProtocol(),
            database.getServerName(), database.getPort().toString(), "mysql");
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(databaseUrl);//from w w  w.  j  a va2 s.c  o  m
    dataSource.setUsername(database.getUsername());
    dataSource.setPassword(database.getPassword());
    return dataSource;
}

From source file:com.jt.dbcp.example.BasicDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("root");
    ds.setPassword("root");
    ds.setUrl(connectURI);//from w w  w . j  av a 2 s .  c o  m
    return ds;
}