Search in sources :

Example 6 with PoolProperties

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

the class DataSourceFactory method build.

@Override
public ManagedDataSource build(MetricRegistry metricRegistry, String name) {
    final Properties properties = new Properties();
    for (Map.Entry<String, String> property : this.properties.entrySet()) {
        properties.setProperty(property.getKey(), property.getValue());
    }
    final PoolProperties poolConfig = new PoolProperties();
    poolConfig.setAbandonWhenPercentageFull(abandonWhenPercentageFull);
    poolConfig.setAlternateUsernameAllowed(alternateUsernamesAllowed);
    poolConfig.setCommitOnReturn(commitOnReturn);
    poolConfig.setRollbackOnReturn(rollbackOnReturn);
    poolConfig.setDbProperties(properties);
    poolConfig.setDefaultAutoCommit(autoCommitByDefault);
    poolConfig.setDefaultCatalog(defaultCatalog);
    poolConfig.setDefaultReadOnly(readOnlyByDefault);
    poolConfig.setDefaultTransactionIsolation(defaultTransactionIsolation.get());
    poolConfig.setDriverClassName(driverClass);
    poolConfig.setFairQueue(useFairQueue);
    poolConfig.setInitialSize(initialSize);
    poolConfig.setInitSQL(initializationQuery);
    poolConfig.setLogAbandoned(logAbandonedConnections);
    poolConfig.setLogValidationErrors(logValidationErrors);
    poolConfig.setMaxActive(maxSize);
    poolConfig.setMaxIdle(maxSize);
    poolConfig.setMinIdle(minSize);
    if (getMaxConnectionAge().isPresent()) {
        poolConfig.setMaxAge(maxConnectionAge.toMilliseconds());
    }
    poolConfig.setMaxWait((int) maxWaitForConnection.toMilliseconds());
    poolConfig.setMinEvictableIdleTimeMillis((int) minIdleTime.toMilliseconds());
    poolConfig.setName(name);
    poolConfig.setUrl(url);
    poolConfig.setUsername(user);
    poolConfig.setPassword(user != null && password == null ? "" : password);
    poolConfig.setRemoveAbandoned(removeAbandoned);
    poolConfig.setRemoveAbandonedTimeout(Ints.saturatedCast(removeAbandonedTimeout.toSeconds()));
    poolConfig.setTestWhileIdle(checkConnectionWhileIdle);
    poolConfig.setValidationQuery(validationQuery);
    poolConfig.setTestOnBorrow(checkConnectionOnBorrow);
    poolConfig.setTestOnConnect(checkConnectionOnConnect);
    poolConfig.setTestOnReturn(checkConnectionOnReturn);
    poolConfig.setTimeBetweenEvictionRunsMillis((int) evictionInterval.toMilliseconds());
    poolConfig.setValidationInterval(validationInterval.toMilliseconds());
    if (getValidationQueryTimeout().isPresent()) {
        poolConfig.setValidationQueryTimeout((int) validationQueryTimeout.toSeconds());
    }
    validatorClassName.ifPresent(poolConfig::setValidatorClassName);
    return new ManagedPooledDataSource(poolConfig, metricRegistry);
}
Also used : Properties(java.util.Properties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 7 with PoolProperties

use of org.apache.tomcat.jdbc.pool.PoolProperties in project voltdb by VoltDB.

the class JDBCBenchmark method main.

// Application entry point
public static void main(String[] args) {
    try {
        KVConfig config = new KVConfig();
        config.parse(JDBCBenchmark.class.getName(), args);
        System.out.println(config.getConfigDumpString());
        // ---------------------------------------------------------------------------------------------------------------------------------------------------
        // We need only do this once, to "hot cache" the JDBC driver reference so the JVM may realize it's there.
        Class.forName(DRIVER_NAME);
        // Prepare the JDBC URL for the VoltDB driver
        String url = "jdbc:voltdb://" + config.servers;
        // Prepare the Datasource if choose to use a connection pool
        if (config.externalConnectionPool.equalsIgnoreCase(C3P0_CONNECTIONPOOL)) {
            useConnectionPool = true;
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            //loads the jdbc driver
            cpds.setDriverClass(DRIVER_NAME);
            cpds.setJdbcUrl(url);
            Ds = cpds;
        } else if (config.externalConnectionPool.equalsIgnoreCase(TOMCAT_CONNECTIONPOOL)) {
            useConnectionPool = true;
            // read the config file for connection pool
            String configName = "tomcat.properties";
            boolean useDefaultConnectionPoolConfig = true;
            Properties cpProperties = new Properties();
            try {
                FileInputStream fileInput = new FileInputStream(new File(configName));
                cpProperties.load(fileInput);
                fileInput.close();
                useDefaultConnectionPoolConfig = false;
            } catch (FileNotFoundException e) {
                System.out.println("connection pool property file '" + configName + "' not found, use default settings");
            }
            PoolProperties p = new PoolProperties();
            p.setUrl(url);
            p.setDriverClassName(DRIVER_NAME);
            if (useDefaultConnectionPoolConfig) {
                p.setInitialSize(config.threads + 1);
            } else {
                p.setInitialSize(Integer.parseInt(cpProperties.getProperty("tomcat.initialSize", "40")));
            }
            org.apache.tomcat.jdbc.pool.DataSource tomcatDs = new org.apache.tomcat.jdbc.pool.DataSource();
            tomcatDs.setPoolProperties(p);
            Ds = tomcatDs;
        } else if (config.externalConnectionPool.equalsIgnoreCase(BONECP_CONNECTIONPOOL)) {
            useConnectionPool = true;
            String configName = "bonecp.properties";
            boolean useDefaultConnectionPoolConfig = true;
            Properties cpProperties = new Properties();
            try {
                FileInputStream fileInput = new FileInputStream(new File(configName));
                cpProperties.load(fileInput);
                fileInput.close();
                useDefaultConnectionPoolConfig = false;
            } catch (FileNotFoundException e) {
                System.out.println("connection pool property file '" + configName + "' not found, use default settings");
            }
            BoneCPConfig p;
            if (useDefaultConnectionPoolConfig) {
                p = new BoneCPConfig();
                p.setDefaultReadOnly(false);
                p.setPartitionCount(config.threads / 2);
                p.setMaxConnectionsPerPartition(4);
            } else {
                p = new BoneCPConfig(cpProperties);
            }
            // set the JDBC url
            p.setJdbcUrl(url + "?enableSetReadOnly=true");
            BoneCPDataSource boneDs = new BoneCPDataSource(p);
            Ds = boneDs;
        } else if (config.externalConnectionPool.equalsIgnoreCase(HIKARI_CONNECTIONPOOL)) {
            useConnectionPool = true;
            HikariConfig p = new HikariConfig("hikari.properties");
            p.setDriverClassName(DRIVER_NAME);
            p.setJdbcUrl(url);
            HikariDataSource hiDs = new HikariDataSource(p);
            Ds = hiDs;
        } else {
            useConnectionPool = false;
            Ds = null;
        }
        // Get a client connection - we retry for a while in case the server hasn't started yet
        System.out.printf("Connecting to: %s\n", url);
        int sleep = 1000;
        while (true) {
            try {
                if (useConnectionPool) {
                    Ds.getConnection();
                    System.out.printf("Using Connection Pool: %s\n", config.externalConnectionPool);
                }
                Con = DriverManager.getConnection(url, "", "");
                break;
            } catch (Exception e) {
                System.err.printf("Connection failed - retrying in %d second(s).\n " + e, sleep / 1000);
                try {
                    Thread.sleep(sleep);
                } catch (Exception tie) {
                }
                if (sleep < 8000)
                    sleep += sleep;
            }
        }
        // Statistics manager objects from the connection, used to generate latency histogram
        ClientStatsContext fullStatsContext = ((IVoltDBConnection) Con).createStatsContext();
        periodicStatsContext = ((IVoltDBConnection) Con).createStatsContext();
        System.out.println("Connected.  Starting benchmark.");
        // Get a payload generator to create random Key-Value pairs to store in the database and process (uncompress) pairs retrieved from the database.
        final PayloadProcessor processor = new PayloadProcessor(config.keysize, config.minvaluesize, config.maxvaluesize, config.entropy, config.poolsize, config.usecompression);
        // Initialize the store
        if (config.preload) {
            System.out.print("Initializing data store... ");
            final PreparedStatement removeCS = Con.prepareStatement("DELETE FROM store;");
            final CallableStatement putCS = Con.prepareCall("{call STORE.upsert(?,?)}");
            for (int i = 0; i < config.poolsize; i++) {
                if (i == 0) {
                    removeCS.execute();
                }
                putCS.setString(1, String.format(processor.KeyFormat, i));
                putCS.setBytes(2, processor.generateForStore().getStoreValue());
                putCS.execute();
            }
            System.out.println(" Done.");
        }
        // start the stats
        fullStatsContext.fetchAndResetBaseline();
        periodicStatsContext.fetchAndResetBaseline();
        benchmarkStartTS = System.currentTimeMillis();
        // ---------------------------------------------------------------------------------------------------------------------------------------------------
        // Create a Timer task to display performance data on the operating procedures
        Timer timer = new Timer();
        TimerTask statsPrinting = new TimerTask() {

            @Override
            public void run() {
                printStatistics();
            }
        };
        timer.scheduleAtFixedRate(statsPrinting, config.displayinterval * 1000l, config.displayinterval * 1000l);
        // ---------------------------------------------------------------------------------------------------------------------------------------------------
        // Create multiple processing threads
        ArrayList<Thread> threads = new ArrayList<Thread>();
        for (int i = 0; i < config.threads; i++) threads.add(new Thread(new ClientThread(url, processor, config.duration, config.getputratio)));
        // Start threads
        for (Thread thread : threads) thread.start();
        // Wait for threads to complete
        for (Thread thread : threads) thread.join();
        // ---------------------------------------------------------------------------------------------------------------------------------------------------
        // We're done - stop the performance statistics display task
        timer.cancel();
        // ---------------------------------------------------------------------------------------------------------------------------------------------------
        // Now print application results:
        // stop and fetch the stats
        ClientStats stats = fullStatsContext.fetch().getStats();
        // 1. Store statistics as tracked by the application (ops counts, payload traffic)
        System.out.printf("\n-------------------------------------------------------------------------------------\n" + " Store Results\n" + "-------------------------------------------------------------------------------------\n\n" + "A total of %,d operations was posted...\n" + " - GETs: %,9d Operations (%,9d Misses/Failures)\n" + "         %,9d MB in compressed store data\n" + "         %,9d MB in uncompressed application data\n" + "         Network Throughput: %6.3f Gbps*\n\n" + " - PUTs: %,9d Operations (%,9d Failures)\n" + "         %,9d MB in compressed store data\n" + "         %,9d MB in uncompressed application data\n" + "         Network Throughput: %6.3f Gbps*\n\n" + " - Total Network Throughput: %6.3f Gbps*\n\n" + "* Figure includes key & value traffic but not database protocol overhead.\n" + "\n" + "-------------------------------------------------------------------------------------\n", GetStoreResults.get(0) + GetStoreResults.get(1) + PutStoreResults.get(0) + PutStoreResults.get(1), GetStoreResults.get(0), GetStoreResults.get(1), GetCompressionResults.get(0) / 1048576l, GetCompressionResults.get(1) / 1048576l, ((double) GetCompressionResults.get(0) + (GetStoreResults.get(0) + GetStoreResults.get(1)) * config.keysize) / (134217728d * config.duration), PutStoreResults.get(0), PutStoreResults.get(1), PutCompressionResults.get(0) / 1048576l, PutCompressionResults.get(1) / 1048576l, ((double) PutCompressionResults.get(0) + (PutStoreResults.get(0) + PutStoreResults.get(1)) * config.keysize) / (134217728d * config.duration), ((double) GetCompressionResults.get(0) + (GetStoreResults.get(0) + GetStoreResults.get(1)) * config.keysize) / (134217728d * config.duration) + ((double) PutCompressionResults.get(0) + (PutStoreResults.get(0) + PutStoreResults.get(1)) * config.keysize) / (134217728d * config.duration));
        System.out.println("\n\n-------------------------------------------------------------------------------------\n" + " Client Latency Statistics\n" + "-------------------------------------------------------------------------------------\n\n");
        System.out.printf("Average latency:               %,9.2f ms\n", stats.getAverageLatency());
        System.out.printf("10th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.1));
        System.out.printf("25th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.25));
        System.out.printf("50th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.5));
        System.out.printf("75th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.75));
        System.out.printf("90th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.9));
        System.out.printf("95th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.95));
        System.out.printf("99th percentile latency:       %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.99));
        System.out.printf("99.5th percentile latency:     %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.995));
        System.out.printf("99.9th percentile latency:     %,9.2f ms\n", stats.kPercentileLatencyAsDouble(.999));
        System.out.println("\n\n" + stats.latencyHistoReport());
        // Dump statistics to a CSV file
        Con.unwrap(IVoltDBConnection.class).saveStatistics(stats, config.statsfile);
        Con.close();
    // ---------------------------------------------------------------------------------------------------------------------------------------------------
    } catch (Exception x) {
        System.out.println("Exception: " + x);
        x.printStackTrace();
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Properties(java.util.Properties) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) ClientStats(org.voltdb.client.ClientStats) ClientStatsContext(org.voltdb.client.ClientStatsContext) TimerTask(java.util.TimerTask) CallableStatement(java.sql.CallableStatement) IVoltDBConnection(org.voltdb.jdbc.IVoltDBConnection) PreparedStatement(java.sql.PreparedStatement) HikariConfig(com.zaxxer.hikari.HikariConfig) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) DataSource(javax.sql.DataSource) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) Timer(java.util.Timer) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) File(java.io.File)

Example 8 with PoolProperties

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

the class DatabaseConfigView method getPoolProperties.

private PoolProperties getPoolProperties() {
    Map<String, Object> bufMap = new HashMap<>();
    tbDatabase.getItems().forEach(item -> {
        Object key = item.get("key");
        Object value = item.get("value");
        if (key == null)
            return;
        bufMap.put(key.toString(), value.toString());
    });
    String driver = bufMap.get(ResourceLoader.BASE_KEY_JDBC_DRIVER).toString();
    String url = bufMap.get(ResourceLoader.BASE_KEY_JDBC_URL).toString();
    String username = bufMap.get(ResourceLoader.BASE_KEY_JDBC_ID).toString();
    String password = bufMap.get(ResourceLoader.BASE_KEY_JDBC_PASS).toString();
    password = decryp(password);
    PoolProperties poolProperties = new PoolProperties();
    poolProperties.setDriverClassName(driver);
    poolProperties.setUrl(url);
    poolProperties.setUsername(username);
    poolProperties.setPassword(password);
    return poolProperties;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 9 with PoolProperties

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

the class DatabaseUrlManagementView method ping.

public void ping(int nRow) throws Exception {
    Map<String, Object> map = tbDatabase.getItems().get(nRow);
    Object object = map.get("dbms");
    if (object == null)
        return;
    String dbms = object.toString();
    if (dbms == null || dbms == "") {
        String msg = "not yet supported..." + dbms;
        LOGGER.error(msg);
        DialogUtil.showMessageDialog(msg);
    }
    DbUtil.ping(() -> {
        String driver = ConfigResourceLoader.getInstance().get("dbms." + dbms);
        String url = map.get(ResourceLoader.BASE_KEY_JDBC_URL).toString();
        String username = map.get(ResourceLoader.BASE_KEY_JDBC_ID) != null ? map.get(ResourceLoader.BASE_KEY_JDBC_ID).toString() : "";
        String password = map.get(ResourceLoader.BASE_KEY_JDBC_PASS) != null ? map.get(ResourceLoader.BASE_KEY_JDBC_PASS).toString() : "";
        password = decryp(password);
        PoolProperties poolProperties = new PoolProperties();
        poolProperties.setDriverClassName(driver);
        poolProperties.setUrl(url);
        poolProperties.setUsername(username);
        poolProperties.setPassword(password);
        return poolProperties;
    }, (bool) -> {
        String msg = "fail!";
        if (bool)
            msg = "success!";
        DialogUtil.showMessageDialog(msg);
    }, ex -> {
        LOGGER.info(ValueUtil.toString("ping test", ex));
    });
// LOGGER.debug(map.toString());
}
Also used : JSONObject(org.json.simple.JSONObject) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties)

Example 10 with PoolProperties

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

the class DataSourceLocator method createDataSource.

private DataSource createDataSource(String name) throws SQLException {
    DatabasePoolConfig poolConfig = DatabasePoolConfigParser.getInstance().getDatabasePoolConifg(name);
    DataSourceConfigure config = provider.getDataSourceConfigure(name);
    if (config == null && poolConfig == null) {
        throw new SQLException("Can not find any connection configure for " + name);
    }
    if (poolConfig == null) {
        // Create default connection pool configure
        poolConfig = new DatabasePoolConfig();
    }
    PoolProperties p = poolConfig.getPoolProperties();
    /**
		 * 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
		 */
    if (config != null) {
        p.setUrl(config.getConnectionUrl());
        p.setUsername(config.getUserName());
        p.setPassword(config.getPassword());
        p.setDriverClassName(config.getDriverClass());
    }
    org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(p);
    ds.createPool();
    logger.info("Datasource[name=" + name + ", Driver=" + p.getDriverClassName() + "] created.");
    return ds;
}
Also used : SQLException(java.sql.SQLException) DatabasePoolConfig(com.ctrip.platform.dal.dao.configure.DatabasePoolConfig) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) DataSource(javax.sql.DataSource)

Aggregations

PoolProperties (org.apache.tomcat.jdbc.pool.PoolProperties)22 Test (org.junit.Test)9 DataSource (org.apache.tomcat.jdbc.pool.DataSource)8 Connection (java.sql.Connection)5 DefaultProperties (org.apache.tomcat.jdbc.test.DefaultProperties)5 Properties (java.util.Properties)4 DataSource (javax.sql.DataSource)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)3 InterceptorDefinition (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition)3 TrapException (org.apache.tomcat.jdbc.pool.TrapException)3 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 InterceptorProperty (org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorProperty)2 DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)1 DatabasePoolConfig (com.ctrip.platform.dal.dao.configure.DatabasePoolConfig)1