You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Connect your Java application to a Redis database
13
+
linkTitle: Jedis (Java)
14
+
title: Jedis guide (Java)
15
+
weight: 5
16
+
---
17
+
18
+
[Jedis](https://github.com/redis/jedis) is a synchronous Java client for Redis.
19
+
Use [Lettuce]({{< relref "/develop/connect/clients/java/lettuce" >}}) if you need
20
+
a more advanced Java client that also supports asynchronous and reactive connections.
21
+
The sections below explain how to install `Jedis` and connect your application
22
+
to a Redis database.
23
+
24
+
`Jedis` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions.
25
+
26
+
## Install
27
+
28
+
To include `Jedis` as a dependency in your application, edit the dependency file, as follows.
29
+
30
+
* If you use **Maven**:
31
+
32
+
```xml
33
+
<dependency>
34
+
<groupId>redis.clients</groupId>
35
+
<artifactId>jedis</artifactId>
36
+
<version>5.2.0</version>
37
+
</dependency>
38
+
```
39
+
40
+
* If you use **Gradle**:
41
+
42
+
```
43
+
repositories {
44
+
mavenCentral()
45
+
}
46
+
//...
47
+
dependencies {
48
+
implementation 'redis.clients:jedis:5.2.0'
49
+
//...
50
+
}
51
+
```
52
+
53
+
* If you use the JAR files, download the latest Jedis and Apache Commons Pool2 JAR files from [Maven Central](https://central.sonatype.com/) or any other Maven repository.
54
+
55
+
* Build from [source](https://github.com/redis/jedis)
56
+
57
+
58
+
## Connect and test
59
+
60
+
The following code opens a basic connection to a local Redis server:
After you have connected, you can check the connection by storing and
78
+
retrieving a simple string value:
79
+
80
+
```java
81
+
...
82
+
83
+
String res1 = jedis.set("bike:1", "Deimos");
84
+
System.out.println(res1); // OK
85
+
86
+
String res2 = jedis.get("bike:1");
87
+
System.out.println(res2); // Deimos
88
+
89
+
...
90
+
```
91
+
92
+
## More information
93
+
94
+
`Jedis` has a complete [API reference](https://www.javadoc.io/doc/redis.clients/jedis/latest/index.html) available on [javadoc.io/](https://javadoc.io/).
95
+
The `Jedis`[GitHub repository](https://github.com/redis/jedis) also has useful docs
96
+
and examples including a page about handling
97
+
[failover with Jedis](https://github.com/redis/jedis/blob/master/docs/failover.md)
98
+
99
+
See also the other pages in this section for more information and examples:
Copy file name to clipboardExpand all lines: content/develop/clients/jedis/connect.md
+4-126Lines changed: 4 additions & 126 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,51 +10,12 @@ categories:
10
10
- kubernetes
11
11
- clients
12
12
description: Connect your Java application to a Redis database
13
-
linkTitle: Jedis (Java)
14
-
title: Jedis guide (Java)
15
-
weight: 5
13
+
linkTitle: Connect
14
+
title: Connect to the server
15
+
weight: 2
16
16
---
17
17
18
-
[Jedis](https://github.com/redis/jedis) is a synchronous Java client for Redis.
19
-
Use [Lettuce]({{< relref "/develop/connect/clients/java/lettuce" >}}) if you need
20
-
a more advanced Java client that also supports asynchronous and reactive connections.
21
-
The sections below explain how to install `Jedis` and connect your application
22
-
to a Redis database.
23
-
24
-
`Jedis` requires a running Redis or [Redis Stack]({{< relref "/operate/oss_and_stack/install/install-stack/" >}}) server. See [Getting started]({{< relref "/operate/oss_and_stack/install/" >}}) for Redis installation instructions.
25
-
26
-
## Install
27
-
28
-
To include `Jedis` as a dependency in your application, edit the dependency file, as follows.
29
-
30
-
* If you use **Maven**:
31
-
32
-
```xml
33
-
<dependency>
34
-
<groupId>redis.clients</groupId>
35
-
<artifactId>jedis</artifactId>
36
-
<version>5.2.0</version>
37
-
</dependency>
38
-
```
39
-
40
-
* If you use **Gradle**:
41
-
42
-
```
43
-
repositories {
44
-
mavenCentral()
45
-
}
46
-
//...
47
-
dependencies {
48
-
implementation 'redis.clients:jedis:5.2.0'
49
-
//...
50
-
}
51
-
```
52
-
53
-
* If you use the JAR files, download the latest Jedis and Apache Commons Pool2 JAR files from [Maven Central](https://central.sonatype.com/) or any other Maven repository.
54
-
55
-
* Build from [source](https://github.com/redis/jedis)
56
-
57
-
## Connect
18
+
## Basic connection
58
19
59
20
The following code opens a basic connection to a local Redis server:
The following sections explain how to handle situations that may occur
409
-
in your production environment.
410
-
411
-
### Timeouts
412
-
413
-
To set a timeout for a connection, use the `JedisPooled` or `JedisPool` constructor with the `timeout` parameter, or use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters:
.socketTimeoutMillis(5000) // set timeout to 5 seconds
421
-
.connectionTimeoutMillis(5000) // set connection timeout to 5 seconds
422
-
.build(),
423
-
poolConfig
424
-
);
425
-
```
426
-
427
-
### Exception handling
428
-
429
-
The Jedis Exception Hierarchy is rooted on `JedisException`, which implements `RuntimeException`, and are therefore all unchecked exceptions.
430
-
431
-
```
432
-
JedisException
433
-
├── JedisDataException
434
-
│ ├── JedisRedirectionException
435
-
│ │ ├── JedisMovedDataException
436
-
│ │ └── JedisAskDataException
437
-
│ ├── AbortedTransactionException
438
-
│ ├── JedisAccessControlException
439
-
│ └── JedisNoScriptException
440
-
├── JedisClusterException
441
-
│ ├── JedisClusterOperationException
442
-
│ ├── JedisConnectionException
443
-
│ └── JedisValidationException
444
-
└── InvalidURIException
445
-
```
446
-
447
-
#### General exceptions
448
-
449
-
In general, Jedis can throw the following exceptions while executing commands:
450
-
451
-
-`JedisConnectionException` - when the connection to Redis is lost or closed unexpectedly. Configure failover to handle this exception automatically with Resilience4J and the built-in Jedis failover mechanism.
452
-
-`JedisAccessControlException` - when the user does not have the permission to execute the command or the user ID and/or password are incorrect.
453
-
-`JedisDataException` - when there is a problem with the data being sent to or received from the Redis server. Usually, the error message will contain more information about the failed command.
454
-
-`JedisException` - this exception is a catch-all exception that can be thrown for any other unexpected errors.
455
-
456
-
Conditions when `JedisException` can be thrown:
457
-
- Bad return from a health check with the [`PING`]({{< relref "/commands/ping" >}}) command
458
-
- Failure during SHUTDOWN
459
-
- Pub/Sub failure when issuing commands (disconnect)
460
-
- Any unknown server messages
461
-
- Sentinel: can connect to sentinel but master is not monitored or all Sentinels are down.
462
-
- MULTI or DISCARD command failed
463
-
- Shard commands key hash check failed or no Reachable Shards
464
-
- Retry deadline exceeded/number of attempts (Retry Command Executor)
465
-
- POOL - pool exhausted, error adding idle objects, returning broken resources to the pool
466
-
467
-
All the Jedis exceptions are runtime exceptions and in most cases irrecoverable, so in general bubble up to the API capturing the error message.
468
-
469
-
### DNS cache and Redis
470
-
471
-
When you connect to a Redis server with multiple endpoints, such as [Redis Enterprise Active-Active](https://redis.com/redis-enterprise/technology/active-active-geo-distribution/), you *must*
472
-
disable the JVM's DNS cache. If a server node or proxy fails, the IP address for any database
473
-
affected by the failure will change. When this happens, your app will keep
474
-
trying to use the stale IP address if DNS caching is enabled.
description: Get your Jedis app ready for production
13
+
linkTitle: Production usage
14
+
title: Production usage
15
+
weight: 3
16
+
---
17
+
18
+
The following sections explain how to handle situations that may occur
19
+
in your production environment.
20
+
21
+
### Timeouts
22
+
23
+
To set a timeout for a connection, use the `JedisPooled` or `JedisPool` constructor with the `timeout` parameter, or use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters:
.socketTimeoutMillis(5000) // set timeout to 5 seconds
31
+
.connectionTimeoutMillis(5000) // set connection timeout to 5 seconds
32
+
.build(),
33
+
poolConfig
34
+
);
35
+
```
36
+
37
+
### Exception handling
38
+
39
+
The Jedis Exception Hierarchy is rooted on `JedisException`, which implements `RuntimeException`, and are therefore all unchecked exceptions.
40
+
41
+
```
42
+
JedisException
43
+
├── JedisDataException
44
+
│ ├── JedisRedirectionException
45
+
│ │ ├── JedisMovedDataException
46
+
│ │ └── JedisAskDataException
47
+
│ ├── AbortedTransactionException
48
+
│ ├── JedisAccessControlException
49
+
│ └── JedisNoScriptException
50
+
├── JedisClusterException
51
+
│ ├── JedisClusterOperationException
52
+
│ ├── JedisConnectionException
53
+
│ └── JedisValidationException
54
+
└── InvalidURIException
55
+
```
56
+
57
+
#### General exceptions
58
+
59
+
In general, Jedis can throw the following exceptions while executing commands:
60
+
61
+
-`JedisConnectionException` - when the connection to Redis is lost or closed unexpectedly. Configure failover to handle this exception automatically with Resilience4J and the built-in Jedis failover mechanism.
62
+
-`JedisAccessControlException` - when the user does not have the permission to execute the command or the user ID and/or password are incorrect.
63
+
-`JedisDataException` - when there is a problem with the data being sent to or received from the Redis server. Usually, the error message will contain more information about the failed command.
64
+
-`JedisException` - this exception is a catch-all exception that can be thrown for any other unexpected errors.
65
+
66
+
Conditions when `JedisException` can be thrown:
67
+
- Bad return from a health check with the [`PING`]({{< relref "/commands/ping" >}}) command
68
+
- Failure during SHUTDOWN
69
+
- Pub/Sub failure when issuing commands (disconnect)
70
+
- Any unknown server messages
71
+
- Sentinel: can connect to sentinel but master is not monitored or all Sentinels are down.
72
+
- MULTI or DISCARD command failed
73
+
- Shard commands key hash check failed or no Reachable Shards
74
+
- Retry deadline exceeded/number of attempts (Retry Command Executor)
75
+
- POOL - pool exhausted, error adding idle objects, returning broken resources to the pool
76
+
77
+
All the Jedis exceptions are runtime exceptions and in most cases irrecoverable, so in general bubble up to the API capturing the error message.
78
+
79
+
### DNS cache and Redis
80
+
81
+
When you connect to a Redis server with multiple endpoints, such as [Redis Enterprise Active-Active](https://redis.com/redis-enterprise/technology/active-active-geo-distribution/), you *must*
82
+
disable the JVM's DNS cache. If a server node or proxy fails, the IP address for any database
83
+
affected by the failure will change. When this happens, your app will keep
84
+
trying to use the stale IP address if DNS caching is enabled.
0 commit comments