Connect to redis using jedis2.8.0 and connect to redis using jedis2.8.0

Source: Internet
Author: User

Connect to redis using jedis2.8.0 and connect to redis using jedis2.8.0

Download the latest jedis client jedis2.8.0 and find the tool class for using the connection pool of jedis on the Internet. It is found that the jedis is of a lower version:

ReturnResource ();

ReturnBrokenResource ();

The two methods have expired ~, Unable to find the latest version, force Installation failed ~~

So I looked at the source code and found that the comments of the source code were clearly written:

/**   * @deprecated starting from Jedis 3.0 this method will not be exposed.   * Resource cleanup should be done using @see {@link redis.clients.jedis.Jedis#close()}   */  @Override  @Deprecated  public void returnResource(final Jedis resource) {    if (resource != null) {      try {        resource.resetState();        returnResourceObject(resource);      } catch (Exception e) {        returnBrokenResource(resource);        throw new JedisException("Could not return the resource to the pool", e);      }    }  }

You can use the close () method to trace the close () method:

@Override  public void close() {    if (dataSource != null) {      if (client.isBroken()) {        this.dataSource.returnBrokenResource(this);      } else {        this.dataSource.returnResource(this);      }    } else {      client.close();    }  }

DataSource is the defined connection pool. If the connection pool is not empty, perform some restore operations. The jedis connection pool is designed based on org. apache. commons. pool2, these two methods are not followed, but the connection property value is changed to available and so on.

If the connection pool is empty, the connection to the redis service will be disconnected ~

 

So when writing some basic classes, it should look like this:

public <T> void set(T item) {        Jedis jedis =null;        try {            jedis = JedisClient.getJedis();            String v = cacheKeyUtil.getDefaultKey(item);            jedis.set(v, iserializer.serialize(item));        } catch (Exception e) {            logger.warn("set", item, e);        } finally {
      if(null != jsdis){
    jedis.close();
        }                    }    }

If you borrow from someone else, you must pay it back ~~~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.