Example usage for org.apache.commons.dbcp2 BasicDataSource setUrl

List of usage examples for org.apache.commons.dbcp2 BasicDataSource setUrl

Introduction

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

Prototype

public synchronized void setUrl(String url) 

Source Link

Document

Sets the #url .

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:annis.administration.AdministrationDao.java

private BasicDataSource createDataSource(String host, String port, String database, String user,
        String password, boolean useSSL, String schema) {

    String url = "jdbc:postgresql://" + host + ":" + port + "/" + database;

    // DriverManagerDataSource is deprecated
    // return new DriverManagerDataSource("org.postgresql.Driver", url, user, password);
    BasicDataSource result = new BasicDataSource();
    result.setUrl(url);
    if (useSSL) {
        result.setConnectionProperties("ssl=true");
    }//w w  w .  j  av  a  2 s .  com
    result.setUsername(user);
    result.setPassword(password);
    result.setValidationQuery("SELECT 1;");
    result.setAccessToUnderlyingConnectionAllowed(true);
    if (schema == null) {
        schema = "public";
    }
    result.setConnectionInitSqls(Arrays.asList("SET search_path TO \"$user\"," + schema));

    result.setDriverClassName("org.postgresql.Driver");

    return result;
}

From source file:common.SwingGUI01.java

private DataSource prepareDataSource() {
    Properties myconf = new Properties();
    try {//w  ww .j  a  va  2s  .  c om
        myconf.load(Agency.class.getResourceAsStream("/myconf.properties"));
    } catch (IOException ex) {
        errorMessage = "Error opening properties file";
        log.error(errorMessage, ex);
        showErrorDialog(errorMessage);
    }

    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(myconf.getProperty("jdbc.url"));
    ds.setUsername(myconf.getProperty("jdbc.user"));
    ds.setPassword(myconf.getProperty("jdbc.password"));

    return ds;
}

From source file:net.sp1d.chym.loader.RootConfig.java

@Bean
DataSource dataSource() {//from ww w  .  j a  v  a  2s . c o m
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(env.getProperty("db.conn.jdbcUrl"));
    ds.setUsername(env.getProperty("db.conn.user"));
    ds.setPassword(env.getProperty("db.conn.password"));
    ds.setDriverClassName(env.getProperty("db.conn.driverClass"));

    return ds;
}

From source file:net.sp1d.chym.loader.RootConfigDev.java

@Bean
DataSource dataSource() {/*from w  w w. jav  a  2  s  .co  m*/
    BasicDataSource ds = new BasicDataSource();
    //        ds.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");        
    ////        ds.setUrl("jdbc:hsqldb:mem:db");
    //        ds.setUrl("jdbc:hsqldb:file:db/db");
    //        ds.setUsername("SA");
    //        ds.setPassword("SA");

    ds.setUrl(env.getProperty("db.conn.jdbcUrl"));
    ds.setUsername(env.getProperty("db.conn.user"));
    ds.setPassword(env.getProperty("db.conn.password"));
    ds.setDriverClassName(env.getProperty("db.conn.driverClass"));

    return ds;
}

From source file:ninja.jooq.NinjaJooqLifecycle.java

/**
 * This method reads the configuration properties from
 * your application.conf file and configures jOOQ accordingly.
 * //w ww . ja v a 2s  .c om
 */
public final void startServer() {
    logger.info("Starting jOOQ Module.");

    // Setup basic parameters
    boolean renderSchema = ninjaProperties.getBooleanWithDefault(JOOQ_RENDER_SCHEMA, true);

    //renderMapping

    String renderNameStyleString = ninjaProperties.getWithDefault(JOOQ_RENDER_NAME_STYLE, "QUOTED");
    RenderNameStyle renderNameStyle = RenderNameStyle.fromValue(renderNameStyleString);
    String renderKeywordStyleString = ninjaProperties.getWithDefault(JOOQ_RENDER_KEYWORD_STYLE, "LOWER");
    RenderKeywordStyle renderKeywordStyle = RenderKeywordStyle.valueOf(renderKeywordStyleString);

    boolean renderFormatted = ninjaProperties.getBooleanWithDefault(JOOQ_RENDER_FORMATTED, false);

    String statementTypeString = ninjaProperties.getWithDefault(JOOQ_STATEMENT_TYPE, "PREPARED_STATEMENT");
    StatementType statementType = StatementType.valueOf(statementTypeString);

    boolean executeLogging = ninjaProperties.getBooleanWithDefault(JOOQ_EXECUTE_LOGGING, true);

    // Execute listeners

    boolean executeWithOptimisticLocking = ninjaProperties
            .getBooleanWithDefault(JOOQ_EXECUTE_WITH_OPTIMISTIC_LOCKING, true);

    boolean attachRecords = ninjaProperties.getBooleanWithDefault(JOOQ_ATTACH_RECORDS, true);

    String sqlDialectString = ninjaProperties.getWithDefault(JOOQ_SQL_DIALECT, "DEFAULT");
    SQLDialect sqlDialect = SQLDialect.valueOf(sqlDialectString);

    Settings settings = new Settings();
    settings.setRenderSchema(renderSchema);
    settings.setRenderNameStyle(renderNameStyle);
    settings.setRenderKeywordStyle(renderKeywordStyle);
    settings.setRenderFormatted(renderFormatted);
    settings.setStatementType(statementType);
    settings.setExecuteLogging(executeLogging);
    settings.setExecuteWithOptimisticLocking(executeWithOptimisticLocking);
    settings.setAttachRecords(attachRecords);

    String connectionUrl = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_URL);
    String connectionUsername = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_USERNAME);
    String connectionPassword = ninjaProperties.getWithDefault(NinjaConstant.DB_CONNECTION_PASSWORD, "");

    BasicDataSource connectionPool = new BasicDataSource();

    connectionPool.setUrl(connectionUrl);
    connectionPool.setUsername(connectionUsername);
    connectionPool.setPassword(connectionPassword);

    Configuration configuration = new DefaultConfiguration();
    configuration.set(sqlDialect);
    configuration.set(settings);
    configuration.set(connectionPool);

    dslContext = DSL.using(configuration);
}

From source file:no.kantega.publishing.common.util.database.dbConnectionFactory.java

public static void loadConfiguration() {
    try {//from   w ww  . j  a v  a2  s . c o m

        setConfiguration();

        verifyCompleteDatabaseConfiguration();

        DriverManagerDataSource rawDataSource = new DriverManagerDataSource();
        rawDataSource.setDriverClassName(dbDriver);
        rawDataSource.setUrl(dbUrl);

        if (!dbNTMLAuthentication) {
            rawDataSource.setUsername(dbUsername);
            rawDataSource.setPassword(dbPassword);
        }

        if (dbEnablePooling) {
            // Enable DBCP pooling
            BasicDataSource bds = new BasicDataSource();
            bds.setMaxTotal(dbMaxConnections);
            bds.setMaxIdle(dbMaxIdleConnections);
            bds.setMinIdle(dbMinIdleConnections);
            if (dbMaxWait != -1) {
                bds.setMaxWaitMillis(1000 * dbMaxWait);
            }

            if (dbDefaultQueryTimeout != -1) {
                bds.setDefaultQueryTimeout(dbDefaultQueryTimeout);
            }

            bds.setDriverClassName(dbDriver);
            if (!dbNTMLAuthentication) {
                bds.setUsername(dbUsername);
                bds.setPassword(dbPassword);
            }
            bds.setUrl(dbUrl);

            if (dbUseTransactions) {
                bds.setDefaultTransactionIsolation(dbTransactionIsolationLevel);
            }

            if (dbCheckConnections) {
                // Gjr at connections frigjres ved lukking fra database/brannmur
                bds.setValidationQuery("SELECT max(ContentId) from content");
                bds.setTimeBetweenEvictionRunsMillis(1000 * 60 * 2);
                bds.setMinEvictableIdleTimeMillis(1000 * 60 * 5);
                bds.setNumTestsPerEvictionRun(dbMaxConnections);
                if (dbRemoveAbandonedTimeout > 0) {
                    bds.setRemoveAbandonedTimeout(dbRemoveAbandonedTimeout);
                    bds.setLogAbandoned(true);
                }
            }

            ds = bds;
        } else {
            ds = rawDataSource;
        }

        // Use non-pooled datasource for table creation since validation query might fail
        ensureDatabaseExists(rawDataSource);
        if (shouldMigrateDatabase) {
            migrateDatabase(servletContext, rawDataSource);
        }

        if (dbUseTransactions) {
            log.info("Using transactions, database transaction isolation level set to "
                    + dbTransactionIsolationLevel);
        } else {
            log.info("Not using transactions");
        }

        if (debugConnections) {
            proxyDs = (DataSource) Proxy.newProxyInstance(DataSource.class.getClassLoader(),
                    new Class[] { DataSource.class }, new DataSourceWrapper(ds));
        }

    } catch (Exception e) {
        log.error("********* could not read aksess.conf **********", e);
    }

}

From source file:olsps.com.healthsoftproject.config.ApplicationConfigClass.java

@Bean(name = "dataSource")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
    dataSource.setUrl("jdbc:derby://localhost:1527/medicalcenter");
    dataSource.setUsername("yusufcassim");
    dataSource.setPassword("yusufcassim");
    return dataSource;
}

From source file:org.adamkrajcik.winecellars.CellarManagerImplTest.java

private static DataSource prepareDataSource() {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl("jdbc:derby:memory:cellarmanager-test;create=true");
    return bds;/*from w  ww  .j a v a  2s. c o  m*/
}

From source file:org.adamkrajcik.winecellars.WineCellarsManagerImplTest.java

private DataSource prepareDataSource() {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl("jdbc:derby:memory:winecellarmanager-test;create=true");
    return bds;//from  www. jav a2 s . c  o m
}

From source file:org.adamkrajcik.winecellars.WineManagerImplTest.java

@Before
public void setUp() throws SQLException {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl("jdbc:derby:memory:winemanager-test;create=true");
    dataSource = bds;/*from   w w  w  . j av  a  2 s.c  o  m*/

    try (Connection conn = dataSource.getConnection()) {
        conn.prepareStatement(
                "CREATE TABLE CELLAR (id BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, "
                        + "name VARCHAR(256) NOT NULL, address VARCHAR(256) NOT NULL, wineCapacity INTEGER NOT NULL, "
                        + "CONSTRAINT WINECAPACITY_LESS_THAN_OR_EQUAL_ZERO CHECK (wineCapacity > 0))")
                .executeUpdate();
        conn.prepareStatement("CREATE TABLE WINE (id BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, "
                + "cellarId BIGINT REFERENCES CELLAR (id), name VARCHAR(256) NOT NULL, "
                + "country VARCHAR(128) NOT NULL, productionYear INTEGER NOT NULL, "
                + "quantity INTEGER NOT NULL, type VARCHAR(5) NOT NULL, "
                + "CONSTRAINT YEAR_LESS_THAN_OR_EQUAL_ZERO CHECK (productionYear > 0), "
                + "CONSTRAINT QUANTITY_LESS_THEN_OR_EQUAL_ZERO CHECK (quantity > 0), "
                + "CONSTRAINT INVALID_TYPE CHECK (type IN ('RED', 'WHITE', 'ROSE')))").executeUpdate();
    }

    manager = new WineManagerImpl();
    manager.setDataSource(dataSource);
}