当前位置: 首页>>代码示例>>Java>>正文


Java BasicDataSource.setValidationQuery方法代码示例

本文整理汇总了Java中org.apache.commons.dbcp.BasicDataSource.setValidationQuery方法的典型用法代码示例。如果您正苦于以下问题:Java BasicDataSource.setValidationQuery方法的具体用法?Java BasicDataSource.setValidationQuery怎么用?Java BasicDataSource.setValidationQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.dbcp.BasicDataSource的用法示例。


在下文中一共展示了BasicDataSource.setValidationQuery方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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);
}
 
开发者ID:Atypon-OpenSource,项目名称:wayf-cloud,代码行数:25,代码来源:WayfGuiceModule.java

示例2: _get

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private static BasicDataSource _get(String D, String username, String passwd, String url, int N) {
  BasicDataSource external = new BasicDataSource();
  external.setDriverClassName(D.trim());

  if (!X.isEmpty(username)) {
    external.setUsername(username.trim());
  }
  if (!X.isEmpty(passwd)) {
    external.setPassword(passwd.trim());
  }

  external.setUrl(url.trim());

  external.setMaxActive(N);
  external.setDefaultAutoCommit(true);
  external.setMaxIdle(N);
  external.setMaxWait(MAX_WAIT_TIME);
  external.setDefaultAutoCommit(true);
  external.setDefaultReadOnly(false);
  external.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  external.setValidationQuery(null);// VALIDATION_SQL);
  external.setPoolPreparedStatements(true);

  return external;
}
 
开发者ID:giiwa,项目名称:giiwa,代码行数:26,代码来源:RDB.java

示例3: getBaseDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private BasicDataSource getBaseDataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setUsername(dbUser);
    ds.setPassword(dbPass);
    ds.setDriverClassName(DRIVER);
    ds.setTestOnBorrow(true);
    ds.setValidationQuery("select 1");
    ds.setDefaultAutoCommit(false);
    return ds;
}
 
开发者ID:oneops,项目名称:oneops,代码行数:11,代码来源:DataSourceConfig.java

示例4: createDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private DataSource createDataSource(String url, String userName, String password, String driverClassName,
                                    DataMediaType dataMediaType, String encoding) {
    BasicDataSource dbcpDs = new BasicDataSource();

    dbcpDs.setRemoveAbandoned(true);
    dbcpDs.setLogAbandoned(true);
    dbcpDs.setTestOnBorrow(true);
    dbcpDs.setTestWhileIdle(true);

    // 动态的参数
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);

    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
        dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "true");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        if (StringUtils.isNotEmpty(encoding)) {
            dbcpDs.addConnectionProperty("characterEncoding", encoding);
        }
        dbcpDs.setValidationQuery("select 1");
    }

    return dbcpDs;
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:31,代码来源:AbstractDbDialectTest.java

示例5: initialize

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
/**
 * Create the underlying DBCP BasicDataSource with the 
 * default supported properties.
 */
private void initialize(
    String dbDriver, 
    String dbURL, 
    String dbUser,
    String dbPassword, 
    int maxConnections, 
    String dbValidationQuery) throws SQLException {
    if (dbURL == null) {
        throw new SQLException(
            "DBPool could not be created: DB URL cannot be null");
    }
    
    if (dbDriver == null) {
        throw new SQLException(
            "DBPool '" + dbURL + "' could not be created: " +
            "DB driver class name cannot be null!");
    }
    
    if (maxConnections < 0) {
        throw new SQLException(
            "DBPool '" + dbURL + "' could not be created: " + 
            "Max connections must be greater than zero!");
    }

    datasource = new BasicDataSource();
    datasource.setDriverClassName(dbDriver);
    datasource.setUrl(dbURL);
    datasource.setUsername(dbUser);
    datasource.setPassword(dbPassword);
    datasource.setMaxActive(maxConnections);
    if (dbValidationQuery != null) {
        datasource.setValidationQuery(dbValidationQuery);
    }
}
 
开发者ID:AsuraTeam,项目名称:asura,代码行数:39,代码来源:PoolingConnectionProvider.java

示例6: getValidationQuery

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
@Override
public void getValidationQuery() {
	BasicDataSource dataSource = createDataSource();
	dataSource.setValidationQuery("SELECT FROM FOO");
	assertThat(new CommonsDbcpDataSourcePoolMetadata(dataSource).getValidationQuery())
			.isEqualTo("SELECT FROM FOO");
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:8,代码来源:CommonsDbcpDataSourcePoolMetadataTests.java

示例7: setAdvancedConfig

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
@Override
public void setAdvancedConfig(BasicDataSource ds) {
    // 解决 java.sql.SQLException: Already closed. 的问题(连接池会自动关闭长时间没有使用的连接)
    ds.setValidationQuery("select 1 from dual");
}
 
开发者ID:smxc,项目名称:garlicts,代码行数:6,代码来源:DefaultDataSourceFactory.java

示例8: doCreateDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
protected DataSource doCreateDataSource(String url) {
    BasicDataSource dbcpDs = new BasicDataSource();

    dbcpDs.setInitialSize(initialSize);// 初始化连接池时创建的连接数
    dbcpDs.setMaxActive(maxActive);// 连接池允许的最大并发连接数,值为非正数时表示不限制
    dbcpDs.setMaxIdle(maxIdle);// 连接池中的最大空闲连接数,超过时,多余的空闲连接将会被释放,值为负数时表示不限制
    dbcpDs.setMinIdle(minIdle);// 连接池中的最小空闲连接数,低于此数值时将会创建所欠缺的连接,值为0时表示不创建
    dbcpDs.setMaxWait(maxWait);// 以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时表示无限等待
    dbcpDs.setRemoveAbandoned(true);// 是否清除已经超过removeAbandonedTimeout设置的无效连接
    dbcpDs.setLogAbandoned(true);// 当清除无效链接时是否在日志中记录清除信息的标志
    dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 以秒表示清除无效链接的时限
    dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// 确保连接池中没有已破损的连接
    dbcpDs.setTestOnBorrow(false);// 指定连接被调用时是否经过校验
    dbcpDs.setTestOnReturn(false);// 指定连接返回到池中时是否经过校验
    dbcpDs.setTestWhileIdle(true);// 指定连接进入空闲状态时是否经过空闲对象驱逐进程的校验
    dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 以毫秒表示空闲对象驱逐进程由运行状态进入休眠状态的时长,值为非正数时表示不运行任何空闲对象驱逐进程
    dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 以毫秒表示连接被空闲对象驱逐进程驱逐前在池中保持空闲状态的最小时间

    // 动态的参数
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);

    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
        dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 将0000-00-00的时间类型返回null
        dbcpDs.addConnectionProperty("yearIsDateType", "false");// 直接返回字符串,不做year转换date处理
        if (StringUtils.isNotEmpty(encoding)) {
            dbcpDs.addConnectionProperty("characterEncoding", encoding);
        }
        dbcpDs.setValidationQuery("select 1");
    } else {
        logger.error("ERROR ## Unknow database type");
    }

    return dbcpDs;
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:44,代码来源:MediaPushDataSource.java

示例9: createDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private DataSource createDataSource(String url, String userName, String password, String driverClassName,
                                    DataMediaType dataMediaType, String encoding) {
    BasicDataSource dbcpDs = new BasicDataSource();

    dbcpDs.setInitialSize(initialSize);// 初始化连接池时创建的连接数
    dbcpDs.setMaxActive(maxActive);// 连接池允许的最大并发连接数,值为非正数时表示不限制
    dbcpDs.setMaxIdle(maxIdle);// 连接池中的最大空闲连接数,超过时,多余的空闲连接将会被释放,值为负数时表示不限制
    dbcpDs.setMinIdle(minIdle);// 连接池中的最小空闲连接数,低于此数值时将会创建所欠缺的连接,值为0时表示不创建
    dbcpDs.setMaxWait(maxWait);// 以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时表示无限等待
    dbcpDs.setRemoveAbandoned(true);// 是否清除已经超过removeAbandonedTimeout设置的无效连接
    dbcpDs.setLogAbandoned(true);// 当清除无效链接时是否在日志中记录清除信息的标志
    dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 以秒表示清除无效链接的时限
    dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// 确保连接池中没有已破损的连接
    dbcpDs.setTestOnBorrow(false);// 指定连接被调用时是否经过校验
    dbcpDs.setTestOnReturn(false);// 指定连接返回到池中时是否经过校验
    dbcpDs.setTestWhileIdle(true);// 指定连接进入空闲状态时是否经过空闲对象驱逐进程的校验
    dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 以毫秒表示空闲对象驱逐进程由运行状态进入休眠状态的时长,值为非正数时表示不运行任何空闲对象驱逐进程
    dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 以毫秒表示连接被空闲对象驱逐进程驱逐前在池中保持空闲状态的最小时间

    // 动态的参数
    dbcpDs.setDriverClassName(driverClassName);
    dbcpDs.setUrl(url);
    dbcpDs.setUsername(userName);
    dbcpDs.setPassword(password);

    if (dataMediaType.isOracle()) {
        dbcpDs.addConnectionProperty("restrictGetTables", "true");
        dbcpDs.setValidationQuery("select 1 from dual");
    } else if (dataMediaType.isMysql()) {
        // open the batch mode for mysql since 5.1.8
        dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
        dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
        dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 将0000-00-00的时间类型返回null
        dbcpDs.addConnectionProperty("yearIsDateType", "false");// 直接返回字符串,不做year转换date处理
        dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// 返回时间类型的字符串,不做时区处理
        if (StringUtils.isNotEmpty(encoding)) {
            if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) {
                dbcpDs.addConnectionProperty("characterEncoding", "utf8");
                dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4"));
            } else {
                dbcpDs.addConnectionProperty("characterEncoding", encoding);
            }
        }
        dbcpDs.setValidationQuery("select 1");
    } else {
        logger.error("ERROR ## Unknow database type");
    }

    return dbcpDs;
}
 
开发者ID:luoyaogui,项目名称:otter-G,代码行数:51,代码来源:DBDataSourceService.java

示例10: createDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private BasicDataSource createDataSource(Map<String, String> properties, String urlPattern) {
	BasicDataSource dsPool = new BasicDataSource();

	dsPool.setDriverClassName(DSPropertyUtil.getPropertyStringValue("driver", properties, null, null, true));
	dsPool.setUsername(DSPropertyUtil.getPropertyStringValue("username", properties, null, null, true));
	dsPool.setPassword(DSPropertyUtil.getPropertyStringValue("password", properties, null, null, true));

	String url = DSPropertyUtil.getPropertyStringValue("url", properties, null, null, true);
	if (null != urlPattern) {
		url = DSPropertyUtil.replace(url, "{}", urlPattern);
	}
	dsPool.setUrl(url);
	dsPool.setPoolPreparedStatements(DSPropertyUtil.getPropertyBooleanValue("poolingStatements", properties, null, true, true)); // 开启池的prepared
																																	// 池功能
	dsPool.setRemoveAbandoned(DSPropertyUtil.getPropertyBooleanValue("removeAbandoned", properties, null, true, true));
	dsPool.setRemoveAbandonedTimeout(DSPropertyUtil.getPropertyIntegerValue("removeAbandonedTimeout", properties, null, 1000, true));
	dsPool.setLogAbandoned(DSPropertyUtil.getPropertyBooleanValue("logAbandoned", properties, null, true, true));

	dsPool.setInitialSize(DSPropertyUtil.getPropertyIntegerValue("initialSize", properties, null, 2, true));
	dsPool.setMaxActive(DSPropertyUtil.getPropertyIntegerValue("maxActive", properties, null, 8, true));
	dsPool.setMaxIdle(DSPropertyUtil.getPropertyIntegerValue("maxIdle", properties, null, 8, true));
	dsPool.setMinIdle(DSPropertyUtil.getPropertyIntegerValue("minIdle", properties, null, 0, true));
	dsPool.setMaxWait(DSPropertyUtil.getPropertyIntegerValue("maxWait", properties, null, 10000, true));

	dsPool.setTimeBetweenEvictionRunsMillis(DSPropertyUtil.getPropertyIntegerValue("timeBetweenEvictionRunsMillis", properties, null, -1, true));

	dsPool.setTestOnBorrow(DSPropertyUtil.getPropertyBooleanValue("testOnBorrow", properties, null, false, true));
	dsPool.setTestOnReturn(DSPropertyUtil.getPropertyBooleanValue("testOnReturn", properties, null, false, true));
	dsPool.setTestWhileIdle(DSPropertyUtil.getPropertyBooleanValue("testWhileIdle", properties, null, false, true));

	String validationQuery = DSPropertyUtil.getPropertyStringValue("validationQuery", properties, null, null, false);
	if (null != validationQuery) {
		dsPool.setValidationQuery(validationQuery);
	}
	int timeout = DSPropertyUtil.getPropertyIntegerValue("", properties, null, -1, false);
	if (timeout > -1) {
		dsPool.setValidationQueryTimeout(timeout);
	}

	dsPool.setNumTestsPerEvictionRun(DSPropertyUtil.getPropertyIntegerValue("numTestsPerEvictionRun", properties, null, 10, true));
	dsPool.setMinEvictableIdleTimeMillis(DSPropertyUtil.getPropertyIntegerValue("minEvictableIdleTimeMillis", properties, null, 60000, true));

	// mysql:select 1
	// oracle:select 1 from dual
	// sqlserver:select 1
	// jtds:select 1

	boolean openTest = DSPropertyUtil.getPropertyBooleanValue("openTest", properties, null, false, false);
	if (openTest) {
		try {
			Connection conn = dsPool.getConnection();
			conn.close();
			log.info("test open database success.");
		} catch (Exception e) {
			throw new DataSourceException("test open database error: " + e.getMessage(), e);
		}
	}
	return dsPool;
}
 
开发者ID:xsonorg,项目名称:tangyuan2,代码行数:60,代码来源:DBCPDataSourceCreater.java

示例11: createDataSource

import org.apache.commons.dbcp.BasicDataSource; //导入方法依赖的package包/类
private BasicDataSource createDataSource(Map<String, String> properties, Map<String, String> decryptProperties, String urlPattern) {
	BasicDataSource dsPool = new BasicDataSource();

	dsPool.setDriverClassName(getPropertyStringValue("driver", properties, null, null, true));
	dsPool.setUsername(getPropertyStringValue("username", properties, null, null, true));
	dsPool.setPassword(getPropertyStringValue("password", properties, null, null, true));

	String url = getPropertyStringValue("url", properties, null, null, true);
	if (null != urlPattern) {
		url = StringUtils.replace(url, "{}", urlPattern);
	}
	dsPool.setUrl(url);

	dsPool.setPoolPreparedStatements(getPropertyBooleanValue("poolingStatements", properties, null, true, true)); // 开启池的prepared statement 池功能
	dsPool.setRemoveAbandoned(getPropertyBooleanValue("removeAbandoned", properties, null, true, true));
	dsPool.setRemoveAbandonedTimeout(getPropertyIntegerValue("removeAbandonedTimeout", properties, null, 1000, true));
	dsPool.setLogAbandoned(getPropertyBooleanValue("logAbandoned", properties, null, true, true));

	dsPool.setInitialSize(getPropertyIntegerValue("initialSize", properties, null, 2, true));
	dsPool.setMaxActive(getPropertyIntegerValue("maxActive", properties, null, 8, true));
	dsPool.setMaxIdle(getPropertyIntegerValue("maxIdle", properties, null, 8, true));
	dsPool.setMinIdle(getPropertyIntegerValue("minIdle", properties, null, 0, true));
	dsPool.setMaxWait(getPropertyIntegerValue("maxWait", properties, null, 10000, true));

	dsPool.setTimeBetweenEvictionRunsMillis(getPropertyIntegerValue("timeBetweenEvictionRunsMillis", properties, null, -1, true));

	dsPool.setTestOnBorrow(getPropertyBooleanValue("testOnBorrow", properties, null, false, true));
	dsPool.setTestOnReturn(getPropertyBooleanValue("testOnReturn", properties, null, false, true));
	dsPool.setTestWhileIdle(getPropertyBooleanValue("testWhileIdle", properties, null, false, true));

	String validationQuery = getPropertyStringValue("validationQuery", properties, null, null, false);
	if (null != validationQuery) {
		dsPool.setValidationQuery(validationQuery);
	}
	// dsPool.setValidationQueryTimeout(timeout);

	dsPool.setNumTestsPerEvictionRun(getPropertyIntegerValue("numTestsPerEvictionRun", properties, null, 10, true));
	dsPool.setMinEvictableIdleTimeMillis(getPropertyIntegerValue("minEvictableIdleTimeMillis", properties, null, 60000, true));

	// mysql:select 1
	// oracle:select 1 from dual
	// sqlserver:select 1
	// jtds:select 1

	boolean openTest = getPropertyBooleanValue("openTest", properties, null, false, false);
	if (openTest) {
		try {
			Connection conn = dsPool.getConnection();
			conn.close();
			log.info("test open database success.");
		} catch (Exception e) {
			throw new DataSourceException("test open database error: " + e.getMessage(), e);
		}
	}
	return dsPool;
}
 
开发者ID:xsonorg,项目名称:tangyuan,代码行数:57,代码来源:DBCPDataSourceCreater.java


注:本文中的org.apache.commons.dbcp.BasicDataSource.setValidationQuery方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。