本文整理汇总了Java中org.apache.commons.dbcp.BasicDataSource类的典型用法代码示例。如果您正苦于以下问题:Java BasicDataSource类的具体用法?Java BasicDataSource怎么用?Java BasicDataSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BasicDataSource类属于org.apache.commons.dbcp包,在下文中一共展示了BasicDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: register
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public boolean register(JdbcVo conf) {
boolean isOk = true;
try {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(conf.getDriverClass());
bds.setUrl(conf.getUrl());
bds.setUsername(conf.getUserName());
bds.setPassword(conf.getPassword());
bds.setInitialSize(conf.getInitialSize());
bds.setMaxActive(conf.getMaxActive());
bds.setMaxIdle(conf.getMaxIdle());
bds.setMinIdle(conf.getMinIdle());
cmap.put(conf.getKey(), bds);
} catch (Exception e) {
logger.error("[db container init key " + conf.getKey() + " datasource error!]", e);
isOk = false;
}
return isOk;
}
示例2: buildDataSource
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
/**
* 通过基础配置信息构建DBCP数据源信息.
* @param driver 数据库连接的JDBC驱动
* @param url 数据库连接的url
* @param user 数据库连接的用户名
* @param password 数据库连接的密码
*/
public BasicDataSource buildDataSource(String driver, String url, String user, String password) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
示例3: register
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public boolean register(JdbcVo conf) {
boolean isOk = true;
try {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(conf.getDriverClass());
bds.setUrl(conf.getUrl());
bds.setUsername(conf.getUserName());
bds.setPassword(conf.getPassword());
bds.setInitialSize(conf.getInitialSize());
bds.setMaxActive(conf.getMaxActive());
bds.setMaxIdle(conf.getMaxIdle());
bds.setMinIdle(conf.getMinIdle());
cmap.put(conf.getKey(), bds);
} catch (Exception e) {
LoggerFactory.getLogger().error("[db container initThreadPool key " + conf.getKey() + " datasource error!]", e);
isOk = false;
}
return isOk;
}
示例4: doStart
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
@Override
protected void doStart() throws Exception {
final Map<String, Object> options = getOptions();
if (!options.containsKey("dataSource")) {
if (options.containsKey("user") && options.containsKey("password") && options.containsKey("url")) {
BasicDataSource ds = new BasicDataSource();
consumeOption("user", String.class, ds::setUsername);
consumeOption("password", String.class, ds::setPassword);
consumeOption("url", String.class, ds::setUrl);
addOption("dataSource", ds);
} else {
LOGGER.debug("Not enough information provided to set-up the DataSource");
}
}
super.doStart();
}
示例5: doStart
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
@Override
protected void doStart() throws Exception {
final Map<String, Object> options = getOptions();
if (!options.containsKey("dataSource")) {
if (options.containsKey("user") && options.containsKey("password") && options.containsKey("url")) {
BasicDataSource ds = new BasicDataSource();
consumeOption("user", String.class, ds::setUsername);
consumeOption("password", String.class, ds::setPassword);
consumeOption("url", String.class, ds::setUrl);
addOption("dataSource", ds);
} else {
LOGGER.debug("Not enough information provided to set-up the DataSource");
}
}
super.doStart();
}
示例6: getJobEventRdbConfiguration
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
/**
* 获取作业数据库事件配置.
*
* @return 作业数据库事件配置
*/
public Optional<JobEventRdbConfiguration> getJobEventRdbConfiguration() {
String driver = getValue(EnvironmentArgument.EVENT_TRACE_RDB_DRIVER);
String url = getValue(EnvironmentArgument.EVENT_TRACE_RDB_URL);
String username = getValue(EnvironmentArgument.EVENT_TRACE_RDB_USERNAME);
String password = getValue(EnvironmentArgument.EVENT_TRACE_RDB_PASSWORD);
if (!Strings.isNullOrEmpty(driver) && !Strings.isNullOrEmpty(url) && !Strings.isNullOrEmpty(username)) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return Optional.of(new JobEventRdbConfiguration(dataSource));
}
return Optional.absent();
}
示例7: JdbcStoragePlugin
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public JdbcStoragePlugin(JdbcStorageConfig config, DrillbitContext context, String name) {
this.context = context;
this.config = config;
this.name = name;
BasicDataSource source = new BasicDataSource();
source.setDriverClassName(config.getDriver());
source.setUrl(config.getUrl());
if (config.getUsername() != null) {
source.setUsername(config.getUsername());
}
if (config.getPassword() != null) {
source.setPassword(config.getPassword());
}
this.source = source;
this.dialect = JdbcSchema.createDialect(source);
this.convention = new DrillJdbcConvention(dialect, name);
}
示例8: destroyDataSource
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public void destroyDataSource(DataSource dataSource) {
try {
// for filter to destroy custom datasource
if (letHandlerDestroyIfSupport(0L, dataSource)) {
return;
}
if (dataSource == null) {
return;
}
BasicDataSource basicDataSource = (BasicDataSource) dataSource;
basicDataSource.close();
} catch (SQLException e) {
logger.error("ERROR ## close the datasource has an error", e);
}
}
示例9: destroy
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public void destroy(Long pipelineId) {
Map<DbMediaSource, DataSource> sources = dataSources.remove(pipelineId);
if (sources != null) {
for (DataSource source : sources.values()) {
try {
// for filter to destroy custom datasource
if (letHandlerDestroyIfSupport(pipelineId, source)) {
continue;
}
// fallback for regular destroy
// TODO need to integrate to handler
BasicDataSource basicDataSource = (BasicDataSource) source;
basicDataSource.close();
} catch (SQLException e) {
logger.error("ERROR ## close the datasource has an error", e);
}
}
sources.clear();
}
}
示例10: getJdbcTemplate
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
@Provides
@Singleton
public NamedParameterJdbcTemplate getJdbcTemplate(
@Named("jdbc.driver") String driver,
@Named("jdbc.username") String username,
@Named("jdbc.password") String password,
@Named("jdbc.url") String url,
@Named("jdbc.maxActive") Integer maxActive,
@Named("jdbc.maxIdle") Integer maxIdle,
@Named("jdbc.initialSize") Integer initialSize,
@Named("jdbc.validationQuery") String validationQuery) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
dataSource.setMaxActive(maxActive);
dataSource.setMaxIdle(maxIdle);
dataSource.setInitialSize(initialSize);
dataSource.setValidationQuery(validationQuery);
return new NamedParameterJdbcTemplate(dataSource);
}
示例11: createFromJdbcUrl
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
private static DataSource createFromJdbcUrl(Class<? extends Driver> driverClass, String url,
Credential credential, int numThreads, ImmutableList<String> initSqls, Properties extraConnectionProperties) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClass.getName());
dataSource.setUrl(url);
dataSource.setUsername(credential.getUsername());
dataSource.setPassword(credential.getPassword());
// connection pool settings
dataSource.setInitialSize(numThreads);
dataSource.setMaxActive(numThreads);
// keep the connections open if possible; only close them via the removeAbandonedTimeout feature
dataSource.setMaxIdle(numThreads);
dataSource.setMinIdle(0);
dataSource.setRemoveAbandonedTimeout(300);
dataSource.setConnectionInitSqls(initSqls.castToList());
if (extraConnectionProperties != null) {
for (String key : extraConnectionProperties.stringPropertyNames()) {
dataSource.addConnectionProperty(key, extraConnectionProperties.getProperty(key));
}
}
return dataSource;
}
示例12: getDataSource
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
public static DataSource getDataSource() {
BasicDataSource ds = new BasicDataSource();
if (StringUtil.isNotEmpty(driver)) {
ds.setDriverClassName(driver);
}
if (StringUtil.isNotEmpty(url)) {
ds.setUrl(url);
}
if (StringUtil.isNotEmpty(username)) {
ds.setUsername(username);
}
if (StringUtil.isNotEmpty(password)) {
ds.setPassword(password);
}
if (maxActive != 0) {
ds.setMaxActive(maxActive);
}
if (maxIdle != 0) {
ds.setMaxIdle(maxIdle);
}
return ds;
}
示例13: createBasicDataSource
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
/**
* createBasicDataSource
*
* @param dbDriver
* database driver
* @param dbUser
* database user
* @param dbPwd
* database password
* @param dbURL
* database url
* @return BasicDataSource based on parameter inputs
*/
public BasicDataSource createBasicDataSource(String dbDriver, String dbUser, String dbPwd, String dbURL) {
BasicDataSource d = new BasicDataSource();
d.setDriverClassName(dbDriver);
d.setUsername(dbUser);
d.setPassword(dbPwd);
d.setUrl(dbURL);
try {
Class.forName(ConfigurationManager.getInstance().getDatabaseDriver());
} catch (ClassNotFoundException e) {
log.error("Unable to initialize database driver.");
}
return d;
}
示例14: destroy
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
/**
* Destroy the initial context.
*/
public static void destroy() {
Map<String, Object> jndiObjectsMap = jndiContextData.get();
if (jndiObjectsMap != null) {
for (Map.Entry entry : jndiObjectsMap.entrySet()) {
Object value = entry.getValue();
if (value != null && value instanceof BasicDataSource) {
try {
((BasicDataSource) value).close();
} catch (SQLException e) {
//Just Ignore for now.
}
}
}
jndiContextData.remove();
}
}
示例15: addContextLookup
import org.apache.commons.dbcp.BasicDataSource; //导入依赖的package包/类
private static void addContextLookup(String name, BasicDataSource object) {
Map context = jndiContextData.get();
if (context == null) {
context = new HashMap();
jndiContextData.set(context);
}
Object old = context.get(name);
if (old instanceof BasicDataSource) {
try {
((BasicDataSource) old).close();
} catch (Exception e) {
log.error("Error while closing the in-memory H2 Database.", e);
}
}
context.put(name, object);
}