Search in sources :

Example 21 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project Gargoyle by callakrsos.

the class DbUtil method ping.

/**
	 * 데이터베이스 접속 여부 확인
	 *
	 * @param conSupplier
	 *            커넥션정보를 제공
	 * @param onSuccess
	 *            접속성공시 처리
	 * @param exHandler
	 *            에러발생시 처리
	 * @param timeoutSec
	 *            핑테스트 대기시간 (Secound)
	 * @return 접속 성공여부
	 */
public static void ping(Supplier<PoolProperties> conSupplier, Consumer<Boolean> onSuccess, Consumer<Throwable> exHandler, int timeoutSec) {
    boolean result = false;
    PoolProperties prop = conSupplier.get();
    String driverClassName = prop.getDriverClassName();
    String url = prop.getUrl();
    String userName = prop.getUsername();
    String password = prop.getPassword();
    try (Connection connection = getConnection(driverClassName, url, userName, password, timeoutSec)) {
        String pingSQL = ConfigResourceLoader.getInstance().get(ConfigResourceLoader.SQL_PING, driverClassName);
        // 리스너들에게 공지
        noticeQuery(pingSQL);
        if (connection != null) {
            // connection.setAutoCommit(false);
            // try {
            // // postgresql was not supported...
            // Oracle에서 관련 에러 발생.. Connection객체를 얻어오는 과정과
            // executeQuery처리결과만으로 판단
            // connection.setNetworkTimeout(Executors.newSingleThreadExecutor(),
            // 5000);
            // } catch (Exception e) {
            //
            // }
            ResultSet executeQuery = connection.createStatement().executeQuery(pingSQL);
            result = executeQuery.next();
        }
    } catch (Throwable e) {
        exHandler.accept(e);
    }
    onSuccess.accept(result);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 22 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project dal by ctripcorp.

the class DatabasePoolConfigParser method parseResource.

private DatabasePoolConfig parseResource(Node resource) {
    DatabasePoolConfig poolConfig = new DatabasePoolConfig();
    poolConfig.setName(getAttribute(resource, NAME));
    Map<String, String> map = new HashMap<>();
    poolConfig.setMap(map);
    PoolProperties prop = poolConfig.getPoolProperties();
    // provider is set
    if (hasAttribute(resource, USER_NAME)) {
        prop.setUsername(getAttribute(resource, USER_NAME));
    }
    if (hasAttribute(resource, PASSWORD)) {
        prop.setPassword(getAttribute(resource, PASSWORD));
    }
    if (hasAttribute(resource, CONNECTION_URL)) {
        prop.setUrl(getAttribute(resource, CONNECTION_URL));
    }
    if (hasAttribute(resource, DRIVER_CLASS_NAME)) {
        prop.setDriverClassName(getAttribute(resource, DRIVER_CLASS_NAME));
    }
    // The following are common options
    if (hasAttribute(resource, TESTWHILEIDLE)) {
        String value = getAttribute(resource, TESTWHILEIDLE);
        boolean testWhileIdle = Boolean.parseBoolean(value);
        prop.setTestWhileIdle(testWhileIdle);
        map.put(TESTWHILEIDLE, value);
    }
    if (hasAttribute(resource, TESTONBORROW)) {
        String value = getAttribute(resource, TESTONBORROW);
        boolean testOnBorrow = Boolean.parseBoolean(value);
        prop.setTestOnBorrow(testOnBorrow);
        map.put(TESTONBORROW, value);
    }
    if (hasAttribute(resource, TESTONRETURN)) {
        String value = getAttribute(resource, TESTONRETURN);
        boolean testOnReturn = Boolean.parseBoolean(value);
        prop.setTestOnReturn(testOnReturn);
        map.put(TESTONRETURN, value);
    }
    if (hasAttribute(resource, VALIDATIONQUERY)) {
        String validationQuery = getAttribute(resource, VALIDATIONQUERY);
        prop.setValidationQuery(validationQuery);
        map.put(VALIDATIONQUERY, validationQuery);
    }
    if (hasAttribute(resource, VALIDATIONINTERVAL)) {
        String value = getAttribute(resource, VALIDATIONINTERVAL);
        long validationInterval = Long.parseLong(value);
        prop.setValidationInterval(validationInterval);
        map.put(VALIDATIONINTERVAL, value);
    }
    if (hasAttribute(resource, TIMEBETWEENEVICTIONRUNSMILLIS)) {
        String value = getAttribute(resource, TIMEBETWEENEVICTIONRUNSMILLIS);
        int timeBetweenEvictionRunsMillis = Integer.parseInt(value);
        prop.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        map.put(TIMEBETWEENEVICTIONRUNSMILLIS, value);
    }
    if (hasAttribute(resource, MAX_AGE)) {
        String value = getAttribute(resource, MAX_AGE);
        int maxAge = Integer.parseInt(value);
        prop.setMaxAge(maxAge);
        map.put(MAX_AGE, value);
    }
    if (hasAttribute(resource, MAXACTIVE)) {
        String value = getAttribute(resource, MAXACTIVE);
        int maxActive = Integer.parseInt(value);
        prop.setMaxActive(maxActive);
        map.put(MAXACTIVE, value);
    }
    if (hasAttribute(resource, MINIDLE)) {
        String value = getAttribute(resource, MINIDLE);
        int minIdle = Integer.parseInt(value);
        prop.setMinIdle(minIdle);
        map.put(MINIDLE, value);
    }
    if (hasAttribute(resource, MAXWAIT)) {
        String value = getAttribute(resource, MAXWAIT);
        int maxWait = Integer.parseInt(value);
        prop.setMaxWait(maxWait);
        map.put(MAXWAIT, value);
    }
    if (hasAttribute(resource, INITIALSIZE)) {
        String value = getAttribute(resource, INITIALSIZE);
        int initialSize = Integer.parseInt(value);
        prop.setInitialSize(initialSize);
        map.put(INITIALSIZE, value);
    }
    if (hasAttribute(resource, REMOVEABANDONEDTIMEOUT)) {
        String value = getAttribute(resource, REMOVEABANDONEDTIMEOUT);
        int removeAbandonedTimeout = Integer.parseInt(value);
        prop.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        map.put(REMOVEABANDONEDTIMEOUT, value);
    }
    if (hasAttribute(resource, REMOVEABANDONED)) {
        String value = getAttribute(resource, REMOVEABANDONED);
        boolean removeAbandoned = Boolean.parseBoolean(value);
        prop.setRemoveAbandoned(removeAbandoned);
        map.put(REMOVEABANDONED, value);
    }
    if (hasAttribute(resource, LOGABANDONED)) {
        String value = getAttribute(resource, LOGABANDONED);
        boolean logAbandoned = Boolean.parseBoolean(value);
        prop.setLogAbandoned(logAbandoned);
        map.put(LOGABANDONED, value);
    }
    if (hasAttribute(resource, MINEVICTABLEIDLETIMEMILLIS)) {
        String value = getAttribute(resource, MINEVICTABLEIDLETIMEMILLIS);
        int minEvictableIdleTimeMillis = Integer.parseInt(value);
        prop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        map.put(MINEVICTABLEIDLETIMEMILLIS, value);
    }
    if (hasAttribute(resource, INIT_SQL)) {
        String value = getAttribute(resource, INIT_SQL);
        prop.setInitSQL(value);
        map.put(INIT_SQL, value);
    }
    if (hasAttribute(resource, INIT_SQL2)) {
        String value = getAttribute(resource, INIT_SQL2);
        prop.setInitSQL(value);
        map.put(INIT_SQL2, value);
    }
    /**
         * Special handing for connectionProperties and option. If connectionProperties is not set, we will use option's
         * value if connectionProperties is set, we will ignore option's value
         */
    if (hasAttribute(resource, CONNECTIONPROPERTIES)) {
        String value = getAttribute(resource, CONNECTIONPROPERTIES);
        prop.setConnectionProperties(value);
        map.put(CONNECTIONPROPERTIES, value);
    } else {
        if (hasAttribute(resource, OPTION)) {
            String value = getAttribute(resource, OPTION);
            prop.setConnectionProperties(value);
            map.put(CONNECTIONPROPERTIES, value);
        }
    }
    if (hasAttribute(resource, VALIDATORCLASSNAME)) {
        String value = getAttribute(resource, VALIDATORCLASSNAME);
        prop.setValidatorClassName(value);
        map.put(VALIDATORCLASSNAME, value);
    }
    return poolConfig;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 23 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project ocvn by devgateway.

the class DatabaseConfiguration method dataSource.

/**
     * Creates a {@link javax.sql.DataSource} based on Tomcat {@link DataSource}
     * 
     * @return
     */
@Bean
@DependsOn(value = { "derbyServer" })
public DataSource dataSource() {
    PoolProperties pp = new PoolProperties();
    pp.setJmxEnabled(true);
    pp.setDefaultTransactionIsolation(springDatasourceTransactionIsolation);
    pp.setInitialSize(springDatasourceInitialSize);
    pp.setMaxActive(springDatasourceMaxActive);
    DataSource dataSource = new DataSource(pp);
    dataSource.setUrl(springDatasourceUrl);
    dataSource.setUsername(springDatasourceUsername);
    dataSource.setPassword(springDatasourcePassword);
    dataSource.setDriverClassName(springDatasourceDriverClassName);
    return dataSource;
}
Also used : PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(org.apache.tomcat.jdbc.pool.DataSource) DependsOn(org.springframework.context.annotation.DependsOn) Bean(org.springframework.context.annotation.Bean)

Example 24 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project dal by ctripcorp.

the class DatabasePoolConfigParser method getDatabasePoolConfig.

public DatabasePoolConfig getDatabasePoolConfig(String name) {
    DataSourceConfigure configure = DataSourceConfigureLocator.getInstance().getDataSourceConfigure(name);
    PoolProperties poolProperties = new PoolProperties();
    poolProperties.setTestWhileIdle(configure.getBooleanProperty(TESTWHILEIDLE, DEFAULT_TESTWHILEIDLE));
    poolProperties.setTestOnBorrow(configure.getBooleanProperty(TESTONBORROW, DEFAULT_TESTONBORROW));
    poolProperties.setTestOnReturn(configure.getBooleanProperty(TESTONRETURN, DEFAULT_TESTONRETURN));
    poolProperties.setValidationQuery(configure.getProperty(VALIDATIONQUERY, DEFAULT_VALIDATIONQUERY));
    poolProperties.setValidationQueryTimeout(configure.getIntProperty(VALIDATIONQUERYTIMEOUT, DEFAULT_VALIDATIONQUERYTIMEOUT));
    poolProperties.setValidationInterval(configure.getLongProperty(VALIDATIONINTERVAL, DEFAULT_VALIDATIONINTERVAL));
    poolProperties.setTimeBetweenEvictionRunsMillis(configure.getIntProperty(TIMEBETWEENEVICTIONRUNSMILLIS, DEFAULT_TIMEBETWEENEVICTIONRUNSMILLIS));
    poolProperties.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTABLEIDLETIMEMILLIS);
    poolProperties.setMaxAge(configure.getIntProperty(MAX_AGE, DEFAULT_MAXAGE));
    poolProperties.setMaxActive(configure.getIntProperty(MAXACTIVE, DEFAULT_MAXACTIVE));
    poolProperties.setMinIdle(configure.getIntProperty(MINIDLE, DEFAULT_MINIDLE));
    poolProperties.setMaxWait(configure.getIntProperty(MAXWAIT, DEFAULT_MAXWAIT));
    poolProperties.setInitialSize(configure.getIntProperty(INITIALSIZE, DEFAULT_INITIALSIZE));
    poolProperties.setRemoveAbandonedTimeout(configure.getIntProperty(REMOVEABANDONEDTIMEOUT, DEFAULT_REMOVEABANDONEDTIMEOUT));
    poolProperties.setRemoveAbandoned(configure.getBooleanProperty(REMOVEABANDONED, DEFAULT_REMOVEABANDONED));
    poolProperties.setLogAbandoned(configure.getBooleanProperty(LOGABANDONED, DEFAULT_LOGABANDONED));
    poolProperties.setConnectionProperties(configure.getProperty(CONNECTIONPROPERTIES, DEFAULT_CONNECTIONPROPERTIES));
    poolProperties.setValidatorClassName(configure.getProperty(VALIDATORCLASSNAME, DEFAULT_VALIDATORCLASSNAME));
    poolProperties.setJmxEnabled(DEFAULT_JMXENABLED);
    poolProperties.setJdbcInterceptors(configure.getProperty(JDBC_INTERCEPTORS, DEFAULT_JDBCINTERCEPTORS));
    return new DatabasePoolConfig(poolProperties);
}
Also used : PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 25 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project dal by ctripcorp.

the class PoolPropertiesHelper method convert.

public PoolProperties convert(DataSourceConfigure config) {
    PoolProperties properties = new PoolProperties();
    /**
     * It is assumed that user name/password/url/driver class name are provided in pool config If not, it should be
     * provided by the config provider
     */
    properties.setUrl(config.getConnectionUrl());
    properties.setUsername(config.getUserName());
    properties.setPassword(config.getPassword());
    properties.setDriverClassName(config.getDriverClass());
    properties.setTestWhileIdle(config.getBooleanProperty(TESTWHILEIDLE, DEFAULT_TESTWHILEIDLE));
    properties.setTestOnBorrow(config.getBooleanProperty(TESTONBORROW, DEFAULT_TESTONBORROW));
    properties.setTestOnReturn(config.getBooleanProperty(TESTONRETURN, DEFAULT_TESTONRETURN));
    properties.setValidationQuery(config.getProperty(VALIDATIONQUERY, DEFAULT_VALIDATIONQUERY));
    properties.setValidationQueryTimeout(config.getIntProperty(VALIDATIONQUERYTIMEOUT, DEFAULT_VALIDATIONQUERYTIMEOUT));
    properties.setValidationInterval(config.getLongProperty(VALIDATIONINTERVAL, DEFAULT_VALIDATIONINTERVAL));
    properties.setTimeBetweenEvictionRunsMillis(config.getIntProperty(TIMEBETWEENEVICTIONRUNSMILLIS, DEFAULT_TIMEBETWEENEVICTIONRUNSMILLIS));
    properties.setMinEvictableIdleTimeMillis(config.getIntProperty(MINEVICTABLEIDLETIMEMILLIS, DEFAULT_MINEVICTABLEIDLETIMEMILLIS));
    properties.setMaxAge(config.getIntProperty(MAX_AGE, DEFAULT_MAXAGE));
    properties.setMaxActive(config.getIntProperty(MAXACTIVE, DEFAULT_MAXACTIVE));
    properties.setMinIdle(config.getIntProperty(MINIDLE, DEFAULT_MINIDLE));
    properties.setMaxWait(config.getIntProperty(MAXWAIT, DEFAULT_MAXWAIT));
    properties.setInitialSize(config.getIntProperty(INITIALSIZE, DEFAULT_INITIALSIZE));
    properties.setRemoveAbandonedTimeout(config.getIntProperty(REMOVEABANDONEDTIMEOUT, DEFAULT_REMOVEABANDONEDTIMEOUT));
    properties.setRemoveAbandoned(config.getBooleanProperty(REMOVEABANDONED, DEFAULT_REMOVEABANDONED));
    properties.setLogAbandoned(config.getBooleanProperty(LOGABANDONED, DEFAULT_LOGABANDONED));
    properties.setConnectionProperties(config.getProperty(CONNECTIONPROPERTIES, DEFAULT_CONNECTIONPROPERTIES));
    properties.setValidatorClassName(config.getProperty(VALIDATORCLASSNAME, DEFAULT_VALIDATORCLASSNAME));
    String initSQL = config.getProperty(INIT_SQL);
    if (initSQL != null && !initSQL.isEmpty())
        properties.setInitSQL(initSQL);
    String initSQL2 = config.getProperty(INIT_SQL2);
    if (initSQL2 != null && !initSQL2.isEmpty())
        properties.setInitSQL(initSQL2);
    // This are current hard coded as default value
    properties.setJmxEnabled(DEFAULT_JMXENABLED);
    properties.setJdbcInterceptors(config.getProperty(JDBC_INTERCEPTORS, DEFAULT_JDBCINTERCEPTORS));
    return properties;
}
Also used : PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Aggregations

PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)26 DataSource (org.apache.tomcat.jdbc.pool.DataSource)10 Test (org.junit.Test)10 Connection (java.sql.Connection)6 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 Properties (java.util.Properties)4 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 DataSource (javax.sql.DataSource)3 InterceptorDefinition (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition)3 TrapException (org.apache.tomcat.jdbc.pool.TrapException)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ConnectionPool (org.apache.tomcat.jdbc.pool.ConnectionPool)2 InterceptorProperty (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty)2