本文整理汇总了Java中com.mchange.v2.c3p0.ComboPooledDataSource.setTestConnectionOnCheckin方法的典型用法代码示例。如果您正苦于以下问题:Java ComboPooledDataSource.setTestConnectionOnCheckin方法的具体用法?Java ComboPooledDataSource.setTestConnectionOnCheckin怎么用?Java ComboPooledDataSource.setTestConnectionOnCheckin使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mchange.v2.c3p0.ComboPooledDataSource
的用法示例。
在下文中一共展示了ComboPooledDataSource.setTestConnectionOnCheckin方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createConnection
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
private ComboPooledDataSource createConnection() {
try {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://" + MYSQL_DATA.HOST + ":" + MYSQL_DATA.PORT + "/" + MYSQL_DATA.DATABASE);
cpds.setProperties(connectionProperties);
cpds.setInitialPoolSize(POOL_DATA.INITIAL_POOL_SIZE);
cpds.setMinPoolSize(POOL_DATA.MIN_POOL_SIZE);
cpds.setMaxPoolSize(POOL_DATA.MAX_POOL_SIZE);
cpds.setTestConnectionOnCheckin(POOL_DATA.TEST_CONNECTION_ON_CHECKIN);
cpds.setIdleConnectionTestPeriod(POOL_DATA.IDLE_CONNECTION_TEST_PERIOD);
return cpds;
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return null;
}
示例2: 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
示例3: 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());
}
示例4: L2DatabaseFactory
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public L2DatabaseFactory() throws SQLException
{
try
{
if (Config.DATABASE_MAX_CONNECTIONS < 2)
{
Config.DATABASE_MAX_CONNECTIONS = 2;
_log.warning("at least " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
}
_source = new ComboPooledDataSource();
_source.setAutoCommitOnClose(true);
_source.setInitialPoolSize(10);
_source.setMinPoolSize(10);
_source.setMaxPoolSize(Config.DATABASE_MAX_CONNECTIONS);
_source.setAcquireRetryAttempts(0); // try to obtain connections indefinitely (0 = never quit)
_source.setAcquireRetryDelay(500); // 500 miliseconds wait before try to acquire connection again
_source.setCheckoutTimeout(0); // 0 = wait indefinitely for new connection
// if pool is exhausted
_source.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time
// cause there is a "long" delay on acquire connection
// so taking more than one connection at once will make connection pooling
// more effective.
// this "connection_test_table" is automatically created if not already there
_source.setAutomaticTestTable("connection_test_table");
_source.setTestConnectionOnCheckin(false);
// testing OnCheckin used with IdleConnectionTestPeriod is faster than testing on checkout
_source.setIdleConnectionTestPeriod(3600); // test idle connection every 60 sec
_source.setMaxIdleTime(0); // 0 = idle connections never expire
// *THANKS* to connection testing configured above
// but I prefer to disconnect all connections not used
// for more than 1 hour
// enables statement caching, there is a "semi-bug" in c3p0 0.9.0 but in 0.9.0.2 and later it's fixed
_source.setMaxStatementsPerConnection(100);
_source.setBreakAfterAcquireFailure(false); // never fail if any way possible
// setting this to true will make
// c3p0 "crash" and refuse to work
// till restart thus making acquire
// errors "FATAL" ... we don't want that
// it should be possible to recover
_source.setDriverClass(Config.DATABASE_DRIVER);
_source.setJdbcUrl(Config.DATABASE_URL);
_source.setUser(Config.DATABASE_LOGIN);
_source.setPassword(Config.DATABASE_PASSWORD);
/* Test the connection */
_source.getConnection().close();
if (Config.DEBUG) _log.fine("Database Connection Working");
if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
_providerType = ProviderType.MsSql;
else
_providerType = ProviderType.MySql;
}
catch (SQLException x)
{
if (Config.DEBUG) _log.fine("Database Connection FAILED");
// rethrow the exception
throw x;
}
catch (Exception e)
{
if (Config.DEBUG) _log.fine("Database Connection FAILED");
throw new SQLException("could not init DB connection:"+e);
}
}
示例5: 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);
}
}
示例6: createFromConfig
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
private TransactionAwareDataSource createFromConfig(String instanceName, rapture.common.ConnectionInfo info) throws PropertyVetoException {
String url = PostgresConnectionInfoConfigurer.getUrl(info);
String user = info.getUsername();
validateConfig(url, user);
log.info("Host is " + url);
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDataSourceName(createDataSourceName(instanceName));
dataSource.setDriverClass(DRIVER_CLASS); // loads the jdbc driver
dataSource.setJdbcUrl(url);
dataSource.setUser(user);
dataSource.setCheckoutTimeout(DEFAULT_CHECKOUT_TIMEOUT);
String password = info.getPassword();
if (!StringUtils.isBlank(password)) {
dataSource.setPassword(password);
} else {
throw RaptureExceptionFactory.create("Password cannot be null!");
}
// pool size configuration
dataSource.setInitialPoolSize(getOptionAsInt(info, "initialPoolSize"));
dataSource.setMinPoolSize(getOptionAsInt(info, "minPoolSize"));
dataSource.setMaxPoolSize(getOptionAsInt(info, "maxPoolSize"));
dataSource.setMaxIdleTimeExcessConnections(getOptionAsInt(info, "maxIdleTimeExcessConnections"));
// statement size configuration
dataSource.setMaxStatements(getOptionAsInt(info, "maxStatements"));
dataSource.setStatementCacheNumDeferredCloseThreads(getOptionAsInt(info, "statementCacheNumDeferredCloseThreads"));
// connection testing
dataSource.setIdleConnectionTestPeriod(getOptionAsInt(info, "idleConnectionTestPeriod"));
boolean testConnectionOnCheckin = true;
if(info.getOption("testConnectionOnCheckin") != null) {
testConnectionOnCheckin = (boolean) info.getOption("testConnectionOnCheckin");
}
dataSource.setTestConnectionOnCheckin(testConnectionOnCheckin);
monitor.monitor(dataSource);
try {
Connection connection = DriverManager.getConnection(url, user, password);
connection.close();
} catch (SQLException e) {
throw RaptureExceptionFactory.create(ExceptionToString.format(e));
}
return new TransactionAwareDataSource(dataSource);
}
示例7: 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());
}
}
示例8: 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());
}
}
示例9: init
import com.mchange.v2.c3p0.ComboPooledDataSource; //导入方法依赖的package包/类
public void init(Map<String, String> configuration) {
try {
Class.forName("com.mysql.jdbc.Driver");
String connectURI = "jdbc:mysql://" + MYSQL_SERVER + ":"
+ MYSQL_PORT + "/" + MYSQL_DATABASE + "?"
+ "user=" + MYSQL_USER + "&password=" + MYSQL_PASSWORD;
datasource = new ComboPooledDataSource();
datasource.setJdbcUrl(connectURI);
datasource.setMaxPoolSize(16);
datasource.setMinPoolSize(4);
datasource.setInitialPoolSize(4);
datasource.setNumHelperThreads(6);
datasource.setTestConnectionOnCheckin(true);
datasource.setTestConnectionOnCheckout(true);
connection = datasource.getConnection();
preparedLookupStatement = connection
.prepareStatement("SELECT value FROM " + MYSQL_TABLE
+ " WHERE `key` = ?");
preparedDeleteStatement = connection
.prepareStatement("DELETE FROM " + MYSQL_TABLE
+ " WHERE `key` = ?");
preparedDeleteAllStatement = connection
.prepareStatement("DELETE FROM " + MYSQL_TABLE);
preparedInsertStatement = connection
.prepareStatement("INSERT INTO " + MYSQL_TABLE
+ " (`key`, value) VALUES (?, ?)");
preparedCreateTableStatement = connection
.prepareStatement("CREATE TABLE "
+ MYSQL_TABLE
+ " (`key` VARCHAR(255) NOT NULL, value BLOB NOT NULL, PRIMARY KEY (`key`))"
+ "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
preparedCreateDatabaseStatement = connection
.prepareStatement("CREATE DATABASE IF NOT EXISTS "
+ MYSQL_DATABASE);
// Check if database exist, on fail, create it
//
preparedCreateDatabaseStatement.executeUpdate();
// Check if table exist, on fail, create it
//
DatabaseMetaData dbm = connection.getMetaData();
resultSet = dbm.getTables(null, null, MYSQL_TABLE, null);
if (!resultSet.next()) {
preparedCreateTableStatement.executeUpdate();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}