List of usage examples for org.apache.commons.dbcp2 BasicDataSource setPassword
public void setPassword(String password)
Sets the #password .
Note: this method currently has no effect once the pool has been initialized.
From source file:io.druid.indexing.jdbc.JDBCIndexTask.java
@Override public TaskStatus run(final TaskToolbox toolbox) throws Exception { log.info("Starting up!"); startTime = DateTime.now();//www . j av a2s .com mapper = toolbox.getObjectMapper(); status = Status.STARTING; if (chatHandlerProvider.isPresent()) { log.info("Found chat handler of class[%s]", chatHandlerProvider.get().getClass().getName()); chatHandlerProvider.get().register(getId(), this, false); } else { log.warn("No chat handler detected"); } runThread = Thread.currentThread(); // Set up FireDepartmentMetrics final FireDepartment fireDepartmentForMetrics = new FireDepartment(dataSchema, new RealtimeIOConfig(null, null, null), null); fireDepartmentMetrics = fireDepartmentForMetrics.getMetrics(); toolbox.getMonitorScheduler() .addMonitor(new RealtimeMetricsMonitor(ImmutableList.of(fireDepartmentForMetrics), ImmutableMap.of(DruidMetrics.TASK_ID, new String[] { getId() }))); BasicDataSource dataSource = new BasicDataSource(); dataSource.setUsername(ioConfig.getUser()); dataSource.setPassword(ioConfig.getPassword()); dataSource.setUrl(ioConfig.getConnectURI()); dataSource.setDriverClassLoader(getClass().getClassLoader()); final String table = ioConfig.getTableName(); if (!StringUtils.isEmpty(ioConfig.getDriverClass())) { dataSource.setDriverClassName(ioConfig.getDriverClass()); } final Handle handle = new DBI(dataSource).open(); try (final Appenderator appenderator0 = newAppenderator(fireDepartmentMetrics, toolbox); final AppenderatorDriver driver = newDriver(appenderator0, toolbox, fireDepartmentMetrics)) { toolbox.getDataSegmentServerAnnouncer().announce(); appenderator = appenderator0; // Start up, set up initial offsets. final Object restoredMetadata = driver.startJob(); if (restoredMetadata == null) { nextOffsets.putAll(ioConfig.getJdbcOffsets().getOffsetMaps()); } else { final Map<String, Object> restoredMetadataMap = (Map) restoredMetadata; final JDBCOffsets restoredNextPartitions = toolbox.getObjectMapper() .convertValue(restoredMetadataMap.get(METADATA_NEXT_OFFSETS), JDBCOffsets.class); nextOffsets.putAll(restoredNextPartitions.getOffsetMaps()); // Sanity checks. if (!restoredNextPartitions.getTable().equals(ioConfig.getTableName())) { throw new ISE("WTF?! Restored table[%s] but expected table[%s]", restoredNextPartitions.getTable(), ioConfig.getTableName()); } if (!nextOffsets.equals(ioConfig.getJdbcOffsets().getOffsetMaps())) { throw new ISE("WTF?! Restored partitions[%s] but expected partitions[%s]", nextOffsets, ioConfig.getJdbcOffsets().getOffsetMaps()); } } // Set up sequenceNames. final Map<Integer, String> sequenceNames = Maps.newHashMap(); for (Integer partitionNum : nextOffsets.keySet()) { sequenceNames.put(partitionNum, String.format("%s_%s", ioConfig.getBaseSequenceName(), partitionNum)); } // Set up committer. final Supplier<Committer> committerSupplier = new Supplier<Committer>() { @Override public Committer get() { final Map<Integer, Long> snapshot = ImmutableMap.copyOf(nextOffsets); return new Committer() { @Override public Object getMetadata() { return ImmutableMap.of(METADATA_NEXT_OFFSETS, new JDBCOffsets(ioConfig.getJdbcOffsets().getTable(), snapshot)); } @Override public void run() { // Do nothing. } }; } }; // Set<Integer> assignment = assignPartitionsAndSeekToNext(handle); // boolean stillReading = !assignment.isEmpty(); status = Status.READING; try { // while (stillReading) { // if (possiblyPause(assignment)) { // The partition assignments may have changed while paused by a call to setEndOffsets() so reassign // partitions upon resuming. This is safe even if the end offsets have not been modified. // assignment = assignPartitionsAndSeekToNext(handle); // if (assignment.isEmpty()) { // log.info("All partitions have been fully read"); // publishOnStop = true; // stopRequested = true; // } // } // if (stopRequested) { // break; // } final String query = (ioConfig.getQuery() != null) ? ioConfig.getQuery() : makeQuery(ioConfig.getColumns(), ioConfig.getJdbcOffsets()); org.skife.jdbi.v2.Query<Map<String, Object>> dbiQuery = handle.createQuery(query); final ResultIterator<InputRow> rowIterator = dbiQuery.map(new ResultSetMapper<InputRow>() { List<String> queryColumns = (ioConfig.getColumns() == null) ? Lists.<String>newArrayList() : ioConfig.getColumns(); List<Boolean> columnIsNumeric = Lists.newArrayList(); @Override public InputRow map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException { try { if (queryColumns.size() == 0) { ResultSetMetaData metadata = r.getMetaData(); for (int idx = 1; idx <= metadata.getColumnCount(); idx++) { queryColumns.add(metadata.getColumnName(idx)); } Preconditions.checkArgument(queryColumns.size() > 0, String.format("No column in table [%s]", table)); verifyParserSpec(parser.getParseSpec(), queryColumns); } if (columnIsNumeric.size() == 0) { ResultSetMetaData metadata = r.getMetaData(); Preconditions.checkArgument(metadata.getColumnCount() >= queryColumns.size(), String.format( "number of column names [%d] exceeds the actual number of returning column values [%d]", queryColumns.size(), metadata.getColumnCount())); columnIsNumeric.add(false); // dummy to make start index to 1 for (int idx = 1; idx <= metadata.getColumnCount(); idx++) { boolean isNumeric = false; int type = metadata.getColumnType(idx); switch (type) { case BIGINT: case DECIMAL: case DOUBLE: case FLOAT: case INTEGER: case NUMERIC: case SMALLINT: case TINYINT: isNumeric = true; break; } columnIsNumeric.add(isNumeric); } } final Map<String, Object> columnMap = Maps.newHashMap(); int columnIdx = 1; for (String column : queryColumns) { Object objToPut = null; if (table != null) { objToPut = r.getObject(column); } else { objToPut = r.getObject(columnIdx); } columnMap.put(column, objToPut == null ? columnIsNumeric.get(columnIdx) : objToPut); columnIdx++; } return parser.parse(columnMap); } catch (IllegalArgumentException e) { throw new SQLException(e); } } }).iterator(); org.skife.jdbi.v2.Query<Map<String, Object>> maxItemQuery = handle .createQuery(makeMaxQuery(ioConfig.getJdbcOffsets())); long currOffset = maxItemQuery != null ? (long) maxItemQuery.list(1).get(0).get("MAX") : 0; while (rowIterator.hasNext()) { InputRow row = rowIterator.next(); try { if (!ioConfig.getMinimumMessageTime().isPresent() || !ioConfig.getMinimumMessageTime().get().isAfter(row.getTimestamp())) { final String sequenceName = sequenceNames.get(nextOffsets.keySet().toArray()[0]); //TODO::: check data final AppenderatorDriverAddResult addResult = driver.add(row, sequenceName, committerSupplier); if (addResult.isOk()) { // If the number of rows in the segment exceeds the threshold after adding a row, // move the segment out from the active segments of AppenderatorDriver to make a new segment. if (addResult.getNumRowsInSegment() > tuningConfig.getMaxRowsPerSegment()) { driver.moveSegmentOut(sequenceName, ImmutableList.of(addResult.getSegmentIdentifier())); } } else { // Failure to allocate segment puts determinism at risk, bail out to be safe. // May want configurable behavior here at some point. // If we allow continuing, then consider blacklisting the interval for a while to avoid constant checks. throw new ISE("Could not allocate segment for row with timestamp[%s]", row.getTimestamp()); } fireDepartmentMetrics.incrementProcessed(); } else { fireDepartmentMetrics.incrementThrownAway(); } } catch (ParseException e) { if (tuningConfig.isReportParseExceptions()) { throw e; } else { log.debug(e, "Dropping unparseable row from row[%d] .", row); fireDepartmentMetrics.incrementUnparseable(); } } } nextOffsets.put((int) ioConfig.getJdbcOffsets().getOffsetMaps().keySet().toArray()[0], currOffset); // if (nextOffsets.get(record.partition()).equals(endOffsets.get(record.partition())) // && assignment.remove(record.partition())) { // log.info("Finished reading table[%s], partition[%,d].", record.topic(), record.partition()); // stillReading = ioConfig.isPauseAfterRead() || !assignment.isEmpty(); // } // } } finally { driver.persist(committerSupplier.get()); // persist pending data } synchronized (statusLock) { if (stopRequested && !publishOnStop) { throw new InterruptedException("Stopping without publishing"); } status = Status.PUBLISHING; } final TransactionalSegmentPublisher publisher = (segments, commitMetadata) -> { final JDBCOffsets finalOffsets = toolbox.getObjectMapper() .convertValue(((Map) commitMetadata).get(METADATA_NEXT_OFFSETS), JDBCOffsets.class); // Sanity check, we should only be publishing things that match our desired end state. //TODO::: Santiny Check! // if (!endOffsets.equals(finalOffsets.getOffsetMaps())) { // throw new ISE("WTF?! Driver attempted to publish invalid metadata[%s].", commitMetadata); // } final SegmentTransactionalInsertAction action; if (ioConfig.isUseTransaction()) { action = new SegmentTransactionalInsertAction(segments, new JDBCDataSourceMetadata(ioConfig.getJdbcOffsets()), new JDBCDataSourceMetadata(finalOffsets) //TODO::: Check Values ); } else { action = new SegmentTransactionalInsertAction(segments, null, null); } log.info("Publishing with isTransaction[%s].", ioConfig.isUseTransaction()); return toolbox.getTaskActionClient().submit(action).isSuccess(); }; // Supervised kafka tasks are killed by JDBCSupervisor if they are stuck during publishing segments or waiting // for hand off. See JDBCSupervisorIOConfig.completionTimeout. final SegmentsAndMetadata published = driver .publish(publisher, committerSupplier.get(), sequenceNames.values()).get(); final SegmentsAndMetadata handedOff; if (tuningConfig.getHandoffConditionTimeout() == 0) { handedOff = driver.registerHandoff(published).get(); } else { handedOff = driver.registerHandoff(published).get(tuningConfig.getHandoffConditionTimeout(), TimeUnit.MILLISECONDS); } if (handedOff == null) { throw new ISE("Transaction failure publishing segments, aborting"); } else { log.info("Published segments[%s] with metadata[%s].", Joiner.on(", ") .join(Iterables.transform(handedOff.getSegments(), new Function<DataSegment, String>() { @Override public String apply(DataSegment input) { return input.getIdentifier(); } })), handedOff.getCommitMetadata()); } } catch (InterruptedException | RejectedExecutionException e) { // handle the InterruptedException that gets wrapped in a RejectedExecutionException if (e instanceof RejectedExecutionException && (e.getCause() == null || !(e.getCause() instanceof InterruptedException))) { throw e; } // if we were interrupted because we were asked to stop, handle the exception and return success, else rethrow if (!stopRequested) { Thread.currentThread().interrupt(); throw e; } log.info("The task was asked to stop before completing"); } finally { if (chatHandlerProvider.isPresent()) { chatHandlerProvider.get().unregister(getId()); } handle.close(); } toolbox.getDataSegmentServerAnnouncer().unannounce(); //TODO::implement return success(); }
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);//from ww w . j a v a 2s .c o m if (useSSL) { result.setConnectionProperties("ssl=true"); } 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 {//from w ww. j a va2 s . com 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() {/*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() {// www .ja v a 2s . c o 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. * /*from www. j a v a2 s.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 {// w w w .j a v a2 s.co 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.apache.jmeter.protocol.jdbc.config.DataSourceElement.java
private BasicDataSource initPool(String maxPool) { BasicDataSource dataSource = new BasicDataSource(); if (log.isDebugEnabled()) { StringBuilder sb = new StringBuilder(40); sb.append("MaxPool: "); sb.append(maxPool);/*w ww . j a va 2 s .c o m*/ sb.append(" Timeout: "); sb.append(getTimeout()); sb.append(" TrimInt: "); sb.append(getTrimInterval()); sb.append(" Auto-Commit: "); sb.append(isAutocommit()); log.debug(sb.toString()); } int poolSize = Integer.parseInt(maxPool); dataSource.setMinIdle(0); dataSource.setInitialSize(poolSize); dataSource.setMaxIdle(poolSize); dataSource.setMaxTotal(poolSize); dataSource.setMaxWaitMillis(Long.parseLong(getTimeout())); dataSource.setDefaultAutoCommit(Boolean.valueOf(isAutocommit())); if (log.isDebugEnabled()) { StringBuilder sb = new StringBuilder(40); sb.append("KeepAlive: "); sb.append(isKeepAlive()); sb.append(" Age: "); sb.append(getConnectionAge()); sb.append(" CheckQuery: "); sb.append(getCheckQuery()); log.debug(sb.toString()); } dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setTestOnCreate(false); dataSource.setTestWhileIdle(false); if (isKeepAlive()) { dataSource.setTestWhileIdle(true); dataSource.setValidationQuery(getCheckQuery()); dataSource.setSoftMinEvictableIdleTimeMillis(Long.parseLong(getConnectionAge())); dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(getTrimInterval())); } int transactionIsolation = DataSourceElementBeanInfo.getTransactionIsolationMode(getTransactionIsolation()); if (transactionIsolation >= 0) { dataSource.setDefaultTransactionIsolation(transactionIsolation); } String _username = getUsername(); if (log.isDebugEnabled()) { StringBuilder sb = new StringBuilder(40); sb.append("Driver: "); sb.append(getDriver()); sb.append(" DbUrl: "); sb.append(getDbUrl()); sb.append(" User: "); sb.append(_username); log.debug(sb.toString()); } dataSource.setDriverClassName(getDriver()); dataSource.setUrl(getDbUrl()); if (_username.length() > 0) { dataSource.setUsername(_username); dataSource.setPassword(getPassword()); } // log is required to ensure errors are available //source.enableLogging(new LogKitLogger(log)); if (log.isDebugEnabled()) { log.debug("PoolConfiguration:" + this.dataSource); } return dataSource; }
From source file:org.assertj.db.database.sqlite.SqliteConfiguration.java
@Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.sqlite.JDBC"); dataSource.setUrl("jdbc:sqlite:target/testDerby.db"); dataSource.setUsername(""); dataSource.setPassword(""); try {/*from w w w . ja v a 2 s . co m*/ try (Connection connection = dataSource.getConnection();) { try (Statement statement = connection.createStatement()) { statement.executeUpdate("drop table if exists teSt;"); statement.executeUpdate("create table teSt (" + " Var1 BIGINT PRIMARY KEY,\n" + " vAr2 BLOB,\n" + " vaR3 CHAR,\n" + " var4 CHAR FOR BIT DATA,\n" + " var5 CLOB,\n" + " var6 DATE,\n" + " var7 DECIMAL,\n" + " var8 DOUBLE,\n" + " var9 DOUBLE PRECISION,\n" + " var10 FLOAT,\n" + " var11 INTEGER,\n" + " var12 LONG VARCHAR,\n" + " var13 LONG VARCHAR FOR BIT DATA,\n" + " var14 NUMERIC,\n" + " var15 REAL,\n" + " var16 SMALLINT,\n" + " var17 TIME,\n" + " var18 TIMESTAMP,\n" + " var19 VARCHAR,\n" + " var20 VARCHAR FOR BIT DATA" + " )"); } } } catch (Exception e) { e.printStackTrace(); } return dataSource; }