本文整理汇总了Java中com.mchange.v2.c3p0.ComboPooledDataSource.setPreferredTestQuery方法的典型用法代码示例。如果您正苦于以下问题:Java ComboPooledDataSource.setPreferredTestQuery方法的具体用法?Java ComboPooledDataSource.setPreferredTestQuery怎么用?Java ComboPooledDataSource.setPreferredTestQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mchange.v2.c3p0.ComboPooledDataSource
的用法示例。
在下文中一共展示了ComboPooledDataSource.setPreferredTestQuery方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public static DataSource configDataSource(RepDatabase cfg)
throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(cfg.getDriverclass().trim());
// loads the jdbc driver
ds.setJdbcUrl(cfg.getUrl().trim());
ds.setUser(cfg.getUsername().trim());
ds.setPassword(cfg.getPassword().trim());
ds.setMaxStatements(20);//
ds.setMaxIdleTime(cfg.getMaxidletime());// 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃
ds.setMaxPoolSize(cfg.getMaxpoolsize());// 池最大连接数
ds.setMinPoolSize(cfg.getMinpoolsize());// 池最小连接数
// 检查有效性
if(StringHelper.isNotEmpty(cfg.getTestquerysql())&&cfg.getTestoeriod()!=null){
ds.setPreferredTestQuery(cfg.getTestquerysql());
ds.setIdleConnectionTestPeriod(cfg.getTestoeriod());// 每隔10分钟检查一次空闲连接的有效性
}
//获取连接超时
ds.setCheckoutTimeout(10000);
return ds;
}
示例2: DAOFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DAOFactory(String driverClassName, String jdbcURL, String userName, String userPassword, int initPoolSize,
int MinPoolSize, int MaxPoolSize, int maxIdleTime, int idleConnectionTestPeriod, String testQuerySQL) {
ds = new ComboPooledDataSource();
// 设置JDBC的Driver类
try {
ds.setDriverClass(driverClassName);
}
catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
// 设置JDBC的URL
ds.setJdbcUrl(jdbcURL);
// 设置数据库的登录用户名
ds.setUser(userName);
// 设置数据库的登录用户密码
ds.setPassword(userPassword);
// 设置连接池的最大连接数
ds.setMaxPoolSize(MaxPoolSize);
// 设置连接池的最小连接数
ds.setMinPoolSize(MinPoolSize);
// 设置初始化连接数
ds.setInitialPoolSize(initPoolSize);
// 设置最大闲置时间
ds.setMaxIdleTime(maxIdleTime);
// 设置测试SQL
ds.setPreferredTestQuery(testQuerySQL);
// 设置闲置测试周期
ds.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
// 增加单个连接的Statements数量
ds.setMaxStatements(0);
ds.setMaxStatementsPerConnection(200);
}
示例3: DAOFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DAOFactory(String facName, String driverClassName, String jdbcURL, String userName, String userPassword,
int initPoolSize, int MinPoolSize, int MaxPoolSize, int maxIdleTime, int idleConnectionTestPeriod,
String testQuerySQL) {
this.facName = facName;
ds = new ComboPooledDataSource();
// 设置JDBC的Driver类
try {
ds.setDriverClass(driverClassName);
}
catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
// 设置JDBC的URL
ds.setJdbcUrl(jdbcURL);
// 设置数据库的登录用户名
ds.setUser(userName);
// 设置数据库的登录用户密码
ds.setPassword(userPassword);
// 设置连接池的最大连接数
ds.setMaxPoolSize(MaxPoolSize);
// 设置连接池的最小连接数
ds.setMinPoolSize(MinPoolSize);
// 设置初始化连接数
ds.setInitialPoolSize(initPoolSize);
// 设置最大闲置时间
ds.setMaxIdleTime(maxIdleTime);
// 设置测试SQL
ds.setPreferredTestQuery(testQuerySQL);
// 设置闲置测试周期
ds.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
// 增加单个连接的Statements数量
ds.setMaxStatements(0);
// 连接池内单个连接所拥有的最大缓存statements数
ds.setMaxStatementsPerConnection(200);
// 获取空闲连接超时时间
ds.setCheckoutTimeout(10 * 1000);
}
示例4: init
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
public void init(String configurationName, Map<String, String> properties) {
this.configurationName = configurationName;
this.properties = new HashMap<>(properties);
String connectionKey = getConnectionKey();
try {
String driver = property("driver");
String userName = property("user");
String password = property("pass");
cpds = new ComboPooledDataSource();
cpds.setDriverClass(driver);
cpds.setJdbcUrl(getJdbcUrl());
cpds.setUser(userName);
cpds.setPassword(password);
int minPoolSize = Integer.parseInt(property("pool_min_size"));
int aquireIncrement = Integer.parseInt(property("pool_increment_by"));
int maxPoolSize = Integer.parseInt(property("pool_max_size"));
cpds.setMinPoolSize(minPoolSize);
cpds.setAcquireIncrement(aquireIncrement);
cpds.setMaxPoolSize(maxPoolSize);
cpds.setIdleConnectionTestPeriod(300);
cpds.setPreferredTestQuery("SELECT 1");
cpds.setTestConnectionOnCheckin(false);
cpds.setTestConnectionOnCheckout(false);
} catch (Throwable t) {
throw new RuntimeException("Unable to establich SQL connection for JDBC-key: " + connectionKey, t);
}
}
开发者ID:geetools,项目名称:geeCommerce-Java-Shop-Software-and-PIM,代码行数:35,代码来源:MySqlDatabaseConnection.java
示例5: start
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
@Override
public void start() {
cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(getServerURL());
cpds.setUser(getUsername());
cpds.setPassword(getPassword());
cpds.setMinPoolSize(getMinConnections());
cpds.setMaxPoolSize(getMaxConnections());
cpds.setIdleConnectionTestPeriod(getIdleTestInterval());
cpds.setTestConnectionOnCheckout(getTestBeforeUse());
cpds.setTestConnectionOnCheckin(getTestAfterUse());
cpds.setPreferredTestQuery(getTestSQL());
cpds.setMaxConnectionAge(getConnectionTimeout());
}
示例6: Dao
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public Dao(DbConfig config) {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(config.getUrl());
dataSource.setUser(config.getUser());
dataSource.setPassword(config.getPassword());
try {
dataSource.setDriverClass(config.getDriverClass());
} catch (PropertyVetoException e) {
throw new ServiceException(MessageFormat.format("{0} not found",
config.getDriverClass()));
}
logger.info("Dao init url={},user={},driverClass={}", new Object[] {
config.getUrl(), config.getUser(), config.getDriverClass() });
if (config.getInitialPoolSize() != 0) {
dataSource.setInitialPoolSize(config.getInitialPoolSize());
}
if (config.getMaxPoolSize() != 0) {
dataSource.setMaxPoolSize(config.getMaxPoolSize());
}
if (config.getMinPoolSize() != 0) {
dataSource.setMinPoolSize(config.getMinPoolSize());
}
if (config.getAcquireIncrement() != 0) {
dataSource.setAcquireIncrement(config.getAcquireIncrement());
}
if (config.getMaxIdleTime() != 0) {
dataSource.setMaxIdleTime(config.getMaxIdleTime());
}
if (config.getMaxStatements() != 0) {
dataSource.setMaxStatements(config.getMaxStatements());
}
if (config.getMaxStatementsPerConnection() != 0) {
dataSource.setMaxStatementsPerConnection(config
.getMaxStatementsPerConnection());
}
if (config.getPreferredTestQuery() != null) {
dataSource.setPreferredTestQuery(config.getPreferredTestQuery());
}
jdbc = new JDBCTemplate(dataSource);
}
示例7: initialize
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Create the underlying C3PO ComboPooledDataSource with the
* default supported properties.
* @throws SchedulerException
*/
private void initialize(
String dbDriver,
String dbURL,
String dbUser,
String dbPassword,
int maxConnections,
int maxStatementsPerConnection,
String dbValidationQuery,
boolean validateOnCheckout,
int idleValidationSeconds,
int maxIdleSeconds) throws SQLException, SchedulerException {
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 ComboPooledDataSource();
try {
datasource.setDriverClass(dbDriver);
} catch (PropertyVetoException e) {
throw new SchedulerException("Problem setting driver class name on datasource: " + e.getMessage(), e);
}
datasource.setJdbcUrl(dbURL);
datasource.setUser(dbUser);
datasource.setPassword(dbPassword);
datasource.setMaxPoolSize(maxConnections);
datasource.setMinPoolSize(1);
datasource.setMaxIdleTime(maxIdleSeconds);
datasource.setMaxStatementsPerConnection(maxStatementsPerConnection);
if (dbValidationQuery != null) {
datasource.setPreferredTestQuery(dbValidationQuery);
if(!validateOnCheckout)
datasource.setTestConnectionOnCheckin(true);
else
datasource.setTestConnectionOnCheckout(true);
datasource.setIdleConnectionTestPeriod(idleValidationSeconds);
}
}
示例8: createDataPool
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
protected DataSource createDataPool(PoolConfig conf) {
if (!conf.isValid()) {
throw new RuntimeException("数据库驱动为空或者数据库用户名或者密码或者连接字符串为空");
}
log.info("db config:{}", conf.toString());
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setJdbcUrl(conf.jdbcUrl);
cpds.setUser(conf.userName);
cpds.setPassword(conf.password);
try {
cpds.setDriverClass(conf.driverClass);
} catch (PropertyVetoException e) {
throw new RuntimeException("数据库驱动设置失败");
}
cpds.setPreferredTestQuery("select 1");
cpds.setIdleConnectionTestPeriod(60);
if (conf.initialPoolSize != null && !conf.initialPoolSize.isEmpty()) {
cpds.setInitialPoolSize(Integer.parseInt(conf.initialPoolSize));
}
if (conf.acquireIncrement != null && !conf.acquireIncrement.isEmpty()) {
cpds.setAcquireIncrement(Integer.parseInt(conf.acquireIncrement));
}
if (conf.maxPoolSize != null && !conf.maxPoolSize.isEmpty()) {
cpds.setMaxPoolSize(Integer.parseInt(conf.maxPoolSize));
}
if (conf.minPoolSize != null && !conf.minPoolSize.isEmpty()) {
cpds.setMinPoolSize(Integer.parseInt(conf.minPoolSize));
}
if (conf.checkoutTimeout != null && !conf.checkoutTimeout.isEmpty()) {
cpds.setCheckoutTimeout(Integer.parseInt(conf.checkoutTimeout));
}
if (conf.maxStatements != null && !conf.maxStatements.isEmpty()) {
cpds.setMaxStatements(Integer.parseInt(conf.maxStatements));
}
if (conf.maxStatementsPerConnection != null && !conf.maxStatementsPerConnection.isEmpty()) {
cpds.setMaxStatementsPerConnection(Integer.parseInt(conf.maxStatementsPerConnection));
}
if (conf.maxIdleTime != null && !conf.maxIdleTime.isEmpty()) {
cpds.setMaxIdleTime(Integer.parseInt(conf.maxIdleTime));
}
if (conf.unreturnedConnectionTimeout != null && !conf.unreturnedConnectionTimeout.isEmpty()) {
cpds.setDebugUnreturnedConnectionStackTraces(true);
cpds.setUnreturnedConnectionTimeout(Integer.parseInt(conf.unreturnedConnectionTimeout));
} else {
cpds.setDebugUnreturnedConnectionStackTraces(false);
}
return cpds;
}
示例9: createDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Creates {@link DataSource} based on {@link CConnection} properties.
*
* NOTE: this method never throws exception but just logs it.
*
* @param connection
* @return {@link DataSource} or <code>null</code> if it cannot be created
*/
private ComboPooledDataSource createDataSource(final CConnection connection)
{
try
{
System.setProperty("com.mchange.v2.log.MLog", com.mchange.v2.log.slf4j.Slf4jMLog.class.getName());
// System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "ALL");
final ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDataSourceName("AdempiereDS");
cpds.setDriverClass(DRIVER);
// loads the jdbc driver
cpds.setJdbcUrl(getConnectionURL(connection));
cpds.setUser(connection.getDbUid());
cpds.setPassword(connection.getDbPwd());
cpds.setPreferredTestQuery(DEFAULT_CONN_TEST_SQL);
cpds.setIdleConnectionTestPeriod(1200);
// cpds.setTestConnectionOnCheckin(true);
// cpds.setTestConnectionOnCheckout(true);
cpds.setAcquireRetryAttempts(2);
// Set checkout timeout to avoid forever locking when trying to connect to a not existing host.
cpds.setCheckoutTimeout(SystemUtils.getSystemProperty(CONFIG_CheckoutTimeout, 20 * 1000));
if (Ini.isClient())
{
cpds.setInitialPoolSize(1);
cpds.setMinPoolSize(1);
cpds.setMaxPoolSize(20);
cpds.setMaxIdleTimeExcessConnections(1200);
cpds.setMaxIdleTime(900);
m_maxbusyconnectionsThreshold = 15;
}
else
{
cpds.setInitialPoolSize(10);
cpds.setMinPoolSize(5);
cpds.setMaxPoolSize(150);
cpds.setMaxIdleTimeExcessConnections(1200);
cpds.setMaxIdleTime(1200);
m_maxbusyconnectionsThreshold = 120;
}
// the following sometimes kill active connection!
// cpds.setUnreturnedConnectionTimeout(1200);
// cpds.setDebugUnreturnedConnectionStackTraces(true);
// 04006: add a customizer to set the log level for message that are send to the client
// background: if there are too many messages sent (e.g. from a verbose and long-running DB function)
// then the whole JVM might suffer an OutOfMemoryError
cpds.setConnectionCustomizerClassName(DB_PostgreSQL_ConnectionCustomizer.class.getName());
return cpds;
}
catch (Exception ex)
{
throw new DBNoConnectionException("Could not initialise C3P0 Datasource", ex);
}
}
示例10: initDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Initializes the connection pool.
* @param dbSpec the object representing the chosen DB target system.
* @param jdbcUrl the JDBC URL to connect to.
* @throws SQLException
*/
private static void initDataSource(DBSpecifics dbSpec, String jdbcUrl)
throws SQLException {
dataSource = new ComboPooledDataSource();
String username = Settings.get(ArchiveSettings.DB_USERNAME);
if (!username.isEmpty()) {
dataSource.setUser(username);
}
String password = Settings.get(ArchiveSettings.DB_PASSWORD);
if (!password.isEmpty()) {
dataSource.setPassword(password);
}
try {
dataSource.setDriverClass(dbSpec.getDriverClassName());
} catch (PropertyVetoException e) {
final String message =
"Failed to set datasource JDBC driver class '"
+ dbSpec.getDriverClassName() + "'" + "\n";
throw new IOFailure(message, e);
}
dataSource.setJdbcUrl(jdbcUrl);
// Configure pool size
dataSource.setMinPoolSize(
Settings.getInt(ArchiveSettings.DB_POOL_MIN_SIZE));
dataSource.setMaxPoolSize(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_SIZE));
dataSource.setAcquireIncrement(
Settings.getInt(ArchiveSettings.DB_POOL_ACQ_INC));
// Configure idle connection testing
int testPeriod =
Settings.getInt(ArchiveSettings.DB_POOL_IDLE_CONN_TEST_PERIOD);
if (testPeriod > 0) {
dataSource.setIdleConnectionTestPeriod(testPeriod);
dataSource.setTestConnectionOnCheckin(
Settings.getBoolean(
ArchiveSettings.DB_POOL_IDLE_CONN_TEST_ON_CHECKIN));
String testQuery =
Settings.get(ArchiveSettings.DB_POOL_IDLE_CONN_TEST_QUERY);
if (!testQuery.isEmpty()) {
dataSource.setPreferredTestQuery(testQuery);
}
}
// Configure statement pooling
dataSource.setMaxStatements(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_STM));
dataSource.setMaxStatementsPerConnection(
Settings.getInt(ArchiveSettings.DB_POOL_MAX_STM_PER_CONN));
if (log.isInfoEnabled()) {
String msg =
"Connection pool initialized with the following values:";
msg += "\n- minPoolSize=" + dataSource.getMinPoolSize();
msg += "\n- maxPoolSize=" + dataSource.getMaxPoolSize();
msg += "\n- acquireIncrement=" + dataSource.getAcquireIncrement();
msg += "\n- maxStatements=" + dataSource.getMaxStatements();
msg += "\n- maxStatementsPerConnection="
+ dataSource.getMaxStatementsPerConnection();
msg += "\n- idleConnTestPeriod="
+ dataSource.getIdleConnectionTestPeriod();
msg += "\n- idleConnTestQuery='"
+ dataSource.getPreferredTestQuery() + "'";
msg += "\n- idleConnTestOnCheckin="
+ dataSource.isTestConnectionOnCheckin();
log.info(msg.toString());
}
}
示例11: initDataSource
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
/**
* Initializes the connection pool.
* @param dbSpec the object representing the chosen DB target system.
* @param jdbcUrl the JDBC URL to connect to.
* @throws SQLException
*/
private static void initDataSource(DBSpecifics dbSpec, String jdbcUrl)
throws SQLException {
dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass(dbSpec.getDriverClassName());
} catch (PropertyVetoException e) {
final String message =
"Failed to set datasource JDBC driver class '"
+ dbSpec.getDriverClassName() + "'" + "\n";
throw new IOFailure(message, e);
}
dataSource.setJdbcUrl(jdbcUrl);
String username = Settings.get(CommonSettings.DB_USERNAME);
if (!username.isEmpty()) {
dataSource.setUser(username);
}
String password = Settings.get(CommonSettings.DB_PASSWORD);
if (!password.isEmpty()) {
dataSource.setPassword(password);
}
// Configure pool size
dataSource.setMinPoolSize(
Settings.getInt(CommonSettings.DB_POOL_MIN_SIZE));
dataSource.setMaxPoolSize(
Settings.getInt(CommonSettings.DB_POOL_MAX_SIZE));
dataSource.setAcquireIncrement(
Settings.getInt(CommonSettings.DB_POOL_ACQ_INC));
// Configure idle connection testing
int testPeriod =
Settings.getInt(CommonSettings.DB_POOL_IDLE_CONN_TEST_PERIOD);
//TODO This looks odd. Why is checkin-testing inside this if statement?
if (testPeriod > 0) {
dataSource.setIdleConnectionTestPeriod(testPeriod);
dataSource.setTestConnectionOnCheckin(
Settings.getBoolean(
CommonSettings.DB_POOL_IDLE_CONN_TEST_ON_CHECKIN));
String testQuery =
Settings.get(CommonSettings.DB_POOL_IDLE_CONN_TEST_QUERY);
if (!testQuery.isEmpty()) {
dataSource.setPreferredTestQuery(testQuery);
}
}
// Configure statement pooling
dataSource.setMaxStatements(
Settings.getInt(CommonSettings.DB_POOL_MAX_STM));
dataSource.setMaxStatementsPerConnection(
Settings.getInt(CommonSettings.DB_POOL_MAX_STM_PER_CONN));
//dataSource.setTestConnectionOnCheckout(true);
//dataSource.setBreakAfterAcquireFailure(false);
//dataSource.setAcquireRetryAttempts(10000);
//dataSource.setAcquireRetryDelay(10);
if (log.isInfoEnabled()) {
String msg =
"Connection pool initialized with the following values:";
msg += "\n- minPoolSize=" + dataSource.getMinPoolSize();
msg += "\n- maxPoolSize=" + dataSource.getMaxPoolSize();
msg += "\n- acquireIncrement=" + dataSource.getAcquireIncrement();
msg += "\n- maxStatements=" + dataSource.getMaxStatements();
msg += "\n- maxStatementsPerConnection="
+ dataSource.getMaxStatementsPerConnection();
msg += "\n- idleConnTestPeriod="
+ dataSource.getIdleConnectionTestPeriod();
msg += "\n- idleConnTestQuery='"
+ dataSource.getPreferredTestQuery() + "'";
msg += "\n- idleConnTestOnCheckin="
+ dataSource.isTestConnectionOnCheckin();
log.info(msg.toString());
}
}