Package org.apache.commons.dbcp

Examples of org.apache.commons.dbcp.BasicDataSource


    return config.getConfigProperty("org.pentaho.reporting.engine.classic.core." + key);
  }

  public static DataSource convert(final IDatabaseConnection databaseConnection)
  {
    final BasicDataSource basicDatasource = new BasicDataSource();
    final IDatabaseDialectService databaseDialectService =
        ClassicEngineBoot.getInstance().getObjectFactory().get(IDatabaseDialectService.class);
    final IDatabaseDialect dialect = databaseDialectService.getDialect(databaseConnection);
    if ("GENERIC".equals(databaseConnection.getDatabaseType().getShortName()))//$NON-NLS-1$
    {
      basicDatasource.setDriverClassName(databaseConnection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS));
    }
    else
    {
      basicDatasource.setDriverClassName(dialect.getNativeDriver());
    }
    try
    {
      basicDatasource.setUrl(dialect.getURLWithExtraOptions(databaseConnection));
    }
    catch (DatabaseDialectException e)
    {
      basicDatasource.setUrl(null);
    }
    basicDatasource.setUsername(databaseConnection.getUsername());
    basicDatasource.setPassword(databaseConnection.getPassword());
    final Map<String, String> attributes = databaseConnection.getAttributes();
    if (attributes.containsKey(DataBaseConnectionAttributes.MAX_ACTIVE_KEY))
    {
      final String value = attributes.get(DataBaseConnectionAttributes.MAX_ACTIVE_KEY);
      basicDatasource.setMaxActive(Integer.parseInt(value));
    }
    if (attributes.containsKey(DataBaseConnectionAttributes.MAX_WAIT_KEY))
    {
      final String value = attributes.get(DataBaseConnectionAttributes.MAX_WAIT_KEY);
      basicDatasource.setMaxWait(Integer.parseInt(value));
    }
    if (attributes.containsKey(DataBaseConnectionAttributes.MAX_IDLE_KEY))
    {
      final String value = attributes.get(DataBaseConnectionAttributes.MAX_IDLE_KEY);
      basicDatasource.setMaxIdle(Integer.parseInt(value));
    }
    if (attributes.containsKey(DataBaseConnectionAttributes.QUERY_KEY))
    {
      basicDatasource.setValidationQuery(attributes.get(DataBaseConnectionAttributes.QUERY_KEY));
    }
    return basicDatasource;
  }
View Full Code Here


    public static final String TYPE_NAME = "mysql";

    private BasicDataSource dataSource;

    public MysqlStorageConfiguration(VoldemortConfig config) {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl("jdbc:mysql://" + config.getMysqlHost() + ":" + config.getMysqlPort() + "/"
                  + config.getMysqlDatabaseName());
        ds.setUsername(config.getMysqlUsername());
        ds.setPassword(config.getMysqlPassword());
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        this.dataSource = ds;
    }
View Full Code Here

      manager.getConfiguration().save();
      throw new PermissionBackendException("SQL connection is not configured, see config.yml");
    }
    dbDriver = dbUri.split(":", 2)[0];

    this.ds = new BasicDataSource();
    String driverClass = getDriverClass(dbDriver);
    if (driverClass != null) {
      this.ds.setDriverClassName(driverClass);
    }
    this.ds.setUrl("jdbc:" + dbUri);
View Full Code Here

      String username, String password) {
    if (url.startsWith("jdbc:hsqldb:")) {
      // Prevent hsqldb from screwing up java.util.logging.
      System.setProperty("hsqldb.reconfig_logging", "false");
    }
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setDriverClassName(driverClassName);
    return dataSource;
  }
View Full Code Here

                        && Context.class.isAssignableFrom(driverClass)) {
                    DataSource ds = getJndiDataSource((Class<Context>) driverClass, def.getUrl());
                    nameToDataSource.put(def.getLogicalName(), ds);
                    nameToDataSourceDef.put(def.getLogicalName(), def);
                } else {
                    BasicDataSource bds =
                        getDriverDataSource(driverClass, def.getUrl(), def.getUser(), def.getPassword());
                    if (def.getMaxPoolSize() > 0) {
                        bds.setMaxActive(def.getMaxPoolSize());
                    }
                    if (def.getValidationQuery() != null && !"".equals(def.getValidationQuery().trim())) {
                        bds.setValidationQuery(def.getValidationQuery());
                    }
                    nameToDataSource.put(def.getLogicalName(), bds);
                    nameToDataSourceDef.put(def.getLogicalName(), def);
                }
            }
View Full Code Here

     * @param url the JDBC connection URL
     * @return pooling DataSource for accessing the specified database
     */
    private BasicDataSource getDriverDataSource(
            Class<?> driverClass, String url, String user, String password) {
        BasicDataSource ds = new BasicDataSource();
        created.add(ds);

        if (driverClass != null) {
          Driver instance = null;
            try {
                // Workaround for Apache Derby:
                // The JDBC specification recommends the Class.forName
                // method without the .newInstance() method call,
                // but it is required after a Derby 'shutdown'
                instance = (Driver) driverClass.newInstance();
            } catch (Throwable e) {
                // Ignore exceptions as there's no requirement for
                // a JDBC driver class to have a public default constructor
            }
            if (instance != null) {
                if (instance.jdbcCompliant()) {
                  // JCR-3445 At the moment the PostgreSQL isn't compliant because it doesn't implement this method...                 
                    ds.setValidationQueryTimeout(3);
                }
            }
            ds.setDriverClassName(driverClass.getName());
        }

        ds.setUrl(url);
        ds.setUsername(user);
        ds.setPassword(password);
        ds.setDefaultAutoCommit(true);
        ds.setTestOnBorrow(false);
        ds.setTestWhileIdle(true);
        ds.setTimeBetweenEvictionRunsMillis(600000); // 10 Minutes
        ds.setMinEvictableIdleTimeMillis(60000); // 1 Minute
        ds.setMaxActive(-1); // unlimited
        ds.setMaxIdle(GenericObjectPool.DEFAULT_MAX_IDLE + 10);
        ds.setValidationQuery(guessValidationQuery(url));
        ds.setAccessToUnderlyingConnectionAllowed(true);
        ds.setPoolPreparedStatements(true);
        ds.setMaxOpenPreparedStatements(-1); // unlimited
        return ds;
    }
View Full Code Here

    public void testReleaseConnectionsInCompiledQuery() throws Exception {
        final int connectionPoolSize = 2;
        final int threadCount = 4;
        final int noOfCallsPerThreads = 30;

        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.hsqldb.jdbcDriver");
        ds.setUrl("jdbc:hsqldb:res:metamodel");
        ds.setInitialSize(connectionPoolSize);
        ds.setMaxActive(connectionPoolSize);
        ds.setMaxWait(10000);
        ds.setMinEvictableIdleTimeMillis(1800000);
        ds.setMinIdle(0);
        ds.setMaxIdle(connectionPoolSize);
        ds.setNumTestsPerEvictionRun(3);
        ds.setTimeBetweenEvictionRunsMillis(-1);
        ds.setDefaultTransactionIsolation(java.sql.Connection.TRANSACTION_READ_COMMITTED);

        final JdbcDataContext dataContext = new JdbcDataContext(ds,
                new TableType[] { TableType.TABLE, TableType.VIEW }, null);

        final JdbcCompiledQuery compiledQuery = (JdbcCompiledQuery) dataContext.query().from("CUSTOMERS")
View Full Code Here

        DataSource connectionFactory = (DataSource) mcf.createConnectionFactory(connectionManager);
        return connectionFactory;
    }

    private DataSource createNonJtaDataSource() throws Exception {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.hsqldb.jdbcDriver");
        ds.setUrl("jdbc:hsqldb:mem:JpaTest");
        ds.setUsername("sa");
        ds.setPassword("");
        ds.setMaxActive(100);
        ds.setMaxWait(10000);
        ds.setTestOnBorrow(true);
        return ds;
    }
View Full Code Here

        DataSource connectionFactory = (DataSource) mcf.createConnectionFactory(connectionManager);
        return connectionFactory;
    }

    private DataSource createNonJtaDataSource() throws Exception {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.hsqldb.jdbcDriver");
        ds.setUrl("jdbc:hsqldb:mem:JpaTest");
        ds.setUsername("sa");
        ds.setPassword("");
        ds.setMaxActive(100);
        ds.setMaxWait(10000);
        ds.setTestOnBorrow(true);
        return ds;
    }
View Full Code Here

        params.put(PORT.key, port);
        params.put(USER.key, username);
        params.put(PASSWD.key, password);
        params.put(DATABASE.key, database);

        BasicDataSource dataSource = null;
        Connection connection = null;
        try {
            dataSource = factory.createDataSource(params);
            connection = dataSource.getConnection();

            Statement statement = connection.createStatement();

            if (!hasWritableTable("geometry_columns", "f_geometry_column", statement)) { //$NON-NLS-1$
                error = "Database is not a Postgis Database.\nThe 'geometry_columns' table is either missing or not accessible";
                return;
            }
            if (!hasWritableTable("spatial_ref_sys", "srid", statement)) { //$NON-NLS-1$
                error = "Database is not a Postgis Database.\nThe 'srid' table is either missing or not accessible";
                return;
            }

            // Pair is schema, table name
            List<Pair<String, String>> tableNames = new ArrayList<Pair<String, String>>();

            ResultSet resultSet = statement
                    .executeQuery("SELECT schemaname, tablename FROM pg_tables ORDER BY schemaname, tablename;");
            while( resultSet.next() ) {
                String schema = resultSet.getString("schemaname"); //$NON-NLS-1$
                String table = resultSet.getString("tablename"); //$NON-NLS-1$
                tableNames.add(Pair.create(schema, table));
            }
            Collection<TableDescriptor> results = lookupGeometryColumn(tableNames, connection);
            tables.addAll(results);
            statement.close();
        } catch (SQLException e) {
            error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: "
                    + e.getMessage();
        }
        catch (IOException io ){
            error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: "
                + io.getMessage();
        } finally {
            if( connection != null ){
                connection.close();
            }
            if( dataSource != null ){
                dataSource.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.dbcp.BasicDataSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.