本文整理匯總了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());
}
}