當前位置: 首頁>>代碼示例>>Java>>正文


Java ComboPooledDataSource.setTestConnectionOnCheckin方法代碼示例

本文整理匯總了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;
}
 
開發者ID:Simonsator,項目名稱:BungeecordPartyAndFriends,代碼行數:18,代碼來源:PoolSQLCommunication.java

示例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());
}
 
開發者ID:MoneyBeets,項目名稱:Narvaro,代碼行數:15,代碼來源:AbstractConnectionProvider.java

示例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);
	}
}
 
開發者ID:L2jBrasil,項目名稱:L2jBrasil,代碼行數:75,代碼來源:L2DatabaseFactory.java

示例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);
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:58,代碼來源:PoolingConnectionProvider.java

示例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);
    }
 
開發者ID:RapturePlatform,項目名稱:Rapture,代碼行數:50,代碼來源:ConnectionCacheLoader.java

示例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());
    }
}
 
開發者ID:netarchivesuite,項目名稱:netarchivesuite-svngit-migration,代碼行數:76,代碼來源:ArchiveDBConnection.java

示例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());
    }
}
 
開發者ID:netarchivesuite,項目名稱:netarchivesuite-svngit-migration,代碼行數:81,代碼來源:HarvestDBConnection.java

示例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);
    }

}
 
開發者ID:ManuelB,項目名稱:key-value-benchmark,代碼行數:59,代碼來源:MySQLKeyValueLookup.java


注:本文中的com.mchange.v2.c3p0.ComboPooledDataSource.setTestConnectionOnCheckin方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。