Jedis appears connection timeout problem resolution (Jedispool connection pool usages) _java

Source: Internet
Author: User

Today found Jedis default connection mode jedis=new Jedis ("localhost", 6379), always occurs connection timeout. It was later found that the Jedis package also has a way to set the maximum connection time.

1-> acquiring Jedis instances needs to be obtained from the Jedispool;
2-> Jedis case needs to be returned to Jedispool;
3-> if the Jedis in the use of errors, you also need to return jedispool;
The code is as follows

Copy Code code as follows:

Jedispoolconfig config = new Jedispoolconfig ();

Config.setmaxactive (100);

Config.setmaxidle (20);

Config.setmaxwait (1000l);
Jedispool Pool;
Pool = new Jedispool (config, "2xx.xx.xx.14", 6379);

Boolean borroworoprsuccess = true;
try {
Jedis = Pool.getresource ();
Do Redis opt by instance
catch (Jedisconnectionexception e) {
Borroworoprsuccess = false;
if (Jedis!= null)
Pool.returnbrokenresource (Jedis);

finally {
if (borroworoprsuccess)
Pool.returnresource (Jedis);
}
Jedis = Pool.getresource ();

Jedispool relies on Apache class packs

Commons-pool-1.5.6.jar

Although 1-> throws Jedisconnectionexception, there are actually two types of errors, one is Pool.getreource (), no Jedis instance is available, and the other is jedis.set/ Get error will also throw this exception, in order to achieve the distinction, so based on whether the instance is null to achieve, if it is null to prove that instance is not initialized at all, do not return to pool; if instance is not NULL, It is proved that it needs to be returned to the pool;
2-> in instance error, also call Returnbrokenresource return to pool, otherwise the next time through GetResource get instance buffer may still exist data, there is a problem!

The configuration parameters of Jedispool depend to a great extent on the actual application requirements and the hardware and software capabilities. I've never used commons-pool before, so this time I spent a whole room looking at the meaning of these parameters ... Most of the configuration parameters for Jedispool are assigned by the corresponding entries of Jedispoolconfig.

Maxactive: Controls how many Jedis instances of a pool can be allocated, obtained by Pool.getresource (), if the assignment is-1, is not limited, and if pool has already allocated maxactive Jedis instance, Then the state of pool becomes exhausted, in Jedispoolconfig

Maxidle: Controls the maximum number of Jedis instances in a pool with a status of idle;

Whenexhaustedaction: Represents the action to be taken by the pool when Jedis instances are allocated in the pool; there are three kinds of when_exhausted_fail by default (when there are no Jedis instances, they are thrown directly

nosuchelementexception), When_exhausted_block (it means blocking, or throwing jedisconnectionexception when maxwait is reached), when_exhausted_ GROW (then a new Jedis instance, also said that the set of maxactive useless);

Maxwait: Indicates that when a Jedis instance is borrow, the maximum waiting time, if more than the wait time, is thrown directly jedisconnectionexception;

Testonborrow: Whether alidate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instance is available;

Testonreturn: When return to pool, whether the validate operation in advance;

Testwhileidle: True to indicate that a idle object Evitor thread scans idle object and that this object is dropped from the pool if the validate fails ; This is only meaningful if the Timebetweenevictionrunsmillis is greater than 0;

Timebetweenevictionrunsmillis: Represents the number of milliseconds to sleep between idle object Evitor two scans;

Numtestsperevictionrun: Represents the maximum number of objects that the idle object evitor scans each time;

Minevictableidletimemillis: Represents the minimum time at which an object stays in the idle state before it can be scanned and expelled by the idle object Evitor ; This is only meaningful if the Timebetweenevictionrunsmillis is greater than 0;

Softminevictableidletimemillis: On the basis of minevictableidletimemillis, at least minidle objects have been added to the pool. If the -1,evicted does not expel any objects according to idle time. If minevictableidletimemillis>0, this setting is meaningless, and only if the Timebetweenevictionrunsmillis is greater than 0;

When Lifo:borrowobject returns an object, it uses the Default_lifo (last in-in-one, the most frequently used queue that resembles cache) and, if False, the FIFO queue;

Where Jedispoolconfig the default settings for some parameters are as follows:
Testwhileidle=true
minevictableidletimemills=60000
timebetweenevictionrunsmillis=30000
Numtestsperevictionrun=-1

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.