tencent cloud

Feedback

Sample Code of Jedis Connection Pool

Last updated: 2023-10-20 10:43:18

    Prerequisite

    Download and install Jedis. The latest version is recommended.

    Sample code

    Sample code of the connection pool and the meaning of the parameters are as follows:
    Parameter
    Description
    Suggestion
    setMaxTotal
    Maximum connections in the connection pool
    It is subjected to the factors like the volume of concurrent business, access delay, and maximum connections.
    setMaxIdle
    Maximum idle connections in the connection pool
    Set it to a value same as setMaxTotal
    setMinIdle
    Minimum idle connections in the connection pool
    Set it the same as setMaxTotal
    timeout
    Timeout period
    It is set based on your business model and the network linkage performance.
    When network latency is low and your businesses are very sensitive to service latency, set the value from 50 to 100 ms.
    If your business has high latency tolerance or large volume of key-value data, set it to 500 ms or 1,000 ms.
    setTestOnBorrow
    Whether to test the connection when obtaining it in a connection pool
    When the value is true, connection.isValid() will be called for the connection test. This ensures the availability of the obtained connection while consuming QPS performance.
    When the value is false, connection test will not be performed. The speed of obtaining a connection can be improved, but available connections may not be obtained.
    setTestOnReturn
    Whether to perform verification when the connection is returned to the connection pool.
    When the value is true, connection.isValid() will be called when returning the connection, ensuring the connections are valid.
    When the value is false, connection test will not be performed. The speed of returning a connection can be improved, but the returned connections may be unavailable.
    JedisPoolConfig config = new JedisPoolConfig();
    // Maximum idle connections, which can't exceed the maximum connections of Redis instances
    config.setMaxIdle(200);
    // Maximum connections, which can't exceed the maximum connections of Redis instances
    config.setMaxTotal(200);
    // Minimum idle connections in the connection pool
    config.setMinIdle(20);
    // Maximum wait time when the connections are used up
    config.setMaxWaitMillis(3000);
    // When an object is obtained from the connection pool, a ping check will be performed first. If the check fails, the object will be removed and destroyed.
    config.setTestOnBorrow(false);
    // When a connection is returned, a check will be performed first. Once the check fails, the connection will be terminated.
    config.setTestOnReturn(false);
    // Set the connection pool mode to “queue”
    config.setLifo(false);
    // Set the minimum connections
    config.setTimeBetweenEvictionRunsMillis(3000);
    // Replace the values of "host" and "password" with the connection address and password of the instance respectively
    String host = "192.xx.xx.195";
    String password = "123ad6aq";
    // Read/write timeout in ms
    int timeout = 2000;
    int port = 6379;
    JedisPool pool = new JedisPool(config,host,port,timeout,password);
    Jedis jedis = null;
    boolean broken = false;
    try
    {
    jedis = pool.getResource();
    /// ... do stuff here ... for example
    jedis.set("redis", "tencent");
    String foobar = jedis.get("redis");
    jedis.zadd("tec", 0, "a");
    jedis.zadd("tec", 0, "b");
    Set < String > sose = jedis.zrange("tec", 0, -1);
    }
    catch(Exception e)
    {
    broken = true;
    }
    finally
    {
    if(broken)
    {
    pool.returnBrokenResource(jedis);
    }
    else if(jedis != null)
    {
    pool.returnResource(jedis);
    }
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support