Search in sources :

Example 1 with BoneCPConfig

use of com.jolbox.bonecp.BoneCPConfig in project hive by apache.

the class TxnHandler method setupJdbcConnectionPool.

private static synchronized void setupJdbcConnectionPool(HiveConf conf) throws SQLException {
    if (connPool != null)
        return;
    String driverUrl = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORECONNECTURLKEY);
    String user = getMetastoreJdbcUser(conf);
    String passwd = getMetastoreJdbcPasswd(conf);
    String connectionPooler = conf.getVar(HiveConf.ConfVars.METASTORE_CONNECTION_POOLING_TYPE).toLowerCase();
    if ("bonecp".equals(connectionPooler)) {
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl(driverUrl);
        //if we are waiting for connection for 60s, something is really wrong
        //better raise an error than hang forever
        config.setConnectionTimeoutInMs(60000);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        config.setUser(user);
        config.setPassword(passwd);
        connPool = new BoneCPDataSource(config);
        // Enable retries to work around BONECP bug.
        doRetryOnConnPool = true;
    } else if ("dbcp".equals(connectionPooler)) {
        ObjectPool objectPool = new GenericObjectPool();
        ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd);
        // This doesn't get used, but it's still necessary, see
        // http://svn.apache.org/viewvc/commons/proper/dbcp/branches/DBCP_1_4_x_BRANCH/doc/ManualPoolingDataSourceExample.java?view=markup
        PoolableConnectionFactory poolConnFactory = new PoolableConnectionFactory(connFactory, objectPool, null, null, false, true);
        connPool = new PoolingDataSource(objectPool);
    } else if ("hikaricp".equals(connectionPooler)) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(driverUrl);
        config.setUsername(user);
        config.setPassword(passwd);
        connPool = new HikariDataSource(config);
    } else if ("none".equals(connectionPooler)) {
        LOG.info("Choosing not to pool JDBC connections");
        connPool = new NoPoolConnectionPool(conf);
    } else {
        throw new RuntimeException("Unknown JDBC connection pooling " + connectionPooler);
    }
}
Also used : PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) HikariConfig(com.zaxxer.hikari.HikariConfig) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) ObjectPool(org.apache.commons.pool.ObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory)

Example 2 with BoneCPConfig

use of com.jolbox.bonecp.BoneCPConfig 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 3 with BoneCPConfig

use of com.jolbox.bonecp.BoneCPConfig in project qpid-broker-j by apache.

the class BoneCPConnectionProviderTest method testCreateBoneCPConfig.

public void testCreateBoneCPConfig() throws Exception {
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("qpid.jdbcstore.bonecp.idleMaxAgeInMinutes", "123");
    attributes.put("qpid.jdbcstore.bonecp.connectionTimeoutInMs", "1234");
    attributes.put("qpid.jdbcstore.bonecp.connectionTestStatement", "select 1");
    attributes.put("qpid.jdbcstore.bonecp.logStatementsEnabled", "true");
    attributes.put("qpid.jdbcstore.bonecp.partitionCount", "12");
    String connectionUrl = "jdbc:mariadb://localhost:3306/test";
    String username = "usr";
    String password = "pwd";
    BoneCPConfig config = BoneCPConnectionProvider.createBoneCPConfig(connectionUrl, username, password, attributes);
    assertEquals(connectionUrl, config.getJdbcUrl());
    assertEquals(username, config.getUsername());
    assertEquals(password, config.getPassword());
    assertEquals("Unexpected idleMaxAgeInMinutes", 123, config.getIdleMaxAgeInMinutes());
    assertEquals("Unexpected connectionTimeout", 1234, config.getConnectionTimeoutInMs());
    assertEquals("Unexpected connectionTestStatement", "select 1", config.getConnectionTestStatement());
    assertEquals("Unexpected logStatementsEnabled", true, config.isLogStatementsEnabled());
    assertEquals("Unexpected maxConnectionsPerPartition", DEFAULT_MAX_CONNECTIONS_PER_PARTITION, config.getMaxConnectionsPerPartition());
    assertEquals("Unexpected minConnectionsPerPartition", DEFAULT_MIN_CONNECTIONS_PER_PARTITION, config.getMinConnectionsPerPartition());
    assertEquals("Unexpected partitionCount", 12, config.getPartitionCount());
}
Also used : HashMap(java.util.HashMap) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig)

Example 4 with BoneCPConfig

use of com.jolbox.bonecp.BoneCPConfig in project jaffa-framework by jaffa-projects.

the class BoneCPDataSourceConnectionFactory method configPool.

/**
 * Configures the bone cp pool
 * @throws ClassNotFoundException Exception thrown if the driver can not be loaded
 * @throws SQLException  if there is any sql issues with the connection
 */
private synchronized void configPool() throws ClassNotFoundException, SQLException {
    if (connectionPool != null) {
        return;
    }
    // load the driver
    Class.forName(getDriverClass());
    // set up the configuration
    BoneCPConfig config = new BoneCPConfig();
    config.setAcquireRetryDelay(getMaxConnTime() == null ? 1 : getMaxConnTime().longValue(), TimeUnit.SECONDS);
    config.setDisableConnectionTracking(true);
    config.setMaxConnectionsPerPartition((getMaximumConnections() == null ? 50 : getMaximumConnections()) / 4);
    config.setMinConnectionsPerPartition((getMinimumConnections() == null ? 20 : getMinimumConnections()) / 4);
    config.setMaxConnectionAge(getMaxCheckoutSeconds() == null ? 30 : getMaxCheckoutSeconds(), TimeUnit.SECONDS);
    config.setJdbcUrl(getUrl());
    config.setUsername(getUser());
    config.setPartitionCount(getPartitions() == null || getPartitions() <= 0 ? 1 : getPartitions());
    config.setAcquireRetryAttempts(getRetryAttempts() == null ? 10 : getRetryAttempts());
    config.setPassword(getPassword());
    // create the pool
    connectionPool = new BoneCP(config);
}
Also used : BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) BoneCP(com.jolbox.bonecp.BoneCP)

Example 5 with BoneCPConfig

use of com.jolbox.bonecp.BoneCPConfig in project Village_Defense by Plajer.

the class MySQLConnectionManager method configureConnPool.

public void configureConnPool() {
    FileConfiguration databaseConfig = ConfigurationManager.getConfig("mysql");
    try {
        // also you need the MySQL driver
        Class.forName("com.mysql.jdbc.Driver");
        plugin.getLogger().info("Creating BoneCP Configuration...");
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl(databaseConfig.getString("address"));
        config.setUsername(databaseConfig.getString("user"));
        config.setPassword(databaseConfig.getString("password"));
        // if you say 5 here, there will be 10 connection available
        config.setMinConnectionsPerPartition(databaseConfig.getInt("min-connections"));
        config.setMaxConnectionsPerPartition(databaseConfig.getInt("max-connections"));
        // 2*5 = 10 connection will be available
        config.setPartitionCount(2);
        // config.setLazyInit(true); //depends on the application usage you should chose lazy or not
        // setting Lazy true means BoneCP won't open any connections before you request a one from it.
        plugin.getLogger().info("Setting up MySQL Connection pool...");
        // setup the connection pool
        connectionPool = new BoneCP(config);
        plugin.getLogger().info("Connection pool successfully configured. ");
        plugin.getLogger().info("Total connections ==> " + connectionPool.getTotalCreatedConnections());
    } catch (Exception e) {
        e.printStackTrace();
        BigTextUtils.errorOccured();
        Bukkit.getConsoleSender().sendMessage("Cannot save contents to MySQL database!");
        Bukkit.getConsoleSender().sendMessage("Check configuration of mysql.yml file or disable mysql option in config.yml");
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) BoneCP(com.jolbox.bonecp.BoneCP) SQLException(java.sql.SQLException)

Aggregations

BoneCPConfig (com.jolbox.bonecp.BoneCPConfig)10 SQLException (java.sql.SQLException)6 BoneCP (com.jolbox.bonecp.BoneCP)5 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)4 HashMap (java.util.HashMap)4 Connection (java.sql.Connection)3 Properties (java.util.Properties)3 HikariConfig (com.zaxxer.hikari.HikariConfig)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1