private PoolingDataSource setupDataSource(String connectURI, String user, String password) { // // First, we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // GenericObjectPool.Config config = new GenericObjectPool.Config(); config.maxActive = 150; config.maxIdle = 100; config.minIdle = 30; config.maxWait = 1000; ObjectPool connectionPool = new GenericObjectPool(null, config); // // Next, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // // Properties p = new Properties(); // p.setProperty("user", SQLConstants.USER_NAME); // p.setProperty("password", SQLConstants.PASSWORD); // p.setProperty("useUnicode", "true"); // p.setProperty("characterEncoding", "UTF-8"); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password); // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( // connectURI, p); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); // // Finally, we create the PoolingDriver itself, // passing in the object pool we created. // PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool); return poolingDataSource; }
private void init() { try { GenericObjectPool.Config poolConfig = new GenericObjectPool.Config(); poolConfig.maxActive = 100; poolConfig.maxIdle = 10; poolConfig.minIdle = 2; poolConfig.maxWait = 100; poolConfig.testWhileIdle = true; poolConfig.testOnBorrow = true; poolConfig.testOnReturn = true; poolConfig.minEvictableIdleTimeMillis = 10000; poolConfig.timeBetweenEvictionRunsMillis = 5000; poolConfig.numTestsPerEvictionRun = 10; // create JEDIS pool this.jedisPool = new JedisPool(poolConfig, HOST_NAME, PORT); // check connection checkConnection(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }